-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.ts
58 lines (52 loc) · 1.49 KB
/
utils.ts
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
/**
* Generate a unique universal id
* @returns - unique universal id generated
*/
export const generateUUID = (): string => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0,
v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16).toUpperCase()
})
}
/**
* Turns a string into a camel case string
* @param string - string to transform
* @returns - camel case string created
*/
export const toCamelCase = (string: string): string => {
return string
.replace(/(?:^\w|[A-Z]|\b\w)/g, (ltr, idx) => (idx === 0 ? ltr.toLowerCase() : ltr.toUpperCase()))
.replace(/\s+/g, '')
}
/**
* Turns a string into a kebab case string
* @param string - string to transform
* @returns - kebab case string created
*/
export const toKebabCase = (string: string): string => {
const camelCase = toCamelCase(string)
return camelCase.charAt(0).toUpperCase() + camelCase.slice(1)
}
let warningThrown = 0
/**
* Throw a console warning with the passed arguments
* @param warning - warning to be thrown
*/
export const throwWarning = (warning: string) => {
if (warningThrown > 100) {
return
} else if (warningThrown === 100) {
console.warn('GPUCurtains: too many warnings thrown, stop logging.')
} else {
console.warn(warning)
}
warningThrown++
}
/**
* Throw a javascript error with the passed arguments
* @param error - error to be thrown
*/
export const throwError = (error: string) => {
throw new Error(error)
}