Blog Spot!


JavaScript pub-sub pattern, event emitter made simple

Pub-Sub pattern:


var events = {
    events: {},

    on: function (eventName, fn) {
        this.events[eventName] = this.events[eventName] || [];
        this.events[eventName].push(fn);
    },

    off: function(eventName, fn) {
        if (this.events[eventName]) {
            for (var i = 0; i < this.events[eventName].length; i++) {
                if (this.events[eventName][i] === fn) {
                    this.events[eventName].splice(i, 1);
                    break;
                }
            }
        }
    },

    emit: function (eventName, data) {
        if (this.events[eventName]) {
            this.events[eventName].forEach(function(fn) {
                fn(data);
            });
        }
    }
};

Added on 28.Mar.2017
Tags: js events pub-sub functions

Vim config file default settings

use or create ~/.vimrc

inside put:

" colour
syntax on

" show line numbers
"set nu

" tab size
set tabstop=4

" indent lines
set listchars=tab:\¦\
set list

Added on 24.Mar.2017
Tags: vim config

Convert decimal to hex in JavaScript

Convert to HEX with prepad

function toHex(i, pad) {

  if (typeof(pad) === 'undefined' || pad === null) {
    pad = 6;
  } 

  var strToParse = i.toString(16);

  while (strToParse.length < pad) {
    strToParse = "0" + strToParse;
  }

  return strToParse;
}

console.log(toHex(572886, 6));

Added on 23.Mar.2017
Tags: prepadding js pad functions

Generic simple nginx server config

server {
        listen 3000 default_server;
        listen [::]:3000 default_server;
        server_name redis.donvercety.biz;

        index index.html index.htm index.php;

        root /var/www/html/redis;

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

}

Added on 22.Mar.2017
Tags: server nginx generic

Adding extra zeros in front of a number - prepadding

function pad (str, max) {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
}

pad("3", 3);    // => "003"
pad("123", 3);  // => "123"
pad("1234", 3); // => "1234"

var test = "MR 2";
var parts = test.split(" ");
parts[1] = pad(parts[1], 3);
parts.join(" "); // => "MR 002"

stackoverflow

Added on 16.Mar.2017
Tags: js pad functions

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top