npm

Need private packages and team management tools?Check out npm Orgs. »

deep-cuts

1.3.0 • Public • Published

Deep Cuts

This project is a collection of otherwise uncategorized utility functions. Requires Lodash as peer dependency, be warned.

Installation

npm install --save lodash
npm install --save deep-cuts

Getting Started

Just require the module, every method lives at the top level.

const { safeJsonParse } = require('deep-cuts');
 
console.log(safeJsonParse(JSON.stringify({message: 'I will be safely parsed'})));

Methods

acceptNoArguments(fn, [args])

Creates a function that accepts no arguments at call time. Behaves similar to lodash partial method, arguments can be provided at create time. None can be passed at call time.

function myFunction(...args) {
  console.log(`I was called with ${args.length} arguments.`);
}
 
acceptNoArguments(myFunction)(0, 1, 2); // I was called with 0 arguments.
 
acceptNoArguments(myFunction, 3, 4)(0, 1, 2); // I was called with 2 arguments.

escapeForRegExp(str)

Escapes any special characters so the strings can safely be placed in a RegExp constructor.

console.log(escapeForRegExp('function test() { return 5 * 5; }')); // function test\(\) \{ return 5 \* 5; \}

flattenObject(obj)

Flattens an object so that every property is available at the top-level via the same key path as a property string. Compatible with lodash .get / .set.

const obj = {
  name: {
    first: 'Lemmy',
    last: 'Kilmister'
  },
  favoriteColors: [
    { name: 'Black' },
    { name: 'Red' }
  ]
};
 
flattenObject(obj);
 
/** Output
 
{
  'name.first': 'Lemmy',
  'name.last': 'Kilmister',
  'favoriteColors[0].name': 'Black',
  'favoriteColors[1].name': 'Red'
}
 
**/

isJsonString(str)

Returns a boolean that specifies whether the string is parsable JSON.

const str = JSON.stringify({red: 5, green: 6});
 
isJsonString(str); // true

stringToBoolean(str)

Usually used for url parameters, converts null, undefined, 0, false, or '' to false even if they are strings. All other values are true.

console.log(stringToBoolean('0')); // false
 
console.log(stringToBoolean('null')); // false
 
console.log(stringToBoolean('undefined')); // false
 
console.log(stringToBoolean('false')); // false
 
console.log(stringToBoolean('')); // false
 
console.log(stringToBoolean(0)); // false
 
console.log(stringToBoolean(null)); // false
 
console.log(stringToBoolean(undefined)); // false
 
console.log(stringToBoolean(false)); // false
 
console.log(stringToBoolean()); // false
 
console.log(stringToBoolean(1)); // true
 
console.log(stringToBoolean({})); // true
 
console.log(stringToBoolean(true)); // true

safeJsonParse(strObj)

Wrapper around JSON.parse that will not throw errors for nil or poorly formatted strings. Returns null in any invalid case.

console.log(safeJsonParse("{\"message\": \"I will be safely parsed\"}")); // I will be safely parsed
 
console.log(safeJsonParse("{\"bad_key: \"value\"}")); // null
 
console.log(safeJsonParse(undefined)); // null

Contribution Guidelines

Fork the respository and install all the dependencies:

npm install

Make sure to run the unit tests (and lint) before committing. Obviously, add to the tests as you make changes:

npm run test

For watch:

npm run test:watch

install

npm i deep-cuts

Downloadsweekly downloads

19

version

1.3.0

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability