-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
48 lines (33 loc) · 965 Bytes
/
util.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
45
46
47
48
const {keys} = Object
const toString = val => Object.prototype.toString.call(val)
const getType = val => toString(val).slice(8, -1).toLowerCase()
const isString = val => typeof val === "string" || getType(val) === "string"
// Based ob lodash/isPlainObject
function isPlainObject(val) {
if (getType(val) !== "object") {
return false
}
const pp = Object.getPrototypeOf(val)
if (pp === null || pp === void 0) {
return true
}
const Ctor = pp.constructor && pp.constructor.toString()
return Ctor === Object.toString()
}
function isArrayOf(arr, predicate, ctx = null) {
for (const [key, val] of arr.entries()) {
if (predicate.call(ctx, val, key, arr) === false) {
return false
}
}
return true
}
function map(obj, fn) {
const res = {}
for (const key of keys(obj)) {
const val = obj[key]
res[key] = fn(val, key, obj)
}
return res
}
module.exports = {isArrayOf, getType, isString, isPlainObject, map}