sanife
v0.1.14
Published
Swiss Army Knife
Downloads
10
Readme
Sanife -- Swiss Army Knife
Slight and Elegance Function Utils.
Usage
# install
$ npm install sanife
// ES6 Module
import * as _ from 'sanife'
// COMMONJS
const _ = require('sanife')
Util List
get
lodash like get
but slighter and powerful
const foo = { bar: 233 }
_.get(foo, 'bar') // 233
// more example
const apple = { color: [{ txt: 'red', rgb: '#00ffff' }] }
_.get(apple, 'color[0].rgb') // #00ffff
const people = [{ name: 'tom', age: 18 }]
_.get(tom, '0.name') // tom
_.get(tom, '[0]name') // tom
set
lodash like set
but slighter and powerful
const item = {}
_.set(item, 'class[0].names[0]', 'oops') // class: [{ names: ['oops'] }]
pick
lodash like pick
but slighter and powerful
const item = {
a: 'a',
b: {
c: 'c',
},
e: [{ f: 'f' }],
}
_.pick(item, ['a', 'b.c']) // {a: 'a', c: 'c'}
_.pick(item, ['e[0].f']) // {f: 'f'}
contains
Detect whether:
- array contains spec item
- object contains spec key
const list = [1, 2, 3]
_.contains(list, 2) // true
_.contains(list, 4) // false
const item = { name: 'tom', age: 22 }
_.contains(item, 'name') // true
remove
Remove
- item from array
- subString from string
_.remove([1, 2, 3], 2) // [1, 3]
_.remove('12321474312', ['2', '3']) // 114741
Base
first
const list = [1, 2, 3]
_.fist(list) // 1
last
const list = [1, 2, 3]
_.last(list) // 3
noop
_.noop() // do nothing
hasOwnProperty
Shortcut for Object.prototype.hasOwnProperty
isType
All kind of slight type detection