js functions, just import needed functions.
js 方法库, 按需使用
// install
npm install helper-js -S
import {function1, function2} from 'helper-js'
typeof v !== 'undefined'
Object.prototype.toString.call(v) === '[object Array]'
(10, 3) => '010', (2, 4) => '0002'
(1, 10) => 10, (11, 10) => 11
(1, 10) => 1, (11, 10) => 10
hello => Hello
helloWorld nice => hello-world-nice
helloWorld nice => hello_world_nice
helloWorld nice => helloWorldNice
helloWorldNice => hello World nice
helloWorldNice => Hello World Nice
({'1': '2', '3': '4'}, 'abcd1234') => 'abcd2244'
handler(item, index/key)
- opt {reverse}
({a:{b:1}}, 'a.b') => 1
mapObjectTree(obj, handler, limit=10000) // can clone whole object tree, can traverse every node, can exclude nodes
return cloned obj
handler(value, key, parent)
handler can return null or an object.
null: don't change anything
object{
key: false, // delete. Deprecated, this will be removed in future, please use `delete` instead of it.
key: new key, // use a new key instead of old key. if key == null, the old key will be detected
delete,
value, // new value. if value not gived, the old value will be detected
skip, // skip children
stop,
}
{key: false}: delete
{value}: change value
{key, value}. change key and value
limit: to prevent circular reference.
idKey(item, i)
eg: mapObjects([{id: 1}, {id:2}], 'id') -> {'1':{id:1},'2':{id:2}}
eg: mapObjects([{id: 1}, {id:2}], (item, i) => item.id + 100 ) -> {'101':{id:1},'102':{id:2}}
handler(node, index, parent, path)
// path: [0, 1]
return 'skip children', 'skip siblings', false // retrun false to stop
childrenKey = 'children'
data
constructor(data)
get rootChildren()
* iteratePath(path, opt = {})
opt.reverse // bool
getNode(path)
getNodeIndexAndParent(path)
return {parent, parentPath, index}
getNodeParent(path)
setPathNode(path, node)
removeNode(path)
walk(handler, opt={})
handler(node, index, parent, path)
clone(opt={})
opt.afterNodeCreated(newNode, {oldNode: node, index, parent, path})
resolveValueOrGettter(valueOrGetter, args = []) // return first arg directly if is not function, else return function result
watchChange (getVal, handler) // wrap getter function, when use getter to get value, it will check value if changed
debounce(action, wait = 0, opt = {}) // return newAction, newAction's scope is same with action, its arguments will be passed to action. newAction.stop: function, to stop timeout
opt: {
immediate: false,
}
promiseTimeout(promise, timeout) // timeout unit is millisecond. return a new promise, which throw error named 'timeout' if timeout
getPositionFromOffset (el, offset) // get position of a el if its offset is given. like jQuery.offset.
getBoundingClientRect (el) // get size and position relative to viewport, return {top, right, bottom, left, width, height, x, y}
opt: {
noCache: false,
}
// type: year, month, day, hour, minute, second, millisecond
addDate(now, 1, 'day')
addDate(now, -1, 'hour')
// callback(mid, i) should return mid - your_value
types eg: ['Object', (i) => i > 3, ['Number', default] ]
resolveArgsByType([1,'str'], ['Number', 'Boolean' ,'String']) -> [1, null, 'str']
resolveArgsByType([1,'str'], ['Number', ['Boolean', true] ,'String']) -> [1, true, 'str']
check source code to learn more
makeStorageHelper(storage) // make localStorage or sessionStorage can use json as value and has expiry
const cwep = new CrossWindowEventProcessor()
// CrossWindowEventProcessor has a default name; communicate in same-name instances.
// don't create more than one same-name instances in one window
// set different name if there are more than one same-name instances
cwep.storageName = '_crossWindow_channel2'
// functions
// on, once, off: same to EventProcessor
// emitTo(name, targets, ...args)
// emitLocal(name, ...args) emit to current window
// broadcast(name, ...args) emit to other windows
// emit(name, ...args) emit to all windows