-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
48 lines (39 loc) · 922 Bytes
/
objects.js
File metadata and controls
48 lines (39 loc) · 922 Bytes
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
"use strict"
import { substitute } from './strings'
export function isEmpty (obj) {
// null and undefined are "empty"
if (obj === null || obj === undefined) {
return true
}
if (typeof obj === 'number' && isNaN(obj)) {
return true
}
if (obj.length !== undefined) {
return obj.length === 0
}
if (obj instanceof Date) {
return false
}
if (typeof obj === 'object') {
return Object.keys(obj).length === 0
}
return false
}
export function forEach (obj, fn, context) {
Object.keys(obj).forEach(key => fn.call(context, obj[key], key))
}
export function toTextValue (arr, textTpl='{text}', valueTpl='{id}') {
if (!arr) {
return []
}
arr = arr.map(function (s) {
if (typeof s !== 'object') {
return { $text: s, $value: s }
} else {
s.$text = substitute(textTpl, s)
s.$value = substitute(valueTpl, s)
return s
}
})
return arr
}