forked from sebastianhader/bucks-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
319 lines (279 loc) · 6.83 KB
/
index.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* Checks if value is int.
* @param value
* @returns {boolean}
*/
let isInt = (value) => {
return !isNaN(value) && parseInt(Number(value)) == value && !isNaN(parseInt(value, 10))
}
/**
* Checks if value is string.
* @param value
* @return {boolean}
*/
let isString = (value) => {
return !!(typeof value === 'string' || value instanceof String)
}
/**
* Generates a random string.
* @param length
* @returns string
*/
let randomString = (length = 10) => {
let text = ''
let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (let i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
return text
}
/**
* Generates a random int.
* @param max
* @return {number}
*/
let randomInt = (max) => {
return Math.floor(Math.random() * Math.floor(max))
}
/**
* Copies input or text area value to cache.
* Element is here a dom tree element (document.getElementById)
* @param element
*/
let copyToCache = (element) => {
try {
element.select()
let successful = document.execCommand('copy')
return !!successful
} catch (err) {
return false
}
}
/**
* Copies area value to the users clipboard.
* @param text
*/
let copyToClipboard = (text) => {
let textArea = document.createElement('textarea')
textArea.style.position = 'fixed'
textArea.style.top = 0
textArea.style.left = 0
textArea.style.width = '2em'
textArea.style.height = '2em'
textArea.style.padding = 0
textArea.style.border = 'none'
textArea.style.outline = 'none'
textArea.style.boxShadow = 'none'
textArea.style.background = 'transparent'
// set Value
textArea.value = text
document.body.appendChild(textArea)
let success = copyToCache(textArea)
document.body.removeChild(textArea)
return success
}
/**
* Converts a camel case string to a string with spaces
* and uppercase starting letters.
* @param word
* @returns string
*/
let convertCamelCaseToDash = (word) => {
if (word) {
return word.replace(/([A-Z])/g, ' $1')
.replace(/^./, function(str) {
return str.toUpperCase()
})
}
}
/**
* Converts a string to a capitalized string.
* @param word
* @returns string
*/
let capitalize = (word) => {
if (typeof word !== 'string') return ''
return word.charAt(0).toUpperCase() + word.slice(1)
}
/**
* Assigns multiple objects to one object.
* @param args
* @return {*|{}}
*/
let objectAssign = (...args) => {
return args.reduce(function (r, o) {
Object.keys(o).forEach(function (k) {
r[k] = o[k]
})
return r
}, {})
}
/**
* Filter object (removes all keys with value null).
* @param o
* @return {*|{}}
*/
let objectFilter = (o) => {
let result = {}
Object.keys(o).forEach(function (k) {
if (o[k]) result[k] = o[k]
})
return result
}
/**
* Checks if given object is empty.
* @param o
* @return {*|{}}
*/
let objectEmpty = (o) => {
return !!(Object.keys(o).length === 0 && o.constructor === Object)
}
/**
* Clones object without reference.
* @param o
* @return {*|{}}
*/
let clone = (o) => {
return JSON.parse(JSON.stringify(o))
}
/**
* Reverts all arrays in a two dimensional array.
* @param array2D
* @return {*|{}}
*/
let reverse2DArrays = (array2D) => {
let new2DArray = []
array2D.forEach((smallArray) => {
new2DArray.push(smallArray.reverse())
})
return new2DArray
}
/**
* Flat two dimensional array to one array.
* @param array2D
* @return {*|{}}
*/
let flat2DArray = (array2D) => {
let newArray = []
array2D.forEach((array) => {
array.forEach((item) => {
newArray.push(item)
})
})
return newArray
}
/**
* Calculates the distance between two coordinates (km).
* @param lng1
* @param lat1
* @param lng2
* @param lat2
* @return {*}
*/
let distanceBetweenTwoCoordinates = (lng1, lat1, lng2, lat2) => {
let R = 6371 // km (change this constant to get miles)
let dLat = (lat2 - lat1) * Math.PI / 180
let dLon = (lng2 - lng1) * Math.PI / 180
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
return R * c
}
/**
* Builds url GET query parameter (url encoded) for object and removes
* empty keys.
* @param o
*/
let buildUrlQueryParameter = (o) => {
let query = ''
Object.keys(o).forEach((key) => {
if (Array.isArray(o[key])) {
// if value is an array
o[key].forEach((arrayValue) => {
if (arrayValue) query += '&' + key + '[]=' + encodeURIComponent(arrayValue)
})
} else {
// if value not an array
if (o[key]) query += '&' + key + '=' + encodeURIComponent(o[key])
}
})
// remove first "&" sign
if (query.length > 0) query = query.substr(1)
return query
}
/**
* Checks if text includes one of the words in array words.
* @param text
* @param words
* @param lowerCase
*/
let includeStrings = (text, words, lowerCase = false) => {
if (lowerCase) text = text.toLowerCase()
let wordFound = false
words.forEach((word) => {
if (lowerCase) word = word.toLowerCase()
if (text.indexOf(word) > -1) wordFound = true
})
return wordFound
}
/**
* Debounce function for given milli seconds.
* @param func
* @param wait
* @param immediate
* @return {Function}
*/
let debounce = (func, wait, immediate) => {
let timeout
return function() {
let context = this, args = arguments
let later = function() {
timeout = null
if (!immediate) func.apply(context, args)
}
let callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
}
/**
* Gets key paths from objects. Path example {key}.{key}.
* @param object
* @param path
* @param defaultValue
* @return mixed
*/
let get = (object, path, defaultValue) => {
let result = undefined
let keys = (path && path !== '') ? path.split('.') : []
if (object !== null && keys.length > 0) {
result = object
for (let key of keys) {
result = (result && result[key]) ? result[key] : undefined
}
}
return result === undefined ? defaultValue : result
}
export default {
isInt,
isString,
randomString,
randomInt,
copyToCache,
copyToClipboard,
convertCamelCaseToDash,
capitalize,
objectAssign,
objectFilter,
objectEmpty,
clone,
reverse2DArrays,
flat2DArray,
distanceBetweenTwoCoordinates,
buildUrlQueryParameter,
includeStrings,
debounce,
get
}