@bccampus/utils 0.2.3 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -8
- package/dist/array/chunk.cjs +13 -5
- package/dist/array/chunk.d.ts +7 -0
- package/dist/array/chunk.js +14 -7
- package/dist/array/chunk.test.d.ts +1 -0
- package/dist/date/iso-date-time/constants.cjs +27 -0
- package/dist/date/iso-date-time/constants.d.ts +9 -0
- package/dist/date/iso-date-time/constants.js +19 -0
- package/dist/date/iso-date-time/index.cjs +8 -0
- package/dist/date/iso-date-time/index.d.ts +5 -0
- package/dist/date/iso-date-time/index.js +3 -0
- package/dist/date/iso-date-time/iso-date-time.arithmetic.test.d.ts +1 -0
- package/dist/date/iso-date-time/iso-date-time.cjs +232 -0
- package/dist/date/{iso-date-time.d.ts → iso-date-time/iso-date-time.d.ts} +3 -32
- package/dist/date/iso-date-time/iso-date-time.format.test.d.ts +1 -0
- package/dist/date/iso-date-time/iso-date-time.js +231 -0
- package/dist/date/iso-date-time/iso-date-time.test.d.ts +1 -0
- package/dist/date/iso-date-time/types.cjs +0 -0
- package/dist/date/iso-date-time/types.d.ts +25 -0
- package/dist/date/iso-date-time/types.js +0 -0
- package/dist/duration/add-durations.cjs +5 -8
- package/dist/duration/add-durations.js +5 -9
- package/dist/duration/duration.test.d.ts +1 -0
- package/dist/duration/is-duration.cjs +4 -3
- package/dist/duration/is-duration.js +5 -5
- package/dist/duration/to-number.cjs +11 -10
- package/dist/duration/to-number.d.ts +1 -1
- package/dist/duration/to-number.js +11 -11
- package/dist/format/formatter.cjs +31 -31
- package/dist/format/formatter.d.ts +4 -4
- package/dist/format/formatter.js +32 -37
- package/dist/format/formatter.test.d.ts +1 -0
- package/dist/format/stringfy.test.d.ts +1 -0
- package/dist/format/stringify.cjs +34 -40
- package/dist/format/stringify.d.ts +1 -1
- package/dist/format/stringify.js +35 -42
- package/dist/format/template.cjs +25 -32
- package/dist/format/template.d.ts +3 -3
- package/dist/format/template.js +24 -33
- package/dist/format/template.test.d.ts +1 -0
- package/dist/index.cjs +0 -1
- package/dist/index.js +0 -1
- package/dist/object/get.cjs +5 -5
- package/dist/object/get.d.ts +1 -1
- package/dist/object/get.js +6 -7
- package/dist/object/get.test.d.ts +1 -0
- package/dist/object/is-object.cjs +5 -2
- package/dist/object/is-object.d.ts +1 -1
- package/dist/object/is-object.js +6 -4
- package/dist/object/is-object.test.d.ts +1 -0
- package/dist/object/omit.cjs +14 -16
- package/dist/object/omit.d.ts +1 -1
- package/dist/object/omit.js +15 -18
- package/dist/object/omit.test.d.ts +1 -0
- package/dist/object/pick.cjs +10 -9
- package/dist/object/pick.d.ts +1 -1
- package/dist/object/pick.js +9 -9
- package/dist/object/pick.test.d.ts +1 -0
- package/dist/object/set.cjs +13 -12
- package/dist/object/set.d.ts +1 -1
- package/dist/object/set.js +14 -14
- package/dist/object/set.test.d.ts +1 -0
- package/dist/path/file-url-to-path.cjs +10 -6
- package/dist/path/file-url-to-path.js +11 -8
- package/dist/path/file-url-to-path.test.d.ts +1 -0
- package/dist/path/normalize.cjs +41 -27
- package/dist/path/normalize.d.ts +8 -2
- package/dist/path/normalize.js +41 -28
- package/dist/path/normalize.test.d.ts +1 -0
- package/dist/set/difference.cjs +5 -6
- package/dist/set/difference.d.ts +1 -1
- package/dist/set/difference.js +6 -8
- package/dist/set/intersection.cjs +6 -9
- package/dist/set/intersection.d.ts +1 -1
- package/dist/set/intersection.js +7 -11
- package/dist/set/is-superset.cjs +5 -8
- package/dist/set/is-superset.d.ts +1 -1
- package/dist/set/is-superset.js +6 -10
- package/dist/set/set.test.d.ts +1 -0
- package/dist/set/symmetric-difference.cjs +6 -10
- package/dist/set/symmetric-difference.d.ts +1 -1
- package/dist/set/symmetric-difference.js +7 -12
- package/dist/set/union.cjs +5 -6
- package/dist/set/union.d.ts +1 -1
- package/dist/set/union.js +6 -8
- package/dist/string/capitalize.cjs +5 -2
- package/dist/string/capitalize.d.ts +1 -1
- package/dist/string/capitalize.js +6 -4
- package/dist/string/capitalize.test.d.ts +1 -0
- package/dist/string/normalize.cjs +7 -2
- package/dist/string/normalize.js +8 -4
- package/dist/string/normalize.test.d.ts +1 -0
- package/dist/string/pluralize/index.cjs +5 -4
- package/dist/string/pluralize/index.js +4 -5
- package/dist/string/pluralize/pluralize.cjs +162 -208
- package/dist/string/pluralize/pluralize.d.ts +4 -8
- package/dist/string/pluralize/pluralize.js +158 -205
- package/dist/string/pluralize/pluralize.test.d.ts +1 -0
- package/dist/string/pluralize/rules/index.cjs +8 -9
- package/dist/string/pluralize/rules/index.js +1 -6
- package/dist/string/pluralize/rules/irregular.cjs +55 -59
- package/dist/string/pluralize/rules/irregular.js +56 -61
- package/dist/string/pluralize/rules/pluralization.cjs +28 -36
- package/dist/string/pluralize/rules/pluralization.js +29 -38
- package/dist/string/pluralize/rules/singularization.cjs +27 -44
- package/dist/string/pluralize/rules/singularization.js +28 -46
- package/dist/string/pluralize/rules/uncountable.cjs +104 -110
- package/dist/string/pluralize/rules/uncountable.js +105 -112
- package/dist/string/pluralize/types.cjs +0 -1
- package/dist/string/pluralize/types.js +0 -1
- package/dist/string/slugify/index.cjs +2131 -2246
- package/dist/string/slugify/index.d.ts +1 -1
- package/dist/string/slugify/index.js +2132 -2248
- package/dist/string/slugify/slugify.test.d.ts +1 -0
- package/dist/string/strip-html-tags.cjs +4 -3
- package/dist/string/strip-html-tags.js +5 -5
- package/dist/string/strip-html-tags.test.d.ts +1 -0
- package/dist/string/to-int.cjs +14 -6
- package/dist/string/to-int.js +15 -8
- package/dist/string/to-int.test.d.ts +1 -0
- package/dist/string/to-sentence-case.cjs +6 -6
- package/dist/string/to-sentence-case.d.ts +1 -1
- package/dist/string/to-sentence-case.js +7 -8
- package/dist/string/to-sentence-case.test.d.ts +1 -0
- package/dist/string/trim.cjs +8 -7
- package/dist/string/trim.js +9 -9
- package/dist/string/trim.test.d.ts +1 -0
- package/dist/string/truncate.cjs +16 -15
- package/dist/string/truncate.js +17 -17
- package/dist/string/truncate.test.d.ts +1 -0
- package/dist/value/is-empty.cjs +30 -12
- package/dist/value/is-empty.d.ts +1 -1
- package/dist/value/is-empty.js +31 -14
- package/dist/value/value.test.d.ts +1 -0
- package/package.json +180 -69
- package/dist/array/index.cjs +0 -4
- package/dist/array/index.d.ts +0 -2
- package/dist/array/index.js +0 -4
- package/dist/date/index.cjs +0 -8
- package/dist/date/index.d.ts +0 -3
- package/dist/date/index.js +0 -8
- package/dist/date/iso-date-time.cjs +0 -256
- package/dist/date/iso-date-time.js +0 -256
- package/dist/duration/index.cjs +0 -8
- package/dist/duration/index.d.ts +0 -4
- package/dist/duration/index.js +0 -8
- package/dist/format/index.cjs +0 -13
- package/dist/format/index.d.ts +0 -5
- package/dist/format/index.js +0 -13
- package/dist/object/index.cjs +0 -12
- package/dist/object/index.d.ts +0 -6
- package/dist/object/index.js +0 -12
- package/dist/path/index.cjs +0 -6
- package/dist/path/index.d.ts +0 -4
- package/dist/path/index.js +0 -6
- package/dist/set/index.cjs +0 -12
- package/dist/set/index.d.ts +0 -6
- package/dist/set/index.js +0 -12
- package/dist/string/index.cjs +0 -14
- package/dist/string/index.d.ts +0 -8
- package/dist/string/index.js +0 -14
- package/dist/value/index.cjs +0 -4
- package/dist/value/index.d.ts +0 -2
- package/dist/value/index.js +0 -4
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/set/symmetric-difference.ts
|
|
3
3
|
function symmetricDifference(setA, setB) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} else {
|
|
9
|
-
_difference.add(elem);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return _difference;
|
|
4
|
+
const _difference = new Set(setA);
|
|
5
|
+
for (const elem of setB) if (_difference.has(elem)) _difference.delete(elem);
|
|
6
|
+
else _difference.add(elem);
|
|
7
|
+
return _difference;
|
|
13
8
|
}
|
|
9
|
+
//#endregion
|
|
14
10
|
exports.symmetricDifference = symmetricDifference;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function symmetricDifference(setA: Iterable<
|
|
1
|
+
export declare function symmetricDifference(setA: Iterable<unknown>, setB: Iterable<unknown>): Set<unknown>;
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
+
//#region src/set/symmetric-difference.ts
|
|
1
2
|
function symmetricDifference(setA, setB) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} else {
|
|
7
|
-
_difference.add(elem);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return _difference;
|
|
3
|
+
const _difference = new Set(setA);
|
|
4
|
+
for (const elem of setB) if (_difference.has(elem)) _difference.delete(elem);
|
|
5
|
+
else _difference.add(elem);
|
|
6
|
+
return _difference;
|
|
11
7
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
8
|
+
//#endregion
|
|
9
|
+
export { symmetricDifference };
|
package/dist/set/union.cjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/set/union.ts
|
|
3
3
|
function union(setA, setB) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
return _union;
|
|
4
|
+
const _union = new Set(setA);
|
|
5
|
+
for (const elem of setB) _union.add(elem);
|
|
6
|
+
return _union;
|
|
9
7
|
}
|
|
8
|
+
//#endregion
|
|
10
9
|
exports.union = union;
|
package/dist/set/union.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function union(setA: Iterable<
|
|
1
|
+
export declare function union(setA: Iterable<unknown>, setB: Iterable<unknown>): Set<unknown>;
|
package/dist/set/union.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
+
//#region src/set/union.ts
|
|
1
2
|
function union(setA, setB) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
return _union;
|
|
3
|
+
const _union = new Set(setA);
|
|
4
|
+
for (const elem of setB) _union.add(elem);
|
|
5
|
+
return _union;
|
|
7
6
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
7
|
+
//#endregion
|
|
8
|
+
export { union };
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
|
|
2
|
+
//#region src/string/capitalize.ts
|
|
3
|
+
function capitalize(s) {
|
|
4
|
+
return s[0].toUpperCase() + s.slice(1);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
4
7
|
exports.capitalize = capitalize;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare
|
|
1
|
+
export declare function capitalize<T extends string>(s: T): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/string/normalize.ts
|
|
3
|
+
/**
|
|
4
|
+
* Trims white spaces and
|
|
5
|
+
* replaces multiple white space characters with a single space
|
|
6
|
+
*/
|
|
3
7
|
function normalize(text) {
|
|
4
|
-
|
|
8
|
+
return text.trim().replace(/\s+/g, " ");
|
|
5
9
|
}
|
|
10
|
+
//#endregion
|
|
6
11
|
exports.normalize = normalize;
|
package/dist/string/normalize.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
//#region src/string/normalize.ts
|
|
2
|
+
/**
|
|
3
|
+
* Trims white spaces and
|
|
4
|
+
* replaces multiple white space characters with a single space
|
|
5
|
+
*/
|
|
1
6
|
function normalize(text) {
|
|
2
|
-
|
|
7
|
+
return text.trim().replace(/\s+/g, " ");
|
|
3
8
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
9
|
+
//#endregion
|
|
10
|
+
export { normalize };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
const require_string_pluralize_pluralize = require("./pluralize.cjs");
|
|
3
|
+
//#region src/string/pluralize/index.ts
|
|
4
|
+
var pluralize = new require_string_pluralize_pluralize.Pluralize();
|
|
5
|
+
//#endregion
|
|
6
|
+
exports.Pluralize = require_string_pluralize_pluralize.Pluralize;
|
|
6
7
|
exports.pluralize = pluralize;
|
|
@@ -1,210 +1,164 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
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
|
-
* Check if a word is part of the map.
|
|
168
|
-
*/
|
|
169
|
-
checkWord(word, reason) {
|
|
170
|
-
const replaceMap = reason === "Plurality" ? this.irregularSingles : this.irregularPlurals;
|
|
171
|
-
const keepMap = reason === "Plurality" ? this.irregularPlurals : this.irregularSingles;
|
|
172
|
-
const rules = reason === "Singularity" ? this.pluralRules : this.singularRules;
|
|
173
|
-
const token = word.toLowerCase();
|
|
174
|
-
if (keepMap[token]) return true;
|
|
175
|
-
if (replaceMap[token]) return false;
|
|
176
|
-
return this.sanitizeWord(token, token, rules) === token;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Pluralize or singularize a word based on the passed in count.
|
|
180
|
-
*/
|
|
181
|
-
pluralize(word, count, inclusive) {
|
|
182
|
-
const pluralized = count === 1 ? this.singular(word) : this.plural(word);
|
|
183
|
-
return (inclusive ? `${count} ` : "") + pluralized;
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Pluralize a word.
|
|
187
|
-
*/
|
|
188
|
-
plural(singularWord) {
|
|
189
|
-
return this.replaceWord(singularWord, "SingularToPlural");
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Check if a word is plural.
|
|
193
|
-
*/
|
|
194
|
-
isPlural(word) {
|
|
195
|
-
this.checkWord(word, "Plurality");
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Singularize a word.
|
|
199
|
-
*/
|
|
200
|
-
singular(pluralWord) {
|
|
201
|
-
return this.replaceWord(pluralWord, "PluralToSingular");
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Check if a word is singular.
|
|
205
|
-
*/
|
|
206
|
-
isSingular(word) {
|
|
207
|
-
this.checkWord(word, "Singularity");
|
|
208
|
-
}
|
|
209
|
-
}
|
|
2
|
+
const require_string_pluralize_rules_irregular = require("./rules/irregular.cjs");
|
|
3
|
+
const require_string_pluralize_rules_pluralization = require("./rules/pluralization.cjs");
|
|
4
|
+
const require_string_pluralize_rules_singularization = require("./rules/singularization.cjs");
|
|
5
|
+
const require_string_pluralize_rules_uncountable = require("./rules/uncountable.cjs");
|
|
6
|
+
//#region src/string/pluralize/pluralize.ts
|
|
7
|
+
var TagIndexRegExp = /\$(\d{1,2})/g;
|
|
8
|
+
var Pluralize = class {
|
|
9
|
+
pluralRules = [];
|
|
10
|
+
singularRules = [];
|
|
11
|
+
uncountables = {};
|
|
12
|
+
irregularPlurals = {};
|
|
13
|
+
irregularSingles = {};
|
|
14
|
+
constructor() {
|
|
15
|
+
for (let index = 0; index < require_string_pluralize_rules_irregular.IRREGULAR_WORDS.length; index += 1) this.addIrregularRule(require_string_pluralize_rules_irregular.IRREGULAR_WORDS[index][0], require_string_pluralize_rules_irregular.IRREGULAR_WORDS[index][1]);
|
|
16
|
+
for (let index = 0; index < require_string_pluralize_rules_pluralization.PLURALIZATION_RULES.length; index += 1) this.addPluralRule(require_string_pluralize_rules_pluralization.PLURALIZATION_RULES[index][0], require_string_pluralize_rules_pluralization.PLURALIZATION_RULES[index][1]);
|
|
17
|
+
for (let index = 0; index < require_string_pluralize_rules_singularization.SINGULARIZATION_RULES.length; index += 1) this.addSingularRule(require_string_pluralize_rules_singularization.SINGULARIZATION_RULES[index][0], require_string_pluralize_rules_singularization.SINGULARIZATION_RULES[index][1]);
|
|
18
|
+
for (let index = 0; index < require_string_pluralize_rules_uncountable.UNCOUNTABLE_WORDS.length; index += 1) this.addUncountableRule(require_string_pluralize_rules_uncountable.UNCOUNTABLE_WORDS[index]);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Add a pluralization rule to the collection.
|
|
22
|
+
*/
|
|
23
|
+
addPluralRule(rule, replacement) {
|
|
24
|
+
this.pluralRules.push([this.sanitizeRule(rule), replacement]);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Add a singularization rule to the collection.
|
|
28
|
+
*/
|
|
29
|
+
addSingularRule(rule, replacement) {
|
|
30
|
+
this.singularRules.push([this.sanitizeRule(rule), replacement]);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Add an uncountable word rule.
|
|
34
|
+
*/
|
|
35
|
+
addUncountableRule(word) {
|
|
36
|
+
if (typeof word === "string") {
|
|
37
|
+
this.uncountables[word.toLowerCase()] = true;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this.addPluralRule(word, "$0");
|
|
41
|
+
this.addSingularRule(word, "$0");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Add an irregular word definition.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} single
|
|
47
|
+
* @param {string} plural
|
|
48
|
+
*/
|
|
49
|
+
addIrregularRule(single, plural) {
|
|
50
|
+
plural = plural.toLowerCase();
|
|
51
|
+
single = single.toLowerCase();
|
|
52
|
+
this.irregularSingles[single] = plural;
|
|
53
|
+
this.irregularPlurals[plural] = single;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sanitize a pluralization rule to a usable regular expression.
|
|
57
|
+
*/
|
|
58
|
+
sanitizeRule(rule) {
|
|
59
|
+
if (typeof rule === "string") return new RegExp(`^${rule}$`, "i");
|
|
60
|
+
return rule;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Pass in a word token to produce a function that can replicate the case on
|
|
64
|
+
* another word.
|
|
65
|
+
*/
|
|
66
|
+
restoreCase(word, token) {
|
|
67
|
+
if (word === token) return token;
|
|
68
|
+
if (word === word.toUpperCase()) return token.toUpperCase();
|
|
69
|
+
if (word[0] === word[0].toUpperCase()) return token.charAt(0).toUpperCase() + token.substring(1).toLowerCase();
|
|
70
|
+
return token.toLowerCase();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Interpolate a regexp string.
|
|
74
|
+
*
|
|
75
|
+
* @param {string} str
|
|
76
|
+
* @param {Array} args
|
|
77
|
+
* @return {string}
|
|
78
|
+
*/
|
|
79
|
+
interpolate(str, args) {
|
|
80
|
+
return str.replace(TagIndexRegExp, (match, index) => {
|
|
81
|
+
return args[index] || "";
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Replace a word using a rule.
|
|
86
|
+
*/
|
|
87
|
+
replace(word, rule) {
|
|
88
|
+
return word.replace(rule[0], (...args) => {
|
|
89
|
+
const [match, index] = args;
|
|
90
|
+
const result = this.interpolate(rule[1], args);
|
|
91
|
+
if (match === "") return this.restoreCase(word[index - 1], result);
|
|
92
|
+
return this.restoreCase(match, result);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Sanitize a word by passing in the word and sanitization rules.
|
|
97
|
+
*/
|
|
98
|
+
sanitizeWord(token, word, rules) {
|
|
99
|
+
if (!token.length || this.uncountables[token]) return word;
|
|
100
|
+
let len = rules.length;
|
|
101
|
+
while (len--) {
|
|
102
|
+
const rule = rules[len];
|
|
103
|
+
if (typeof rule[0] === "string" || rule[0].test(word)) return this.replace(word, rule);
|
|
104
|
+
}
|
|
105
|
+
return word;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Replace a word with the updated word.
|
|
109
|
+
*/
|
|
110
|
+
replaceWord(word, direction) {
|
|
111
|
+
const replaceMap = direction === "SingularToPlural" ? this.irregularSingles : this.irregularPlurals;
|
|
112
|
+
const keepMap = direction === "SingularToPlural" ? this.irregularPlurals : this.irregularSingles;
|
|
113
|
+
const rules = direction === "SingularToPlural" ? this.pluralRules : this.singularRules;
|
|
114
|
+
const token = word.toLowerCase();
|
|
115
|
+
if (keepMap[token]) return this.restoreCase(word, token);
|
|
116
|
+
if (replaceMap[token]) return this.restoreCase(word, replaceMap[token]);
|
|
117
|
+
return this.sanitizeWord(token, word, rules);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Check if a word is part of the map.
|
|
121
|
+
*/
|
|
122
|
+
checkWord(word, reason) {
|
|
123
|
+
const replaceMap = reason === "Plurality" ? this.irregularSingles : this.irregularPlurals;
|
|
124
|
+
const keepMap = reason === "Plurality" ? this.irregularPlurals : this.irregularSingles;
|
|
125
|
+
const rules = reason === "Plurality" ? this.pluralRules : this.singularRules;
|
|
126
|
+
const token = word.toLowerCase();
|
|
127
|
+
if (keepMap[token]) return true;
|
|
128
|
+
if (replaceMap[token]) return false;
|
|
129
|
+
return this.sanitizeWord(token, token, rules) === token;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Pluralize or singularize a word based on the passed in count.
|
|
133
|
+
*/
|
|
134
|
+
pluralize(word, count, inclusive = false) {
|
|
135
|
+
const pluralized = count === 1 ? this.singular(word) : this.plural(word);
|
|
136
|
+
return (inclusive ? `${count} ` : "") + pluralized;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Pluralize a word.
|
|
140
|
+
*/
|
|
141
|
+
plural(singularWord) {
|
|
142
|
+
return this.replaceWord(singularWord, "SingularToPlural");
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Check if a word is plural.
|
|
146
|
+
*/
|
|
147
|
+
isPlural(word) {
|
|
148
|
+
return this.checkWord(word, "Plurality");
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Singularize a word.
|
|
152
|
+
*/
|
|
153
|
+
singular(pluralWord) {
|
|
154
|
+
return this.replaceWord(pluralWord, "PluralToSingular");
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Check if a word is singular.
|
|
158
|
+
*/
|
|
159
|
+
isSingular(word) {
|
|
160
|
+
return this.checkWord(word, "Singularity");
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
//#endregion
|
|
210
164
|
exports.Pluralize = Pluralize;
|
|
@@ -41,7 +41,7 @@ export declare class Pluralize {
|
|
|
41
41
|
* @param {Array} args
|
|
42
42
|
* @return {string}
|
|
43
43
|
*/
|
|
44
|
-
interpolate
|
|
44
|
+
private interpolate;
|
|
45
45
|
/**
|
|
46
46
|
* Replace a word using a rule.
|
|
47
47
|
*/
|
|
@@ -50,10 +50,6 @@ export declare class Pluralize {
|
|
|
50
50
|
* Sanitize a word by passing in the word and sanitization rules.
|
|
51
51
|
*/
|
|
52
52
|
sanitizeWord(token: string, word: string, rules: Rule[]): string;
|
|
53
|
-
/**
|
|
54
|
-
* Replace a word with the updated word.
|
|
55
|
-
*/
|
|
56
|
-
old_replaceWord(replaceMap: Record<string, string>, keepMap: Record<string, string>, rules: Rule[]): (word: string) => string;
|
|
57
53
|
/**
|
|
58
54
|
* Replace a word with the updated word.
|
|
59
55
|
*/
|
|
@@ -65,7 +61,7 @@ export declare class Pluralize {
|
|
|
65
61
|
/**
|
|
66
62
|
* Pluralize or singularize a word based on the passed in count.
|
|
67
63
|
*/
|
|
68
|
-
pluralize(word: string, count: number, inclusive
|
|
64
|
+
pluralize(word: string, count: number, inclusive?: boolean): string;
|
|
69
65
|
/**
|
|
70
66
|
* Pluralize a word.
|
|
71
67
|
*/
|
|
@@ -73,7 +69,7 @@ export declare class Pluralize {
|
|
|
73
69
|
/**
|
|
74
70
|
* Check if a word is plural.
|
|
75
71
|
*/
|
|
76
|
-
isPlural(word: string):
|
|
72
|
+
isPlural(word: string): boolean;
|
|
77
73
|
/**
|
|
78
74
|
* Singularize a word.
|
|
79
75
|
*/
|
|
@@ -81,5 +77,5 @@ export declare class Pluralize {
|
|
|
81
77
|
/**
|
|
82
78
|
* Check if a word is singular.
|
|
83
79
|
*/
|
|
84
|
-
isSingular(word: string):
|
|
80
|
+
isSingular(word: string): boolean;
|
|
85
81
|
}
|