Blog Spot!


JavaScript Replace object-based values in text

if (typeof String.prototype.supplant !== 'function') {
    String.prototype.supplant = function (o) {
        return this.replace(/{{([^{}]*)}}/g, function (a, b) {
            var r = o[b];
            return typeof r === 'string' ? r : a;
        });
    };
}

var obj = {
    a: "text",
    b: "text 2",
    c: "text 3"
}

var stringA = "http://{{a}}.something.com/",
    stringB = "http://something.{{b}}.com/",
    stringC = "http://something.com/{{c}}";

alert(stringA.supplant(obj));

stackoverflow

Added on 28.Jan.2016
Tags: js replace template function

Best resources to learn JavaScript

Added on 28.Jan.2016
Tags: js

Escape HTML tags in JavaScript

function escapeHTML(text) {  
    var replacements = {
        "<": "&lt;",
        ">": "&gt;",
        "&": "&amp;",
        "\"": "&quot;"
    };

    return text.replace(/[<>&"]/g, function(character) {  
        return replacements[character];  
    }); 
}

Added on 28.Jan.2016
Tags: escape escaping html js function

Shuffle JavaScript Array

var list = [1,2,3,4,5,6,7,8,9];

list = list.sort(function() {
    return Math.random() - 0.5;
});

console.log(list);

Added on 28.Jan.2016
Tags: js shuffle array function advanced tricks

Example tail implementation in javascript

Sometimes you need to do things in order. Do one thing, wait for it to finish, maybe asynchronous, and the continue with the next one. This in programming is called a tail implementation. Here is a very basic tail in javascript.

var data  = [16, 11, 9, 7, 26, 25],
    len   = data.length,
    tail  = [],
    start = null,
    i;

// simulate lots of data
for (i = 0; i < len; i++) { 
  go({pos:i, val:data[i]});
}

/**
 * Entry point - incomming data!
 */
function go(obj) {

    // add data to tail array
    tail.push(obj);

    // start first run, on first data entry
    if (!start) {
        run(tail.shift(), finish);

        // start the loop
        start = true;
    }
}

/**
 * Run command from tail array
 */
function run(obj, cb) {
    var delay = getRandomInt(1000, 5000);

    // simulates job, with different time execution
    setTimeout(function() {
        console.log("CMD: " + obj.pos + ":" + obj.val);
        cb({isok:"OK", delay:delay});
    }, delay);
}

/**
 * Callback from run(). Hare we contionue the
 * execution if tail array contains other commands.
 */
function finish(res) {
    console.log("RES: isok:" + res.isok + " delay:" + res.delay);
    console.log("INF: tail length:" + tail.length);
    console.log("==========");

    // if no data in array, stop execution
    if (tail.length === 0) {

        // stop the loop
        start = null;
        return;

    // if still data continue execution with next tail element
    } else {
        run(tail.shift(), finish);
    }
}

/**
 * Randomly generated delay
 */
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Added on 26.Jan.2016
Tags: js tail programming

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