-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (60 loc) · 1.6 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
/* eslint-disable no-magic-numbers */
const pluralize = word => {
word = word.toLowerCase().trim();
if (word.length >= 2 && VOWELS.includes(word[word.length - 2])) {
return `${word}s`;
}
if (word.endsWith('s') || word.endsWith('sh') || word.endsWith('ch') || word.endsWith('x') || word.endsWith('z')) {
return `${word}es`;
}
if (word.endsWith('y')) {
return `${word.slice(0, -1)}ies`;
}
return `${word}s`;
};
module.exports = {
pluralize,
quantify: (count = 1, word) => `${count.toLocaleString()} ${count === 1 ? word : pluralize(word)}`,
prune: (string = '', length = 0) => `${string}`.trim().substring(0, length).replace(/(<([^>]+)>)/gi, ''),
capitalizeSlug: source => (
source
.split('-')
.map(word => (
word.replace(
word.charAt(0),
word.charAt(0).toUpperCase()
))
)
.join(' ')
),
toSlug: source => (
source.toLowerCase().trim().replace(/ /, '-')
),
toDollar: number => {
const dollar = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
return dollar.format(number);
},
toSingular: source => {
let result = source;
if (result.substr(-3) === 'ies') {
result = `${result.slice(0, -3)}y`;
}
if (result.substr(-1) === 's') {
result = result.slice(0, -1);
}
return result;
},
generateUUID: () => {
const g4 = () => {
return (((1 + Math.random()) * 0x10000) | 0)
.toString(16)
.substring(1);
};
return (
`${g4()}${g4()}-${g4()}-${g4()}-${g4()}-${g4()}${g4()}${g4()}`
);
}
};