JSPM

  • Created
  • Published
  • Downloads 492
  • Score
    100M100P100Q83387F
  • License MIT

JS utility methods for NODE and Browser.

Package Exports

  • js-flock

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (js-flock) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

js-flock

npm install js-flock --save

JS utility methods for NODE and Browser. Requires ES6 environment as minimum to run the code.

Methods:

promisify

Promisify error first callback function

  const promisify = require('js-flock').promisify;
  const readFile = require("fs").readFile;

  const readFileAsync = promisify(readFile); // Promise version of read file

  // Native version of read file
  readFile('test.txt', 'utf8', (err, data) => {
    if (err) {
      return console.log(err);
    }
    console.log(data);
  });

  // Promisify version
  readFileAsync('test.txt', 'utf8')
    .then((data) => console.log(data))
    .catch((err) => console.log(err));

collar

Set maximum waiting time for promise to resolve. Reject promise if it's not resolved in that time

  const collar = require('js-flock').collar;

  const MAX_WAIT_TIME = 500;

  collar(Promise.all([
    new Promise((resolve) => setTimeout(resolve, 50, '1')),
    new Promise((resolve) => setTimeout(resolve, 100, '2'))
  ]), MAX_WAIT_TIME)
  .then(([first, second]) => {
    console.log('this is called as promises are resolved before timeout')
  }).catch((err) => console.log('this is not called'));

  collar(
    new Promise((resolve) => setTimeout(resolve, 1000, '1')),
    MAX_WAIT_TIME
  ).then((data) =>
    console.log('this is not called as promise is not resolved in max waiting time');
  ).catch((err) => {
    console.log('this is called');
    console.log(err); // err = 'Promises have timed out'
  });

  collar(Http.get('test-url'), MAX_WAIT_TIME)
    .then((response) => console.log(response))
    .catch((err) => console.log('promise have timed out'));

toEnum

Convert object or list of strings to enum representation. Enum representation is immutable (frozen)

  const toEnum = require('js-flock').toEnum;

  const vehicleType = toEnum({
    TRUCK: 'TRUCK',
    CAR: 'CAR'
    MOTORBIKE: 'MOTORBIKE',
    CAMPER: 'CAMPER'
  });

  // We can also use short notation to define above enum when keys are equal to values. Both will result in same enumeration representation

  cont vehicleType = toEnum(['TRUCK', 'CAR', 'MOTORBIKE', 'CAMPER']);

  if(vehicle.type === vehicleType.TRUCK) {
    // Special behaviour only for truck vehicles
  }

  vehicleType.TRUCK = 'boat';
  vehicleType.BOAT = 'BOAT';

  console.log(vehicleType.TRUCK) // TRUCK - enum is immutable
  console.log(vehicleType.BOAT) // undefined - enum is immutable

  // Following enum can't be written in short notation as keys are different then values

  const gender = toEnum({
    MAN: 'M',
    WOMEN: 'W',
    OTHER: 'O'
  });

  // Enum helpers

  gender.keys(); // return array of keys ['MAN', 'WOMEN', 'OTHER']
  gender.values(); // return array of values ['M', 'W', 'O']

  gender.exists('W'); // true
  gender.exists('T'); // false

  gender.haveKey('MAN') // true
  gender.haveKey('CAR') // false

deepFreeze

Recursively apply Object.freez

  const deepFreeze = require('js-flock').deepFreeze;

  const person = {
    fullName: 'test person'
    dob: new Date()
    address: {
      country: 'testiland',
      city: 'this one'
    }
  }

  Object.freeze(person);

  Object.isFrozen(person) // true
  Object.isFrozen(person.address) // false UH OH

  deepFreeze(person);

  Object.isFrozen(person) // true
  Object.isFrozen(person.address) // true WE HE

deepSeal

Recursively apply Object.seal. For example check deepFreeze