-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.js
More file actions
41 lines (36 loc) · 933 Bytes
/
strings.js
File metadata and controls
41 lines (36 loc) · 933 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
"use strict"
let uid = Date.now()
export function nextUid() {
return (uid++).toString(36)
}
export function format() {
let args = [].slice.call(arguments),
str = args.shift()
return str.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] !== undefined
? args[number]
: match
})
}
export function substitute(str, obj) {
return str.replace((/\\?\{([^{}]+)\}/g), function(match, name){
if (match.charAt(0) === '\\') {
return match.slice(1)
}
return (obj[name] !== null) ? obj[name] : ''
})
}
export function toArray(value, sep) {
if (value === null || value === undefined) {
value = []
}
if (typeof value === 'string' && sep) {
value = value.split(sep)
} else if (!(value instanceof Array)) {
value = [value]
} else if (sep) {
// if use sep, convert every value to string
value = value.map(v=>v.toString())
}
return value
}