@bccampus/utils 0.2.2 → 0.3.0

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.
Files changed (216) hide show
  1. package/README.md +3 -8
  2. package/dist/array/chunk.cjs +13 -5
  3. package/dist/array/chunk.d.ts +7 -0
  4. package/dist/array/chunk.js +14 -7
  5. package/dist/array/chunk.test.d.ts +1 -0
  6. package/dist/date/iso-date-time/constants.cjs +27 -0
  7. package/dist/date/iso-date-time/constants.d.ts +9 -0
  8. package/dist/date/iso-date-time/constants.js +19 -0
  9. package/dist/date/iso-date-time/index.cjs +8 -0
  10. package/dist/date/iso-date-time/index.d.ts +5 -0
  11. package/dist/date/iso-date-time/index.js +3 -0
  12. package/dist/date/iso-date-time/iso-date-time.arithmetic.test.d.ts +1 -0
  13. package/dist/date/iso-date-time/iso-date-time.cjs +232 -0
  14. package/dist/date/{iso-date-time.d.ts → iso-date-time/iso-date-time.d.ts} +3 -32
  15. package/dist/date/iso-date-time/iso-date-time.format.test.d.ts +1 -0
  16. package/dist/date/iso-date-time/iso-date-time.js +231 -0
  17. package/dist/date/iso-date-time/iso-date-time.test.d.ts +1 -0
  18. package/dist/date/iso-date-time/types.cjs +0 -0
  19. package/dist/date/iso-date-time/types.d.ts +25 -0
  20. package/dist/date/iso-date-time/types.js +0 -0
  21. package/dist/duration/add-durations.cjs +5 -8
  22. package/dist/duration/add-durations.js +5 -9
  23. package/dist/duration/duration.test.d.ts +1 -0
  24. package/dist/duration/is-duration.cjs +4 -3
  25. package/dist/duration/is-duration.js +5 -5
  26. package/dist/duration/to-number.cjs +11 -10
  27. package/dist/duration/to-number.d.ts +1 -1
  28. package/dist/duration/to-number.js +11 -11
  29. package/dist/format/formatter.cjs +31 -31
  30. package/dist/format/formatter.d.ts +4 -4
  31. package/dist/format/formatter.js +32 -37
  32. package/dist/format/formatter.test.d.ts +1 -0
  33. package/dist/format/stringfy.test.d.ts +1 -0
  34. package/dist/format/stringify.cjs +34 -40
  35. package/dist/format/stringify.d.ts +1 -1
  36. package/dist/format/stringify.js +35 -42
  37. package/dist/format/template.cjs +25 -32
  38. package/dist/format/template.d.ts +3 -3
  39. package/dist/format/template.js +24 -33
  40. package/dist/format/template.test.d.ts +1 -0
  41. package/dist/index.cjs +0 -4
  42. package/dist/index.d.ts +0 -1
  43. package/dist/index.js +0 -4
  44. package/dist/object/get.cjs +5 -5
  45. package/dist/object/get.d.ts +1 -1
  46. package/dist/object/get.js +6 -7
  47. package/dist/object/get.test.d.ts +1 -0
  48. package/dist/object/is-object.cjs +5 -2
  49. package/dist/object/is-object.d.ts +1 -1
  50. package/dist/object/is-object.js +6 -4
  51. package/dist/object/is-object.test.d.ts +1 -0
  52. package/dist/object/omit.cjs +14 -16
  53. package/dist/object/omit.d.ts +1 -1
  54. package/dist/object/omit.js +15 -18
  55. package/dist/object/omit.test.d.ts +1 -0
  56. package/dist/object/pick.cjs +10 -9
  57. package/dist/object/pick.d.ts +1 -1
  58. package/dist/object/pick.js +9 -9
  59. package/dist/object/pick.test.d.ts +1 -0
  60. package/dist/object/set.cjs +13 -12
  61. package/dist/object/set.d.ts +1 -1
  62. package/dist/object/set.js +14 -14
  63. package/dist/object/set.test.d.ts +1 -0
  64. package/dist/path/file-url-to-path.cjs +10 -6
  65. package/dist/path/file-url-to-path.js +11 -8
  66. package/dist/path/file-url-to-path.test.d.ts +1 -0
  67. package/dist/path/normalize.cjs +41 -27
  68. package/dist/path/normalize.d.ts +8 -2
  69. package/dist/path/normalize.js +41 -28
  70. package/dist/path/normalize.test.d.ts +1 -0
  71. package/dist/set/difference.cjs +5 -6
  72. package/dist/set/difference.d.ts +1 -1
  73. package/dist/set/difference.js +6 -8
  74. package/dist/set/intersection.cjs +6 -9
  75. package/dist/set/intersection.d.ts +1 -1
  76. package/dist/set/intersection.js +7 -11
  77. package/dist/set/is-superset.cjs +5 -8
  78. package/dist/set/is-superset.d.ts +1 -1
  79. package/dist/set/is-superset.js +6 -10
  80. package/dist/set/set.test.d.ts +1 -0
  81. package/dist/set/symmetric-difference.cjs +6 -10
  82. package/dist/set/symmetric-difference.d.ts +1 -1
  83. package/dist/set/symmetric-difference.js +7 -12
  84. package/dist/set/union.cjs +5 -6
  85. package/dist/set/union.d.ts +1 -1
  86. package/dist/set/union.js +6 -8
  87. package/dist/string/capitalize.cjs +5 -2
  88. package/dist/string/capitalize.d.ts +1 -1
  89. package/dist/string/capitalize.js +6 -4
  90. package/dist/string/capitalize.test.d.ts +1 -0
  91. package/dist/string/normalize.cjs +7 -2
  92. package/dist/string/normalize.js +8 -4
  93. package/dist/string/normalize.test.d.ts +1 -0
  94. package/dist/string/pluralize/index.cjs +5 -4
  95. package/dist/string/pluralize/index.js +4 -5
  96. package/dist/string/pluralize/pluralize.cjs +162 -208
  97. package/dist/string/pluralize/pluralize.d.ts +4 -8
  98. package/dist/string/pluralize/pluralize.js +158 -205
  99. package/dist/string/pluralize/pluralize.test.d.ts +1 -0
  100. package/dist/string/pluralize/rules/index.cjs +8 -9
  101. package/dist/string/pluralize/rules/index.js +1 -6
  102. package/dist/string/pluralize/rules/irregular.cjs +55 -59
  103. package/dist/string/pluralize/rules/irregular.js +56 -61
  104. package/dist/string/pluralize/rules/pluralization.cjs +28 -36
  105. package/dist/string/pluralize/rules/pluralization.js +29 -38
  106. package/dist/string/pluralize/rules/singularization.cjs +27 -44
  107. package/dist/string/pluralize/rules/singularization.js +28 -46
  108. package/dist/string/pluralize/rules/uncountable.cjs +104 -110
  109. package/dist/string/pluralize/rules/uncountable.js +105 -112
  110. package/dist/string/pluralize/types.cjs +0 -1
  111. package/dist/string/pluralize/types.js +0 -1
  112. package/dist/string/slugify/index.cjs +2129 -2246
  113. package/dist/string/slugify/index.js +2130 -2248
  114. package/dist/string/slugify/slugify.test.d.ts +1 -0
  115. package/dist/string/strip-html-tags.cjs +4 -3
  116. package/dist/string/strip-html-tags.js +5 -5
  117. package/dist/string/strip-html-tags.test.d.ts +1 -0
  118. package/dist/string/to-int.cjs +14 -6
  119. package/dist/string/to-int.js +15 -8
  120. package/dist/string/to-int.test.d.ts +1 -0
  121. package/dist/string/to-sentence-case.cjs +6 -6
  122. package/dist/string/to-sentence-case.d.ts +1 -1
  123. package/dist/string/to-sentence-case.js +7 -8
  124. package/dist/string/to-sentence-case.test.d.ts +1 -0
  125. package/dist/string/trim.cjs +8 -7
  126. package/dist/string/trim.js +9 -9
  127. package/dist/string/trim.test.d.ts +1 -0
  128. package/dist/string/truncate.cjs +16 -15
  129. package/dist/string/truncate.js +17 -17
  130. package/dist/string/truncate.test.d.ts +1 -0
  131. package/dist/value/is-empty.cjs +30 -12
  132. package/dist/value/is-empty.d.ts +1 -1
  133. package/dist/value/is-empty.js +31 -14
  134. package/dist/value/value.test.d.ts +1 -0
  135. package/package.json +183 -67
  136. package/dist/array/index.cjs +0 -4
  137. package/dist/array/index.d.ts +0 -1
  138. package/dist/array/index.js +0 -4
  139. package/dist/date/index.cjs +0 -8
  140. package/dist/date/index.d.ts +0 -1
  141. package/dist/date/index.js +0 -8
  142. package/dist/date/iso-date-time.cjs +0 -256
  143. package/dist/date/iso-date-time.js +0 -256
  144. package/dist/duration/index.cjs +0 -8
  145. package/dist/duration/index.d.ts +0 -3
  146. package/dist/duration/index.js +0 -8
  147. package/dist/format/index.cjs +0 -13
  148. package/dist/format/index.d.ts +0 -3
  149. package/dist/format/index.js +0 -13
  150. package/dist/object/index.cjs +0 -12
  151. package/dist/object/index.d.ts +0 -5
  152. package/dist/object/index.js +0 -12
  153. package/dist/path/index.cjs +0 -6
  154. package/dist/path/index.d.ts +0 -2
  155. package/dist/path/index.js +0 -6
  156. package/dist/set/index.cjs +0 -12
  157. package/dist/set/index.d.ts +0 -5
  158. package/dist/set/index.js +0 -12
  159. package/dist/string/index.cjs +0 -14
  160. package/dist/string/index.d.ts +0 -6
  161. package/dist/string/index.js +0 -14
  162. package/dist/value/index.cjs +0 -4
  163. package/dist/value/index.d.ts +0 -1
  164. package/dist/value/index.js +0 -4
  165. package/eslint.config.js +0 -19
  166. package/src/array/chunk.ts +0 -6
  167. package/src/array/index.ts +0 -1
  168. package/src/date/index.ts +0 -1
  169. package/src/date/iso-date-time.ts +0 -356
  170. package/src/duration/add-durations.ts +0 -16
  171. package/src/duration/index.ts +0 -3
  172. package/src/duration/is-duration.ts +0 -5
  173. package/src/duration/to-number.ts +0 -18
  174. package/src/format/formatter.ts +0 -37
  175. package/src/format/index.ts +0 -3
  176. package/src/format/stringify.ts +0 -87
  177. package/src/format/template.ts +0 -48
  178. package/src/index.ts +0 -1
  179. package/src/object/get.ts +0 -9
  180. package/src/object/index.ts +0 -5
  181. package/src/object/is-object.ts +0 -2
  182. package/src/object/omit.ts +0 -21
  183. package/src/object/pick.ts +0 -10
  184. package/src/object/set.ts +0 -15
  185. package/src/path/file-url-to-path.ts +0 -12
  186. package/src/path/index.ts +0 -2
  187. package/src/path/normalize.ts +0 -72
  188. package/src/set/difference.ts +0 -9
  189. package/src/set/index.ts +0 -5
  190. package/src/set/intersection.ts +0 -12
  191. package/src/set/is-superset.ts +0 -12
  192. package/src/set/symmetric-difference.ts +0 -12
  193. package/src/set/union.ts +0 -8
  194. package/src/string/capitalize.ts +0 -2
  195. package/src/string/index.ts +0 -6
  196. package/src/string/normalize.ts +0 -7
  197. package/src/string/pluralize/index.ts +0 -7
  198. package/src/string/pluralize/pluralize.ts +0 -296
  199. package/src/string/pluralize/rules/index.ts +0 -4
  200. package/src/string/pluralize/rules/irregular.ts +0 -59
  201. package/src/string/pluralize/rules/pluralization.ts +0 -40
  202. package/src/string/pluralize/rules/singularization.ts +0 -46
  203. package/src/string/pluralize/rules/uncountable.ts +0 -107
  204. package/src/string/pluralize/types.ts +0 -4
  205. package/src/string/slugify/index.ts +0 -3
  206. package/src/string/strip-html-tags.ts +0 -4
  207. package/src/string/to-int.ts +0 -20
  208. package/src/string/to-sentence-case.ts +0 -6
  209. package/src/string/trim.ts +0 -11
  210. package/src/string/truncate.ts +0 -44
  211. package/src/value/index.ts +0 -1
  212. package/src/value/is-empty.ts +0 -31
  213. package/tsconfig.json +0 -18
  214. package/tsconfig.lib.json +0 -37
  215. package/tsconfig.node.json +0 -25
  216. package/vite.config.ts +0 -76
@@ -1,5 +0,0 @@
1
- export * from "./is-object";
2
- export * from "./get";
3
- export * from "./set";
4
- export * from "./omit";
5
- export * from "./pick";
@@ -1,2 +0,0 @@
1
- export const isObject = (object: unknown) =>
2
- typeof object === "object" && !Array.isArray(object) && object !== null;
@@ -1,21 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- export const omit = <T extends object>(object: T, props: string[]) => {
3
- const result: Partial<T> = { ...object };
4
-
5
- props.forEach((prop) => {
6
- const propChunks = prop.split(".");
7
- const lastChunk = propChunks.pop();
8
- if (lastChunk) {
9
- const ref = propChunks.reduce<any>(
10
- (reducedObject, key) =>
11
- reducedObject && key in reducedObject
12
- ? reducedObject[key]
13
- : undefined,
14
- result,
15
- );
16
- if (ref && lastChunk in ref) delete ref[lastChunk];
17
- }
18
- });
19
-
20
- return result;
21
- };
@@ -1,10 +0,0 @@
1
- import { set } from ".";
2
- import { get } from "./get";
3
-
4
- export const pick = <T extends object>(object: T, props: string[]) => {
5
- return props.reduce<Record<string, unknown>>((result, key) => {
6
- set(result, key, get(object, key));
7
-
8
- return result;
9
- }, {}) as Partial<T>;
10
- };
package/src/object/set.ts DELETED
@@ -1,15 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- export const set = <T extends object, V>(object: T, prop: string, value: V) => {
3
- const propChunks = prop.split(".");
4
- const lastChunk = propChunks.pop();
5
- if (!lastChunk) return object;
6
-
7
- const ref = propChunks.reduce<any>((reducedObject, key) => {
8
- if (reducedObject[key] === undefined) reducedObject[key] = {};
9
- return reducedObject[key];
10
- }, object);
11
-
12
- ref[lastChunk] = value;
13
-
14
- return object;
15
- };
@@ -1,12 +0,0 @@
1
- const windowsDriveRegExp = /^\/[a-zA-Z1-9_-]+:\//;
2
-
3
- /**
4
- * Browser compatible version of `node:url.fileURLToPath`
5
- */
6
- export function fileURLToPath(fileUrl: string) {
7
- const url = new URL(fileUrl);
8
- let path = decodeURIComponent(url.pathname);
9
- if (windowsDriveRegExp.test(path)) path = path.slice(1);
10
-
11
- return path;
12
- }
package/src/path/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./normalize";
2
- export * from "./file-url-to-path";
@@ -1,72 +0,0 @@
1
- import { fileURLToPath } from "./file-url-to-path";
2
-
3
- export interface NormalizeOptions {
4
- /**
5
- * - `always`: ensure leading slash exists
6
- * - `never`: ensure leading slash does not exist
7
- * - `as-is`: do not modify the leading char
8
- *
9
- * @default "always"
10
- */
11
- leadingSlash?: "always" | "never" | "as-is";
12
-
13
- /**
14
- * - `always`: ensure trailing slash exists
15
- * - `never`: ensure trailing slash does not exist
16
- * - `as-is`: do not modify the trailing char
17
- *
18
- * @default "never"
19
- */
20
- trailingSlash?: "always" | "never" | "as-is";
21
- }
22
-
23
- const defaultOptions: Required<NormalizeOptions> = {
24
- leadingSlash: "always",
25
- trailingSlash: "never",
26
- };
27
-
28
- const windowsPathSepRegExp = /[\\]+/g;
29
- const unixPathSepRegExp = /[/]+/g;
30
- const windowsDriveRegExp = /^[a-zA-Z1-9_-]+:\//;
31
- const fileProtocolRegExp = /^file:\/\//;
32
- const leadingSlashRegExp = /^\//g;
33
- const trailingSlashRegExp = /\/$/g;
34
-
35
- /**
36
- * Clears and converts the provided path to a Posix filepath
37
- *
38
- * By default, adds a leading slash and removes the trailing
39
- *
40
- * @example
41
- * ```
42
- * normalize("src/test/"); //returns: /src/test
43
- * normalize("C:\\Users\\test\\folder\\"); //returns: C:/Users/test/folder
44
- * normalize("file:///C:/Users/test/folder/file.ts"); //returns: C:/Users/test/folder/file.ts
45
- * ```
46
- */
47
- export function normalize(path: string, options?: NormalizeOptions) {
48
- const conf = { ...defaultOptions, ...options };
49
- const sep = "\\";
50
- let normalPath = path.trim();
51
-
52
- if (fileProtocolRegExp.test(normalPath))
53
- normalPath = fileURLToPath(normalPath);
54
-
55
- normalPath = normalPath
56
- .replace(windowsPathSepRegExp, sep) // windows -> linux
57
- .replace(unixPathSepRegExp, sep); // multiple -> single
58
-
59
- if (conf.leadingSlash !== "as-is") {
60
- normalPath = normalPath.replace(leadingSlashRegExp, "");
61
-
62
- if (conf.leadingSlash === "always" && !windowsDriveRegExp.test(normalPath))
63
- normalPath = `${sep}${normalPath}`;
64
- }
65
-
66
- if (conf.trailingSlash !== "as-is") {
67
- normalPath = normalPath.replace(trailingSlashRegExp, "");
68
- if (conf.trailingSlash === "always") normalPath = `${normalPath}${sep}`;
69
- }
70
-
71
- return normalPath;
72
- }
@@ -1,9 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
- export function difference(setA: Iterable<any>, setB: Iterable<any>) {
4
- const _difference = new Set(setA);
5
- for (const elem of setB) {
6
- _difference.delete(elem);
7
- }
8
- return _difference;
9
- }
package/src/set/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from "./union";
2
- export * from "./intersection";
3
- export * from "./is-superset";
4
- export * from "./difference";
5
- export * from "./symmetric-difference";
@@ -1,12 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- export function intersection(setA: Iterable<any>, setB: Iterable<any>) {
3
- const _setA = setA instanceof Set ? setA : new Set(setA);
4
-
5
- const _intersection = new Set();
6
- for (const elem of setB) {
7
- if (_setA.has(elem)) {
8
- _intersection.add(elem);
9
- }
10
- }
11
- return _intersection;
12
- }
@@ -1,12 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
- export function isSuperset(set: Iterable<any>, subset: Iterable<any>) {
4
- const _set = set instanceof Set ? set : new Set(set);
5
-
6
- for (const elem of subset) {
7
- if (!_set.has(elem)) {
8
- return false;
9
- }
10
- }
11
- return true;
12
- }
@@ -1,12 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- export function symmetricDifference(setA: Iterable<any>, setB: Iterable<any>) {
3
- const _difference = new Set(setA);
4
- for (const elem of setB) {
5
- if (_difference.has(elem)) {
6
- _difference.delete(elem);
7
- } else {
8
- _difference.add(elem);
9
- }
10
- }
11
- return _difference;
12
- }
package/src/set/union.ts DELETED
@@ -1,8 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- export function union(setA: Iterable<any>, setB: Iterable<any>) {
3
- const _union = new Set(setA);
4
- for (const elem of setB) {
5
- _union.add(elem);
6
- }
7
- return _union;
8
- }
@@ -1,2 +0,0 @@
1
- export const capitalize = <T extends string>(s: T) =>
2
- (s[0].toUpperCase() + s.slice(1)) as Capitalize<typeof s>;
@@ -1,6 +0,0 @@
1
- export * from "./truncate";
2
- export * from "./trim";
3
- export * from "./strip-html-tags";
4
- export * from "./capitalize";
5
- export * from "./to-sentence-case";
6
- export * from "./to-int";
@@ -1,7 +0,0 @@
1
- /**
2
- * Trims white spaces and
3
- * replaces multiple white space characters with a single space
4
- */
5
- export function normalize(text: string) {
6
- return text.trim().replace(/\s+/g, " ");
7
- }
@@ -1,7 +0,0 @@
1
- import { Pluralize } from "./pluralize";
2
-
3
- const pluralize = new Pluralize();
4
-
5
- export type * from "./types";
6
-
7
- export { pluralize, Pluralize };
@@ -1,296 +0,0 @@
1
- /**
2
- * Typscript version of [pluralize](https://github.com/plurals/pluralize)
3
- */
4
-
5
- import type { Rule, RuleKey, RuleValue } from "./types";
6
-
7
- import {
8
- IRREGULAR_WORDS,
9
- PLURALIZATION_RULES,
10
- SINGULARIZATION_RULES,
11
- UNCOUNTABLE_WORDS,
12
- } from "./rules";
13
-
14
- const TagIndexRegExp = /\$(\d{1,2})/g;
15
-
16
- export class Pluralize {
17
- // Rule storage - pluralize and singularize need to be run sequentially,
18
- // while other rules can be optimized using an object for instant lookups.
19
- private pluralRules: Rule[] = [];
20
- private singularRules: Rule[] = [];
21
- private uncountables: Record<string, boolean> = {};
22
- private irregularPlurals: Record<string, string> = {};
23
- private irregularSingles: Record<string, string> = {};
24
-
25
- constructor() {
26
- for (let index = 0; index < IRREGULAR_WORDS.length; index += 1) {
27
- this.addIrregularRule(
28
- IRREGULAR_WORDS[index][0],
29
- IRREGULAR_WORDS[index][1],
30
- );
31
- }
32
-
33
- for (let index = 0; index < PLURALIZATION_RULES.length; index += 1) {
34
- this.addPluralRule(
35
- PLURALIZATION_RULES[index][0],
36
- PLURALIZATION_RULES[index][1],
37
- );
38
- }
39
-
40
- for (let index = 0; index < SINGULARIZATION_RULES.length; index += 1) {
41
- this.addSingularRule(
42
- SINGULARIZATION_RULES[index][0],
43
- SINGULARIZATION_RULES[index][1],
44
- );
45
- }
46
-
47
- for (let index = 0; index < UNCOUNTABLE_WORDS.length; index += 1) {
48
- this.addUncountableRule(UNCOUNTABLE_WORDS[index]);
49
- }
50
- }
51
-
52
- /**
53
- * Add a pluralization rule to the collection.
54
- */
55
- addPluralRule(rule: RuleKey, replacement: RuleValue) {
56
- this.pluralRules.push([this.sanitizeRule(rule), replacement]);
57
- }
58
-
59
- /**
60
- * Add a singularization rule to the collection.
61
- */
62
- addSingularRule(rule: RuleKey, replacement: RuleValue) {
63
- this.singularRules.push([this.sanitizeRule(rule), replacement]);
64
- }
65
-
66
- /**
67
- * Add an uncountable word rule.
68
- */
69
- addUncountableRule(word: RuleKey) {
70
- if (typeof word === "string") {
71
- this.uncountables[word.toLowerCase()] = true;
72
- return;
73
- }
74
-
75
- // Set singular and plural references for the word.
76
- this.addPluralRule(word, "$0");
77
- this.addSingularRule(word, "$0");
78
- }
79
-
80
- /**
81
- * Add an irregular word definition.
82
- *
83
- * @param {string} single
84
- * @param {string} plural
85
- */
86
- addIrregularRule(single: string, plural: string) {
87
- plural = plural.toLowerCase();
88
- single = single.toLowerCase();
89
-
90
- this.irregularSingles[single] = plural;
91
- this.irregularPlurals[plural] = single;
92
- }
93
-
94
- /**
95
- * Sanitize a pluralization rule to a usable regular expression.
96
- */
97
- sanitizeRule(rule: RuleKey) {
98
- if (typeof rule === "string") {
99
- return new RegExp(`^${rule}$`, "i");
100
- }
101
-
102
- return rule;
103
- }
104
-
105
- /**
106
- * Pass in a word token to produce a function that can replicate the case on
107
- * another word.
108
- */
109
- restoreCase(word: string, token: string) {
110
- // Tokens are an exact match.
111
- if (word === token) return token;
112
-
113
- // Lower cased words. E.g. "hello".
114
- if (word === word.toLowerCase()) return token.toLowerCase();
115
-
116
- // Upper cased words. E.g. "WHISKY".
117
- if (word === word.toUpperCase()) return token.toUpperCase();
118
-
119
- // Title cased words. E.g. "Title".
120
- if (word[0] === word[0].toUpperCase()) {
121
- return token.charAt(0).toUpperCase() + token.substr(1).toLowerCase();
122
- }
123
-
124
- // Lower cased words. E.g. "test".
125
- return token.toLowerCase();
126
- }
127
-
128
- /**
129
- * Interpolate a regexp string.
130
- *
131
- * @param {string} str
132
- * @param {Array} args
133
- * @return {string}
134
- */
135
- interpolate(str: string, args: string[]) {
136
- return str.replace(TagIndexRegExp, (match, index) => {
137
- return args[index] || "";
138
- });
139
- }
140
-
141
- /**
142
- * Replace a word using a rule.
143
- */
144
- replace(word: string, rule: Rule) {
145
- return word.replace(rule[0], (match, index, ...args) => {
146
- const result = this.interpolate(rule[1], args);
147
-
148
- if (match === "") {
149
- return this.restoreCase(word[index - 1], result);
150
- }
151
-
152
- return this.restoreCase(match, result);
153
- });
154
- }
155
-
156
- /**
157
- * Sanitize a word by passing in the word and sanitization rules.
158
- */
159
- sanitizeWord(token: string, word: string, rules: Rule[]) {
160
- // Empty string or doesn't need fixing.
161
- if (!token.length || this.uncountables[token]) {
162
- return word;
163
- }
164
-
165
- let len = rules.length;
166
-
167
- // Iterate over the sanitization rules and use the first one to match.
168
- while (len--) {
169
- const rule = rules[len];
170
-
171
- if (typeof rule[0] === "string" || rule[0].test(word))
172
- return this.replace(word, rule);
173
- }
174
-
175
- return word;
176
- }
177
-
178
- /**
179
- * Replace a word with the updated word.
180
- */
181
- old_replaceWord(
182
- replaceMap: Record<string, string>,
183
- keepMap: Record<string, string>,
184
- rules: Rule[],
185
- ) {
186
- return (word: string) => {
187
- // Get the correct token and case restoration functions.
188
- const token = word.toLowerCase();
189
-
190
- // Check against the keep object map.
191
- if (keepMap[token]) {
192
- return this.restoreCase(word, token);
193
- }
194
-
195
- // Check against the replacement map for a direct word replacement.
196
- if (replaceMap[token]) {
197
- return this.restoreCase(word, replaceMap[token]);
198
- }
199
-
200
- // Run all the rules against the word.
201
- return this.sanitizeWord(token, word, rules);
202
- };
203
- }
204
-
205
- /**
206
- * Replace a word with the updated word.
207
- */
208
- private replaceWord(
209
- word: string,
210
- direction: "SingularToPlural" | "PluralToSingular",
211
- ) {
212
- const replaceMap =
213
- direction === "SingularToPlural"
214
- ? this.irregularSingles
215
- : this.irregularPlurals;
216
- const keepMap =
217
- direction === "SingularToPlural"
218
- ? this.irregularPlurals
219
- : this.irregularSingles;
220
- const rules =
221
- direction === "SingularToPlural" ? this.pluralRules : this.singularRules;
222
-
223
- // Get the correct token and case restoration functions.
224
- const token = word.toLowerCase();
225
-
226
- // Check against the keep object map.
227
- if (keepMap[token]) {
228
- return this.restoreCase(word, token);
229
- }
230
-
231
- // Check against the replacement map for a direct word replacement.
232
- if (replaceMap[token]) {
233
- return this.restoreCase(word, replaceMap[token]);
234
- }
235
-
236
- // Run all the rules against the word.
237
- return this.sanitizeWord(token, word, rules);
238
- }
239
-
240
- /**
241
- * Check if a word is part of the map.
242
- */
243
- private checkWord(word: string, reason: "Singularity" | "Plurality") {
244
- const replaceMap =
245
- reason === "Plurality" ? this.irregularSingles : this.irregularPlurals;
246
- const keepMap =
247
- reason === "Plurality" ? this.irregularPlurals : this.irregularSingles;
248
- const rules =
249
- reason === "Singularity" ? this.pluralRules : this.singularRules;
250
-
251
- const token = word.toLowerCase();
252
-
253
- if (keepMap[token]) return true;
254
-
255
- if (replaceMap[token]) return false;
256
-
257
- return this.sanitizeWord(token, token, rules) === token;
258
- }
259
-
260
- /**
261
- * Pluralize or singularize a word based on the passed in count.
262
- */
263
- pluralize(word: string, count: number, inclusive: boolean) {
264
- const pluralized = count === 1 ? this.singular(word) : this.plural(word);
265
-
266
- return (inclusive ? `${count} ` : "") + pluralized;
267
- }
268
-
269
- /**
270
- * Pluralize a word.
271
- */
272
- plural(singularWord: string) {
273
- return this.replaceWord(singularWord, "SingularToPlural");
274
- }
275
-
276
- /**
277
- * Check if a word is plural.
278
- */
279
- isPlural(word: string) {
280
- this.checkWord(word, "Plurality");
281
- }
282
-
283
- /**
284
- * Singularize a word.
285
- */
286
- singular(pluralWord: string) {
287
- return this.replaceWord(pluralWord, "PluralToSingular");
288
- }
289
-
290
- /**
291
- * Check if a word is singular.
292
- */
293
- isSingular(word: string) {
294
- this.checkWord(word, "Singularity");
295
- }
296
- }
@@ -1,4 +0,0 @@
1
- export { IRREGULAR_WORDS } from './irregular';
2
- export { PLURALIZATION_RULES } from './pluralization';
3
- export { SINGULARIZATION_RULES } from './singularization';
4
- export { UNCOUNTABLE_WORDS } from './uncountable';
@@ -1,59 +0,0 @@
1
- export const IRREGULAR_WORDS: [string, string][] = [
2
- // Pronouns.
3
- ['I', 'we'],
4
- ['me', 'us'],
5
- ['he', 'they'],
6
- ['she', 'they'],
7
- ['them', 'them'],
8
- ['myself', 'ourselves'],
9
- ['yourself', 'yourselves'],
10
- ['itself', 'themselves'],
11
- ['herself', 'themselves'],
12
- ['himself', 'themselves'],
13
- ['themself', 'themselves'],
14
- ['is', 'are'],
15
- ['was', 'were'],
16
- ['has', 'have'],
17
- ['this', 'these'],
18
- ['that', 'those'],
19
- ['my', 'our'],
20
- ['its', 'their'],
21
- ['his', 'their'],
22
- ['her', 'their'],
23
- // Words ending in with a consonant and `o`.
24
- ['echo', 'echoes'],
25
- ['dingo', 'dingoes'],
26
- ['volcano', 'volcanoes'],
27
- ['tornado', 'tornadoes'],
28
- ['torpedo', 'torpedoes'],
29
- // Ends with `us`.
30
- ['genus', 'genera'],
31
- ['viscus', 'viscera'],
32
- // Ends with `ma`.
33
- ['stigma', 'stigmata'],
34
- ['stoma', 'stomata'],
35
- ['dogma', 'dogmata'],
36
- ['lemma', 'lemmata'],
37
- ['schema', 'schemata'],
38
- ['anathema', 'anathemata'],
39
- // Other irregular rules.
40
- ['ox', 'oxen'],
41
- ['axe', 'axes'],
42
- ['die', 'dice'],
43
- ['yes', 'yeses'],
44
- ['foot', 'feet'],
45
- ['eave', 'eaves'],
46
- ['goose', 'geese'],
47
- ['tooth', 'teeth'],
48
- ['quiz', 'quizzes'],
49
- ['human', 'humans'],
50
- ['proof', 'proofs'],
51
- ['carve', 'carves'],
52
- ['valve', 'valves'],
53
- ['looey', 'looies'],
54
- ['thief', 'thieves'],
55
- ['groove', 'grooves'],
56
- ['pickaxe', 'pickaxes'],
57
- ['passerby', 'passersby'],
58
- ['canvas', 'canvases'],
59
- ];
@@ -1,40 +0,0 @@
1
- /* eslint-disable no-control-regex */
2
-
3
- import type { Rule } from "../types";
4
-
5
- export const PLURALIZATION_RULES: Rule[] = [
6
- [/s?$/i, "s"],
7
- [/[^\u0000-\u007F]$/, "$0"],
8
- [/([^aeiou]ese)$/i, "$1"],
9
- [/(ax|test)is$/i, "$1es"],
10
- [/(alias|[^aou]us|t[lm]as|gas|ris)$/i, "$1es"],
11
- [/(e[mn]u)s?$/i, "$1s"],
12
- [/([^l]ias|[aeiou]las|[ejzr]as|[iu]am)$/i, "$1"],
13
- [
14
- /(alumn|syllab|vir|radi|nucle|fung|cact|stimul|termin|bacill|foc|uter|loc|strat)(?:us|i)$/i,
15
- "$1i",
16
- ],
17
- [/(alumn|alg|vertebr)(?:a|ae)$/i, "$1ae"],
18
- [/(seraph|cherub)(?:im)?$/i, "$1im"],
19
- [/(her|at|gr)o$/i, "$1oes"],
20
- [
21
- /(agend|addend|millenni|dat|extrem|bacteri|desiderat|strat|candelabr|errat|ov|symposi|curricul|automat|quor)(?:a|um)$/i,
22
- "$1a",
23
- ],
24
- [
25
- /(apheli|hyperbat|periheli|asyndet|noumen|phenomen|criteri|organ|prolegomen|hedr|automat)(?:a|on)$/i,
26
- "$1a",
27
- ],
28
- [/sis$/i, "ses"],
29
- [/(?:(kni|wi|li)fe|(ar|l|ea|eo|oa|hoo)f)$/i, "$1$2ves"],
30
- [/([^aeiouy]|qu)y$/i, "$1ies"],
31
- [/([^ch][ieo][ln])ey$/i, "$1ies"],
32
- [/(x|ch|ss|sh|zz)$/i, "$1es"],
33
- [/(matr|cod|mur|sil|vert|ind|append)(?:ix|ex)$/i, "$1ices"],
34
- [/\b((?:tit)?m|l)(?:ice|ouse)$/i, "$1ice"],
35
- [/(pe)(?:rson|ople)$/i, "$1ople"],
36
- [/(child)(?:ren)?$/i, "$1ren"],
37
- [/eaux$/i, "$0"],
38
- [/m[ae]n$/i, "men"],
39
- ["thou", "you"],
40
- ];