w43l / nodeON-helpers

A collection of helper funcs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nodeON-helpers

A Collection of helper methods.

Build Status

Install

Install the module using NPM:

npm install nodeon-helpers --save

Table of Contents

  1. API
    1. Set a salt string
    2. Generate a random string
    3. Generate a random number
    4. Hash a string using bcrypt
    5. Verify a hashed string match
    6. Get a url safe string
    7. Truncate arguments from a function
    8. Skip arguments from a function
    9. Will copy an array over an existing one
    10. Get the current user HOME dir
    11. Determine if a value is numeric
    12. Determine if Express Request Accepts JSON
    13. Zero Padding on a number 2 --> '002'

API

Set a salt string

helpers.setSalt(salt)

  • salt string Any string.

Use it once to set a salt for the crypto functions.

[⬆]

Generate a random string

helpers.generateRandomString(optLength)

  • optLength number= Define length, default 32.

Returns string The random string.

Returns a randomized string.

[⬆]

Generate a random number

helpers.generateRandomNumber(optLength)

  • optLength number= Define length, default 20.

Returns string The random string of numbers.

Returns a randomized string only with numbers.

[⬆]

Hash a string using bcrypt

helpers.hash(text, optOpts, done)

  • text string The string to hash.
  • optOpts Object= Optionally define options.
  • done Function() Node.js style callback.

Hashes a string using the bcrypt library.

Bcrypt will only hash strings up to 72 chars long. If the passed string is longer than that the helpers.hash method will fail with a warning. To ignore that behavior set the ignoreLimit option to true:

helpers.hash(longString, {ignoreLimit: true}, function(err, res) {
    // Ignoring limit will not create an error
    expect(err).to.be.null;
    expect(res).to.be.a('string');
});

[⬆]

Verify a hashed string match

helpers.hashVerify(hash, text, done)

  • hash string The hashed string.
  • text string The string to test.
  • done Function(boolean) Callback with a single argument, boolean.

Tests if the given string matches the provided hash.

[⬆]

Get a url safe string

helpers.urlify(text, optRandLen)

  • text string The string to urlify.
  • optRandLen number How many numbers to use for randomizing the url, default 6.

Get a url safe string.

var helpers = require('nodeon-helpers');

var urlString = helpers.urlify('a name with spaces');

console.log(urlString);
// prints: "458202-a-name-with-spaces"

[⬆]

Truncate arguments from a function

helpers.truncateArgs(fn, count, optSelf)

  • fn Function The function to truncate arguments.
  • count number How many arguments to allow before truncating.
  • optSelf Object= Optionally apply context.

Return Function The function to invoke.

Will truncate arguments from a function.

var helpers = require('nodeon-helpers');

function run(one, two, three) {
    console.log(one); // prints 1
    console.log(two); // prints "undefined"
    console.log(three); // prints "undefined"
}

var fn = helpers.truncateArgs(run, 1);

fn(1, 2, 3);

[⬆]

Skip arguments from a function

helpers.skipArgs(fn, count, optSelf)

  • fn Function The function to skip arguments for.
  • count number How many arguments to skip.
  • optSelf Object= Optionally apply context.

Return Function The function to invoke.

Will skip the first n arguments from a function.

var helpers = require('nodeon-helpers');

function run(one) {
    console.log(one); // prints 3
}

var fn = helpers.skipArgs(run, 2);

fn(1, 2, 3);

[⬆]

Will copy an array over an existing one

helpers.pushCopy(src, dst)

  • src Array The source array.
  • dst Array The destination array.

Will copy an array over an existing one.

var helpers = require('nodeon-helpers');

var src = [4,5,6];
var dst = [1,2,3];

helpers.pushCopy(src, dst);

console.log(dst);
// prints: [1, 2, 3, 4, 5, 6]

[⬆]

Get the current user HOME dir.

helpers.getUserHome()

Return string The full path to the user's HOME.

Get the user's HOME directory.

[⬆]

Determine if a value is numeric.

helpers.isNumeric(value)

  • value string|number The value to check.

Return boolean If the value is numeric.

[⬆]

Determine if Express Request Accepts JSON

helpers.isRequestJson(req)

  • req Object The Express request object.

Return boolean If client accepts JSON.

[⬆]

Zero Padding on a number

helpers.zeroPadding(number, width)

  • number number The number to apply zeropadding on.
  • number width The padding.

Return string The zero padded number.

var padded = helpers.zeroPadding(2, 3);
// '002'

[⬆]

Release History

  • v0.1.9, 17 Aug 2015
    • Added option to ignore char limit for hash.
  • v0.1.8, 14 Aug 2015
    • Will now return error if string for hash is 72chars or longer, bcrypt will not handle it.
    • Upgraded all dependencies to latest.
  • v0.1.7, 14 May 2015
    • Made all npm dependencies specific, thank you @kbariotis.
  • v0.1.6, 03 Apr 2015
    • Added the zeroPadding method.
  • v0.1.5, 11 Dec 2014
    • Added the isRequestJson method.
  • v0.1.4, 24 Oct 2014
    • Added skipArgs() method.
  • v0.1.3, 19 Sep 2014
    • Added isNumeric() method.
  • v0.1.0, 14 Aug 2014
    • Big Bang

License

Copyright ©2015 Thanasis Polychronakis. Licensed under the MIT license.

About

A collection of helper funcs

License:MIT License


Languages

Language:JavaScript 100.0%