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 | let i = string.length
|
114 | let j = substring.length
|
115 |
|
116 | if (j > i)
|
117 | {
|
118 | return false
|
119 | }
|
120 |
|
121 | while (j > 0)
|
122 | {
|
123 | i--
|
124 | j--
|
125 |
|
126 | if (string[i] !== substring[j])
|
127 | {
|
128 | return false
|
129 | }
|
130 | }
|
131 |
|
132 | return true
|
133 |
|
134 |
|
135 |
|
136 | }
|
137 |
|
138 | export function is_empty(array)
|
139 | {
|
140 | return array.length === 0
|
141 | }
|
142 |
|
143 | export function not_empty(array)
|
144 | {
|
145 | return array.length > 0
|
146 | }
|
147 |
|
148 |
|
149 | export function repeat(what, times)
|
150 | {
|
151 | let result = ''
|
152 | while (times > 0)
|
153 | {
|
154 | result += what
|
155 | times--
|
156 | }
|
157 | return result
|
158 | }
|
159 |
|
160 |
|
161 | export function is_blank(text)
|
162 | {
|
163 | return !exists(text) || !text.replace(/\s/g, '')
|
164 | }
|
165 |
|
166 |
|
167 | export function zip(a, b)
|
168 | {
|
169 | return a.map(function(_, index)
|
170 | {
|
171 | return [a[index], b[index]]
|
172 | })
|
173 | } |
\ | No newline at end of file |