-
Notifications
You must be signed in to change notification settings - Fork 30
/
helpers.js
44 lines (39 loc) · 1.21 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
export const
ObjectProto = Object.prototype,
bettertypeof = x => ObjectProto.toString.call(x).match(/\s([a-zA-Z]+)/)[1],
getProto = Object.getPrototypeOf,
setProto = Object.setPrototypeOf,
has = (o, prop) => ObjectProto.hasOwnProperty.call(o, prop),
is = (Constructor, obj) => obj instanceof Constructor,
isFunction = f => typeof f === "function",
isObject = o => o && typeof o === "object",
isString = s => typeof s === "string",
isPlainObject = o => isObject(o) && getProto(o) === ObjectProto,
isIterable = x => x && isFunction(x[Symbol.iterator]),
proxify = (val, traps) => new Proxy(val, traps),
merge = (target, src = {}) => {
for (let key in src) {
if (isPlainObject(src[key])) {
const o = {}
merge(o, target[key])
merge(o, src[key])
target[key] = o
} else {
target[key] = src[key]
}
}
return target
},
define = (obj, key, value, enumerable = false) => {
Object.defineProperty(obj, key, { value, enumerable, writable: true, configurable: true })
},
extend = (child, parent, props) => {
child.prototype = Object.assign(Object.create(parent.prototype, {
constructor: {
value: child,
writable: true,
configurable: true
}
}), props)
setProto(child, parent)
}