0.0.4 • Published 6 years ago
youneedme v0.0.4
Lightweight utilities that you will need sooner or later.
Installation
Using npm:
$ npm i youneedme -s
In Node.js:
// Load the full build
const ynm = require('youneedme');
// Load only what you need
const { safeUse } = require('youneedme');
Usage
ynm.safeUse(source, path, defaultValue)
source
(required) string|object could be an object literal, array or string
path
(optional) string|Array A path to the desired value located in the source object
Please note that
path
it will be considered only whensource
it's of type 'object'.
defaultValue
(optional) any A value to be return in case that source
it's null or undefined or the no result was found using the provided path
Examples
Most used examples:
const { safeUse } = require('youneedme');
const object = {
key1: 'Value',
key2: {
id: 7,
},
key3: [
{ name: 'John' },
{ name: 'Doe' },
]
};
safeUse(object, 'key1'); // => 'Value'
safeUse(object, 'key2.id'); // => '7'
safeUse(object, 'key3.0.name'); // => 'John'
safeUse(object, 'non.existent.key', 'Not Found'); // => 'Not Found'
Why youneedme?
Things will change, have faith!
- Good reason 1
- Good reason 2
- Good reason 3. You didn't expect that! 🤯
WIP, BRB