GitXplorerGitXplorer
l

node-utilities

public
2 stars
2 forks
0 issues

Commits

List of commits on branch master.
Unverified
7e5b9cbbee7f8787e0d57d46f8febf9b7fbf2ca7

fix build versions, drop legacy node version

llimianwang committed 7 years ago
Unverified
bda4e2499434bf55ef6cb7c0d4574dea9cb7a68a

add CircleCI config file

llimianwang committed 7 years ago
Unverified
aac2c0f670556325d92850bf53bc218970ae4639

deps: update and upgrade dependencies

llimianwang committed 7 years ago
Unverified
5a7c53fda762a5163bbf5b39b59ebd1571277c1a

deps: update to latest

llimianwang committed 8 years ago
Unverified
5273109127380343823ef5adcf6c4d8519873818

update packages

llimianwang committed 8 years ago
Unverified
7bd4f190fdf7cd620b682b6be2b5b5ecfcb759f7

use codecov for coverage

llimianwang committed 8 years ago

README

The README file for this repository.

node-helper-utilities

a generic utilities library for node.js

Build Status Coverage Status github-tag npm npm Dependency Status devDependency Status

Installation

Install and use it via npm.

npm install node-helper-utilities --save

Usage

Clean: Filter out all non-true values

var util = require('./');

var obj = {
  a: false,
  b: true
};

var b = util.clean(obj);
console.log(b);
//  {
//    b: true
//  }

Clone: Clone an object

var util = require('./');

var obj = {
  key: 'value',
  key2: {
    field : ['a','b']
  }
};

var cloned = util.clone(obj);

Merge: Merge two objects together

var util = require('./');

var a = {
  a: 'b'
};

var b = {
  b: 'c'
};

var out = util.merge(a, b);

Cluster: Helper to start process in a cluster.

Takes optional config as follow:

{
  "enable": true,
  "instances": 5
}

By default, the instances is set to (Max CPUs - 1).

var util = require('./');
var http = require('http');

function start() {
  http.createServer(function(req, res) {}).listen(3000);
}
util.cluster(function() {
  // start server...
  start();
});

// Or using configs.

util.cluster(config, function() {
  // start server...
  start();
});

Token: Token creator

Create uniquely generated tokens.

var util = require('./');

util
  .unique()
  .then(function(token) {
    console.log(token);
  })
  .catch(function(err) {
    // some error happened;
    console.log(err);
  });

// or allow prefix.

util
  .unique('someprefix')
  .then(function(token) {
    console.log(token); // someprefix:<unique>
  })
  .catch(function(err) {
    assert(err);
  });

Encrypt Sensitive Information (using bcrypt)

var util = require('./');

util
  .hash('password')
  .then(function(hashed) {
    console.log(hashed);
  })
  .catch(function(err) {
    console.log(err);
  });

util
  .compare('password', hashed)
  .then(function(result) {
    console.log(result);
  })
  .catch(function(err) {
    console.log(err);
  });

Defer

Defer a function to a later time (default: 0 ms, ie. next iteration)

var util = require('./');

util
  .defer(100)
  .then(function() {
    console.log('this happened 100 ms later!');
  });

console.log('this happens immediately!');

Parse JSON

Often when you deal with async processes and have to pass data as JSON around, there is always potential for errors. This function is to parse json safely, and return a promise.

var util = require('./');
var couldBeJSONcouldBeString = ...;

util
  .parse(couldBeJSONcouldBeString)
  .then(function(parsed) {
    // parsed version of `couldBeJSONcouldBeString`
    console.log(parsed);
  })
  .catch(function(err) {
    // handle error here
    console.log(err);
  });

Read and Write File

var util = require('./');

var path = 'some_path';

util
  .write(path, 'hello world!')
  .then(function() {
    return util.read(path);
  })
  .then(function(data) {
    console.log(data); // hello world!
  })
  .catch(function(err) {
    console.log(err.stack);
  });

Padder

var util = require('./');

var str = 'h';

var padded = util.padder(str, { prefix: 'x', length: 5 });

console.log(padded); // 'xxxxh'

Tests

All tests are within tests.

Run tests using make test or make test-cov for test coverage.

TravisCI build is tested against all versions >= 4.x