JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 459
  • Score
    100M100P100Q91726F
  • License ISC

Useful utilities and rare b-sides.

Package Exports

  • deep-cuts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (deep-cuts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.

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'
}

**/

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