Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-helper-utilities

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-helper-utilities

Generic helper utilities

  • 1.7.5
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

node-helper-utilities

a generic utilities library for node.js

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

Installation

Install and use it via npm.

npm install node-helper-utilities --save

Usage

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);

Memoize: memoize function responses

var util = require('./');

function echo(i) {
  return i;
}

var cache = memoize(echo);

cache(1);
cache(1); // returns from cache

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);
  });

Tests

All tests are within tests.

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

TravisCI build is tested against:

  • 0.10
  • 0.11
  • 0.12
  • iojs (latest)

FAQs

Package last updated on 01 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc