Skip to content

waldez/bookOfSnippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Useful code snippets

Javascript

Actual type of value (returns string)

// to get an actual type of the value
const type = typeof value == 'object' && value !== null ?
        Object.getPrototypeOf(value).constructor.name :
        typeof value;

Lazy value & getter

// /**
//  * @param  {function} lazyFunction
//  * @return {function}
//  */
function createLazyValue(lazyFunction) {

    if (typeof lazyFunction != 'function') {
        throw new Error(`'lazyFunction' parameter should be of type 'funtion' but is ${typeof lazyFunction}!`);
    }

    let value = undefined;
    let called = false;

    return function(...args) {

        if (called) {
            return value;
        } else {
            value = lazyFunction(...args);
            called = true;
            return value;
        }
    };
}

/**
 * @param  {Object} instance
 * @param  {*} key
 * @param  {function} lazyFunction
 * @return {Object}
 */
function createLazyGetter(instance, key, lazyFunction) {

    Object.defineProperty(instance, key, {
        enumerable: true,
        configurable: true,
        get: createLazyValue(lazyFunction)
    });

    return instance;
}

Floating point error treshold & equality

// compute the threshold
const THRESHOLD = (0.1 + 0.2 - 0.3) * 2;

function equals(a, b, threshold = THRESHOLD) {

    return (a < b ? b - a : a - b) < threshold;
}

if (true /*run tests*/) {
    const a = 0.1 + 0.2;
    const b = 0.3;
    const b2 = 0.300000000000001;
    const b3 = 0.3000000000000001;

    // item: [ computed_equality_value, expected_result ]
    const cases = [
        [a === b, false],
        [equals(a, b), true],
        [equals(a, b2), false],
        [equals(a, b3), true]
    ];

    cases.forEach((item, index) => {
        if (item[0] != item[1]) {
            throw new Error(`Test case with index ${index} failed! ${item[0]} should equal to ${item[1]}!`);
        }
    });
}

Simple command line argument getter

function getArg(argv, name) {
    // search for name from index 2
    const argIndex = argv.indexOf('-' + name, 2);
    if (argIndex === -1) {
        return null;
    }

    let argValue = argv[argIndex + 1];
    return typeof argValue == 'string' && argValue[0] !== '-' ? argValue : true;
}

Naive and simple delayed call (usefull for testing)

/**
 * Promise version of delayed function call. Good for testing, great with async/await to flatten the code
 * @param {Function} fn
 * @param {number} delay
 * @return {Promise}
 */
function delayedCall(fn, delay) {

    return new Promise((resolve, reject) => setTimeout(() => fn() || resolve(), delay));
}

Semioptimal & optimal version of fibonacci function

// semioptimal
function fibonacci(n) {

    let result = [0, 1, 1, 2];
    let index = result.length;
    while (n >= index) {
        result.push(result[index - 1] + result[index++ - 2]);
    }

    return result[n];
}

// optimal version
function fibonacciOptimal(n) {

    if (n === 0) return 0;
    if (n === 1 || n === 2) return 1;

    let tmp, n1 = 1, n2 = 1, index = 2;
    while (n > index++) {
        tmp = n2;
        n2 = n2 + n1;
        n1 = tmp;
    }

    return n2;
}

Async/await brokes custom Promise implementations

const OrigPromise = global.Promise;
const Promise = require('bluebird');

async function MakePromise(obj) { return obj; }
async function MakePromiseAwait(obj) { return await obj; }

const bPromise = new Promise((res,rej) => res(666));
const oPromise = new OrigPromise((res,rej) => res(666));

function whichPromise(obj) {

    if (obj instanceof OrigPromise) {
        return 'original';
    }

    if (obj instanceof Promise) {
        return 'bluebird';
    }

    return 'not any promise';
}

console.log('whichPromise(oPromise) is', whichPromise(oPromise));
console.log('whichPromise(bPromise) is', whichPromise(bPromise));
console.log('whichPromise(42) is', whichPromise(42));
console.log('MakePromise(42) is', whichPromise(MakePromise(42)));
console.log('MakePromise(oPromise) is', whichPromise(MakePromise(oPromise)));
console.log('MakePromise(bPromise) is', whichPromise(MakePromise(bPromise)));
console.log('MakePromiseAwait(42) is', whichPromise(MakePromiseAwait(42)));
console.log('MakePromiseAwait(oPromise) is', whichPromise(MakePromiseAwait(oPromise)));
console.log('MakePromiseAwait(bPromise) is', whichPromise(MakePromiseAwait(bPromise)));

bPromise.finally(() => {
    console.log(`Whooohooo!`);
});

try {
    MakePromise(bPromise).finally(() => {
        console.log(`Whooohooo!`);
    });
} catch(err) {
    console.log('Yep! ES7 is also fucked', err);
}

Usefull undocumented V8 functions

// returns active handles stuck on engine
process._getActiveHandles();
// returns active requests stuck in event queue
process._getActiveRequests();

About

The book of useful code snippets.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published