diff options
Diffstat (limited to 'client/src/app/helpers/utils/object.ts')
-rw-r--r-- | client/src/app/helpers/utils/object.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/client/src/app/helpers/utils/object.ts b/client/src/app/helpers/utils/object.ts new file mode 100644 index 000000000..1ca4a23ac --- /dev/null +++ b/client/src/app/helpers/utils/object.ts | |||
@@ -0,0 +1,47 @@ | |||
1 | function immutableAssign <A, B> (target: A, source: B) { | ||
2 | return Object.assign({}, target, source) | ||
3 | } | ||
4 | |||
5 | function removeElementFromArray <T> (arr: T[], elem: T) { | ||
6 | const index = arr.indexOf(elem) | ||
7 | if (index !== -1) arr.splice(index, 1) | ||
8 | } | ||
9 | |||
10 | function sortBy (obj: any[], key1: string, key2?: string) { | ||
11 | return obj.sort((a, b) => { | ||
12 | const elem1 = key2 ? a[key1][key2] : a[key1] | ||
13 | const elem2 = key2 ? b[key1][key2] : b[key1] | ||
14 | |||
15 | if (elem1 < elem2) return -1 | ||
16 | if (elem1 === elem2) return 0 | ||
17 | return 1 | ||
18 | }) | ||
19 | } | ||
20 | |||
21 | function intoArray (value: any) { | ||
22 | if (!value) return undefined | ||
23 | if (Array.isArray(value)) return value | ||
24 | |||
25 | if (typeof value === 'string') return value.split(',') | ||
26 | |||
27 | return [ value ] | ||
28 | } | ||
29 | |||
30 | function toBoolean (value: any) { | ||
31 | if (!value) return undefined | ||
32 | |||
33 | if (typeof value === 'boolean') return value | ||
34 | |||
35 | if (value === 'true') return true | ||
36 | if (value === 'false') return false | ||
37 | |||
38 | return undefined | ||
39 | } | ||
40 | |||
41 | export { | ||
42 | sortBy, | ||
43 | immutableAssign, | ||
44 | removeElementFromArray, | ||
45 | intoArray, | ||
46 | toBoolean | ||
47 | } | ||