-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
499 lines (429 loc) · 10.5 KB
/
utils.js
File metadata and controls
499 lines (429 loc) · 10.5 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// dependencies
// import axios from 'axios';
import { htmlEscape } from 'escape-goat'
// Extended JSON Parse
export const JSONParse = (str, defaultVal = []) => {
try {
if (str != null) {
return JSON.parse(str)
} else {
return defaultVal
}
} catch (e) {
return defaultVal
}
}
// Parse encoded params to object form
export const parseParams = (str) => {
const pieces = str.split('&')
const data = {}
let parts = []
// process each query pair
for (let i = 0; i < pieces.length; i++) {
parts = pieces[i].split('=')
if (parts.length < 2) {
parts.push('')
}
data[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1])
}
return data
}
// get surrounding elements of element in an array
export const getSurroundingElement = (array, element) => {
const minIndex = 0
const maxIndex = array.length - 1
const elementIndex = array.indexOf(element)
let previousElement = null
let nextElement = null
if (elementIndex !== -1) {
if (elementIndex > minIndex) {
previousElement = elementIndex - 1
}
if (elementIndex < maxIndex) {
nextElement = elementIndex + 1
}
}
return {
previousElement: array[previousElement],
nextElement: array[nextElement]
}
}
// copy text
export const copyText = (text) => {
if (process.client) {
const inputField = document.createElement('input')
document.body.appendChild(inputField)
inputField.style.visibility = 'hidden'
inputField.value = text
// Select the text field
inputField.select()
inputField.setSelectionRange(0, 99999) // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(inputField.value)
}
}
// Create a Formdata from an object
export const generateFormDataFromObject = (obj) => {
const formData = new FormData()
const keys = Object.keys(obj)
if (keys.length) {
keys.forEach((key) => {
formData.append(key, obj[key])
})
}
return formData
}
// DeAssociate Object/Array
export const deAssociate = (obj) => {
return obj ? JSON.parse(JSON.stringify(obj)) : {}
}
export const removeEmptyKeys = (obj) => {
if (obj) {
return Object.fromEntries(
Object.entries(obj).filter(([_, v]) => v != null && v !== '')
)
} else {
return obj
}
}
// Intervals
export const interval = (callback, time) => setInterval(callback, time)
/**
* Check Empty Object or array
* @param objectOrArry obj or array
* @returns boolean
*/
export const isEmpty = (objOrArr) => {
if (isObject(objOrArr)) {
return Object.keys(objOrArr).length === 0
} else if (Array.isArray(objOrArr)) {
return objOrArr.length === 0
} else {
return true
}
}
// Check isNotEmpty Object
export const isNotEmpty = (obj) => {
return !isEmpty(obj)
}
// Check if Object
export const isObject = (obj) => {
return typeof obj === 'object' && obj !== null
}
// Convert String to Number
export const parseNumber = (obj) => {
const res = {}
Object.keys(obj).forEach((key) => {
if (obj[key]) {
res[key] = !isNaN(obj[key]) ? Number(obj[key]) : obj[key]
}
})
return res
}
// Rename Object Key
export const renameKey = (obj, oldKey, newKey) => {
if (oldKey !== newKey && Object.hasOwnProperty.call(obj, oldKey)) {
Object.defineProperty(
obj,
newKey,
Object.getOwnPropertyDescriptor(obj, oldKey)
)
delete obj[oldKey]
}
}
// Rename Object Keys (Array)
export const renameKeys = (obj, oldKeys, newKeys) => {
if (oldKeys.length === newKeys.length) {
for (let i = 0; i < oldKeys.length; i++) {
renameKey(obj, oldKeys[i], newKeys[i])
}
}
}
// Remove Object Keys
export const removeKeys = (obj, keys) => {
if (obj) {
keys.forEach((key) => {
if (Object.hasOwnProperty.call(obj, key)) {
delete obj[key]
}
})
return obj
}
}
// Reserve Object Keys
export const reserveKeys = (obj, keys) => {
const _newObj = {}
keys.forEach((key) => {
if (Object.hasOwnProperty.call(obj, key)) {
_newObj[key] = obj[key]
}
})
return _newObj
}
// Querylize
export const querylize = (obj) => {
const str = []
for (const prop in obj) {
if (Object.getOwnPropertyDescriptor(obj, prop)) {
if (obj[prop]) {
str.push(encodeURIComponent(prop) + '=' + encodeURIComponent(obj[prop]))
}
}
}
return '?' + str.join('&')
}
// Handle error response
export const errMessage = (errorObject) => {
if (process.client) {
if (errorObject.response) {
if (errorObject.response.data.errors) {
return errorObject.response.data.errors.join('\n')
} else {
return errorObject.response.data.message
}
} else {
return errorObject
}
}
}
// Validate email for edge cases
export const isEmail = (emailStr) => {
return /\S+@\S+\.\S+/.test(emailStr)
}
// Generate AlphaNum (Payment Refs)
export const generateAlphaNum = (_length) => {
if (typeof _length === 'string') {
_length = _length.length;
}
const length = _length || 10;
const possible =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const text = Array.from({ length }, () =>
possible.charAt(Math.floor(Math.random() * possible.length))
).join('');
return text;
};
// SSR LocalStorage
export const ssrLocalStorage = {
getItem (key) {
if (process.client) {
return localStorage.getItem(key)
}
},
setItem (key, value) {
if (process.client) {
localStorage.setItem(key, value)
}
},
removeItem (key) {
if (process.client) {
localStorage.removeItem(key)
}
},
clear () {
if (process.client) {
localStorage.clear()
}
}
}
// Append link with https
export const withHttp = (url, {
https = true
} = {}) => {
if (typeof url !== 'string') {
throw new TypeError(
`Expected \`url\` to be of type \`string\`, got \`${typeof url}\``
)
}
url = url.trim()
if (url.includes('http://') || url.includes('https://')) {
return url
} else if (url.substring(0, 9) === 'localhost') {
return 'http://' + url
} else {
return 'https://' + url
}
}
// NullSafe while in template
export const nullSafe = (obj, key) => {
if (obj) {
return obj[key]
} else {
return ''
}
}
// ConsoleLog while in template
export const consoleLog = (key, value = null) => {
if (process.client) {
if (value) {
window.console.log(key, value)
} else {
window.console.log(key)
}
}
}
// Validate url
export const isValidURL = (url) => {
let urlInstance
try {
urlInstance = new URL(url)
} catch (_) {
return false
}
return urlInstance.protocol === 'http:' || urlInstance.protocol === 'https:'
}
export const shuffleArray = (arr) => {
for (let i = 0; i < arr.length - 1; i++) {
const j = i + Math.floor(Math.random() * (arr.length - i))
const temp = arr[j]
arr[j] = arr[i]
arr[i] = temp
}
return arr
}
// // Handle error response
// export const formatError = (errObj, isHtml = false) => {
// const validationErrors = errObj.response ?.data ?.errors // Laravel
// const compositeErrors = []
// if (errObj.response) {
// if (validationErrors) {
// for (const key in validationErrors) {
// if (Object.hasOwnProperty.call(validationErrors, key)) {
// compositeErrors.push(...validationErrors[key])
// }
// }
// return compositeErrors.join(isHtml ? '<br>' : '\n')
// } else {
// return errObj.response.data.message
// }
// } else {
// return errObj
// }
// }
export const getFileSize = (x) => {
let l = 0
let n = parseInt(x, 10) || 0
while (n >= 1024 && ++l) {
n = n / 1024
}
return n.toFixed(n < 10 && l > 0 ? 1 : 0)
}
export const rowNumber = (row, perPage, page) => {
return (page - 1) * perPage + row
}
// export const postAction = ($axios, url, payload) => {
// return axios.post(url, payload, {
// params: payload?.params
// })
// }
// export const putAction = ($axios, url, payload) => {
// return axios.put(url, payload, {
// params: payload?.params
// })
// }
// export const getAction = ($axios, url, payload) => {
// return axios.get(url, {
// params: payload
// })
// }
export const pushUniqueValue = (array, value, key = null) => {
const found = array.find((item) => {
const a = key ? item[key] : item
const b = key ? value[key] : value
return a.toLowerCase() === b.toLowerCase()
})
if (!found) {
array.push(value)
}
}
// export const deleteAction = ($axios, url) => {
// return axios.delete(url)
// }
export const stringIncludes = (str, strArr) => {
return strArr
.map((strArrWord) => strArrWord.toLowerCase())
.some((strArrWord) => str.toLowerCase().includes(strArrWord))
}
let timer
export const snooze = (func, timeout = 2000) => {
clearTimeout(timer)
timer = setTimeout(() => {
func.apply()
}, timeout)
}
export const nullSafeSize = (obj) => {
if (obj) {
return obj.length
} else {
return 0
}
}
export const disableBodyScroll = () => {
const body = document.querySelector('body')
body.style.overflowY = 'hidden'
body.style.height = '100vh'
}
export const enableBodyScroll = () => {
const body = document.querySelector('body')
body.style.overflowY = 'visible'
body.style.height = 'auto'
}
// debounce
export function debounce(fn, wait) {
let timer
return function (...args) {
if (timer) {
clearTimeout(timer)
}
const context = this // get the current context
timer = setTimeout(() => {
fn.apply(context, args)
}, wait)
}
}
export const normalizeUrl = (urlString) => {
const parsedUrl = new URL(urlString)
if (!parsedUrl.host || !parsedUrl.protocol) {
return urlString
} else {
return (
parsedUrl.origin +
parsedUrl.pathname.replace(/\/+/g, '/').replace(/\/$/, '')
)
}
}
// 404 error
export const notFoundError = () => {
return {
statusCode: 404,
message: 'This page could not be found'
}
}
export const sortByKeyLength = (obj) => {
Object.keys(obj)
.map(function (k) {
return { key: k, value: obj[k] }
})
.sort(function (a, b) {
return b.value.length - a.value.length
})
}
// Strips a number number input
export const cleanNumber = (value, min = null) => {
value = isNaN(value) ? 0 : value
if (min) {
value = Math.max(min, value)
}
return value
}
export const scrollToTop = () => window.scrollTo(0, 0)
export const decodeEntity = (htmlStr) => {
return htmlEscape(htmlStr)
}
export const kebabToTitle = (str) => {
return str
.split('-')
.map((w) => w[0].toUpperCase() + w.substring(1).toLowerCase())
.join(' ')
}
generateAlphaNum(10)