1 |
|
2 | export const exists = what => typeof what !== 'undefined'
|
3 |
|
4 |
|
5 | const object_constructor = {}.constructor
|
6 |
|
7 |
|
8 | export function is_object(object)
|
9 | {
|
10 | return exists(object) && (object !== null) && object.constructor === object_constructor
|
11 | }
|
12 |
|
13 |
|
14 |
|
15 | export function extend(...objects)
|
16 | {
|
17 | const to = objects[0]
|
18 | const from = objects[1]
|
19 |
|
20 | if (objects.length > 2)
|
21 | {
|
22 | const last = objects.pop()
|
23 | const intermediary_result = extend.apply(this, objects)
|
24 | return extend(intermediary_result, last)
|
25 | }
|
26 |
|
27 | for (let key of Object.keys(from))
|
28 | {
|
29 | if (is_object(from[key]))
|
30 | {
|
31 | if (!is_object(to[key]))
|
32 | {
|
33 | to[key] = {}
|
34 | }
|
35 |
|
36 | extend(to[key], from[key])
|
37 | }
|
38 | else
|
39 | {
|
40 | to[key] = from[key]
|
41 | }
|
42 | }
|
43 |
|
44 | return to
|
45 | }
|
46 |
|
47 | export function merge()
|
48 | {
|
49 | const parameters = Array.prototype.slice.call(arguments, 0)
|
50 | parameters.unshift({})
|
51 | return extend.apply(this, parameters)
|
52 | }
|
53 |
|
54 | export function clone(object)
|
55 | {
|
56 | return merge({}, object)
|
57 | }
|
58 |
|
59 |
|
60 | export function convert_from_camel_case(object)
|
61 | {
|
62 | for (let key of Object.keys(object))
|
63 | {
|
64 | if (/[A-Z]/.test(key))
|
65 |
|
66 | {
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | const lo_dashed_key = key.replace(/([A-Z])/g, function(match, group_1)
|
79 | {
|
80 | return '_' + group_1.toLowerCase()
|
81 | })
|
82 |
|
83 | if (!exists(object[lo_dashed_key]))
|
84 | {
|
85 | object[lo_dashed_key] = object[key]
|
86 | delete object[key]
|
87 | }
|
88 | }
|
89 | }
|
90 |
|
91 | return object
|
92 | }
|
93 |
|
94 | function escape_regexp(string)
|
95 | {
|
96 | const specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", 'g')
|
97 | return string.replace(specials, "\\$&")
|
98 | }
|
99 |
|
100 | export function replace_all(where, what, with_what)
|
101 | {
|
102 | const regexp = new RegExp(escape_regexp(what), 'g')
|
103 | return where.replace(regexp, with_what)
|
104 | }
|
105 |
|
106 | export function starts_with(string, substring)
|
107 | {
|
108 | return string.indexOf(substring) === 0
|
109 | }
|
110 |
|
111 | export function ends_with(string, substring)
|
112 | {
|
113 | const index = string.lastIndexOf(substring)
|
114 | return index >= 0 && index === string.length - substring.length
|
115 | } |
\ | No newline at end of file |