npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-helper-utilities

v3.0.0

Published

Generic helper utilities

Downloads

26

Readme

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