@bccampus/utils 0.1.1 → 0.2.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.
Files changed (72) hide show
  1. package/dist/array/chunk.cjs +9 -0
  2. package/dist/array/index.cjs +4 -0
  3. package/dist/date/index.cjs +8 -0
  4. package/dist/date/iso-date-time.cjs +256 -0
  5. package/dist/duration/add-durations.cjs +12 -0
  6. package/dist/duration/index.cjs +8 -0
  7. package/dist/duration/is-duration.cjs +7 -0
  8. package/dist/duration/to-number.cjs +14 -0
  9. package/dist/format/formatter.cjs +40 -0
  10. package/dist/format/index.cjs +13 -0
  11. package/dist/format/stringify.cjs +42 -0
  12. package/dist/format/template.cjs +35 -0
  13. package/dist/object/get.cjs +7 -0
  14. package/dist/object/index.cjs +12 -0
  15. package/dist/object/is-object.cjs +4 -0
  16. package/dist/object/omit.cjs +18 -0
  17. package/dist/object/pick.cjs +11 -0
  18. package/dist/object/set.cjs +14 -0
  19. package/dist/path/file-url-to-path.cjs +10 -0
  20. package/dist/path/file-url-to-path.d.ts +4 -0
  21. package/dist/path/file-url-to-path.js +10 -0
  22. package/dist/path/index.cjs +6 -0
  23. package/dist/path/index.d.ts +2 -0
  24. package/dist/path/index.js +6 -0
  25. package/dist/path/normalize.cjs +32 -0
  26. package/dist/path/normalize.d.ts +31 -0
  27. package/dist/path/normalize.js +32 -0
  28. package/dist/set/difference.cjs +10 -0
  29. package/dist/set/index.cjs +12 -0
  30. package/dist/set/intersection.cjs +13 -0
  31. package/dist/set/is-superset.cjs +12 -0
  32. package/dist/set/symmetric-difference.cjs +14 -0
  33. package/dist/set/union.cjs +10 -0
  34. package/dist/string/capitalize.cjs +4 -0
  35. package/dist/string/index.cjs +14 -0
  36. package/dist/string/normalize.cjs +6 -0
  37. package/dist/string/normalize.d.ts +5 -0
  38. package/dist/string/normalize.js +6 -0
  39. package/dist/string/pluralize/index.cjs +6 -0
  40. package/dist/string/pluralize/pluralize.cjs +210 -0
  41. package/dist/string/pluralize/rules/index.cjs +10 -0
  42. package/dist/string/pluralize/rules/irregular.cjs +62 -0
  43. package/dist/string/pluralize/rules/pluralization.cjs +39 -0
  44. package/dist/string/pluralize/rules/singularization.cjs +47 -0
  45. package/dist/string/pluralize/rules/uncountable.cjs +113 -0
  46. package/dist/string/pluralize/types.cjs +1 -0
  47. package/dist/string/slugify/index.cjs +2263 -0
  48. package/dist/string/strip-html-tags.cjs +7 -0
  49. package/dist/string/to-int.cjs +10 -0
  50. package/dist/string/to-sentence-case.cjs +8 -0
  51. package/dist/string/trim.cjs +11 -0
  52. package/dist/string/truncate.cjs +20 -0
  53. package/dist/string/truncate.d.ts +22 -1
  54. package/dist/string/truncate.js +14 -3
  55. package/dist/value/index.cjs +4 -0
  56. package/dist/value/index.d.ts +1 -0
  57. package/dist/value/index.js +4 -0
  58. package/dist/value/is-empty.cjs +14 -0
  59. package/dist/value/is-empty.d.ts +17 -0
  60. package/dist/value/is-empty.js +14 -0
  61. package/package.json +29 -10
  62. package/src/format/index.ts +3 -3
  63. package/src/path/file-url-to-path.ts +12 -0
  64. package/src/path/index.ts +2 -0
  65. package/src/path/normalize.ts +72 -0
  66. package/src/string/normalize.ts +7 -0
  67. package/src/string/truncate.ts +44 -11
  68. package/src/value/index.ts +1 -0
  69. package/src/value/is-empty.ts +31 -0
  70. package/tsconfig.lib.json +36 -35
  71. package/vite.config.ts +75 -77
  72. package/tsconfig.lib.tsbuildinfo +0 -1
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const htmlTagRegExp = /(<([^>]+)>)/g;
4
+ function stripHtmlTags(text) {
5
+ return text.replace(htmlTagRegExp, "");
6
+ }
7
+ exports.stripHtmlTags = stripHtmlTags;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function stringToInt(value, fallback = 0) {
4
+ let returnValue = value;
5
+ if (typeof returnValue === "string")
6
+ returnValue = Number.parseInt(returnValue);
7
+ if (Number.isNaN(returnValue)) returnValue = fallback;
8
+ return returnValue;
9
+ }
10
+ exports.stringToInt = stringToInt;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const specialCharRegExp = /[\s-_.+]+/g;
4
+ const toSentenceCase = (s) => (s[0].toUpperCase() + s.slice(1).toLowerCase()).replace(
5
+ specialCharRegExp,
6
+ " "
7
+ );
8
+ exports.toSentenceCase = toSentenceCase;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function trim(text, chars = " ") {
4
+ let start = 0;
5
+ let end = text.length;
6
+ if (!Array.isArray(chars)) chars = [chars];
7
+ while (start < end && chars.includes(text[start])) start += 1;
8
+ while (end > start && chars.includes(text[end - 1])) end -= 1;
9
+ return start > 0 || end < text.length ? text.substring(start, end) : text;
10
+ }
11
+ exports.trim = trim;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const defaultOptions = {
4
+ ending: "...",
5
+ maxlength: 100,
6
+ noWordBreak: false
7
+ };
8
+ function truncate(text, options) {
9
+ const conf = { ...defaultOptions, ...options };
10
+ text = text.trim();
11
+ if (conf.noWordBreak) {
12
+ const lastSpaceIndex = text.indexOf(" ", conf.maxlength);
13
+ if (lastSpaceIndex >= conf.maxlength)
14
+ return text.substring(0, lastSpaceIndex) + conf.ending;
15
+ } else if (text.length > conf.maxlength + conf.ending.length) {
16
+ return text.substring(0, conf.maxlength) + conf.ending;
17
+ }
18
+ return text;
19
+ }
20
+ exports.truncate = truncate;
@@ -1 +1,22 @@
1
- export declare function truncate(text: string, maxlength?: number, ending?: string): string;
1
+ export interface TruncateOptions {
2
+ /**
3
+ * The maximum number of character allowed in the text before truncating it
4
+ *
5
+ * @default 100
6
+ */
7
+ maxlength?: number;
8
+ /**
9
+ * The string to use at the end of the truncated text
10
+ *
11
+ * @default "..."
12
+ */
13
+ ending?: string;
14
+ /**
15
+ * If true, truncates the text at the next space char,
16
+ * so the returned string can exceed the specified `maxLength`
17
+ *
18
+ * @default false
19
+ */
20
+ noWordBreak?: boolean;
21
+ }
22
+ export declare function truncate(text: string, options?: TruncateOptions): string;
@@ -1,6 +1,17 @@
1
- function truncate(text, maxlength = 100, ending = "...") {
2
- if (text.length > maxlength) {
3
- return text.substring(0, maxlength - ending.length) + ending;
1
+ const defaultOptions = {
2
+ ending: "...",
3
+ maxlength: 100,
4
+ noWordBreak: false
5
+ };
6
+ function truncate(text, options) {
7
+ const conf = { ...defaultOptions, ...options };
8
+ text = text.trim();
9
+ if (conf.noWordBreak) {
10
+ const lastSpaceIndex = text.indexOf(" ", conf.maxlength);
11
+ if (lastSpaceIndex >= conf.maxlength)
12
+ return text.substring(0, lastSpaceIndex) + conf.ending;
13
+ } else if (text.length > conf.maxlength + conf.ending.length) {
14
+ return text.substring(0, conf.maxlength) + conf.ending;
4
15
  }
5
16
  return text;
6
17
  }
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const value_isEmpty = require("./is-empty.cjs");
4
+ exports.isEmpty = value_isEmpty.isEmpty;
@@ -0,0 +1 @@
1
+ export * from './is-empty';
@@ -0,0 +1,4 @@
1
+ import { isEmpty } from "./is-empty.js";
2
+ export {
3
+ isEmpty
4
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const isEmpty = (value) => {
4
+ if (value === void 0) return true;
5
+ if (value === null) return true;
6
+ if (typeof value === "string") return value.trim() === "";
7
+ if (typeof value === "object") {
8
+ if (Array.isArray(value) && value.length === 0) return true;
9
+ if (value instanceof Map || value instanceof Set) return value.size === 0;
10
+ return Object.keys(value).length === 0;
11
+ }
12
+ return false;
13
+ };
14
+ exports.isEmpty = isEmpty;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Checks if the value is `empty`
3
+ *
4
+ * returns `true`, if the value
5
+ *
6
+ * - `undefined`
7
+ * - `null`
8
+ * - `string` and there is no character in the string or all white space
9
+ * - `array` and there is no item in the array
10
+ * - `map` and there is no item in the map
11
+ * - `set` and there is no item in the set
12
+ * - `object` and there is no property in the object
13
+ *
14
+ * otherwise, false
15
+ *
16
+ */
17
+ export declare const isEmpty: (value: unknown) => boolean;
@@ -0,0 +1,14 @@
1
+ const isEmpty = (value) => {
2
+ if (value === void 0) return true;
3
+ if (value === null) return true;
4
+ if (typeof value === "string") return value.trim() === "";
5
+ if (typeof value === "object") {
6
+ if (Array.isArray(value) && value.length === 0) return true;
7
+ if (value instanceof Map || value instanceof Set) return value.size === 0;
8
+ return Object.keys(value).length === 0;
9
+ }
10
+ return false;
11
+ };
12
+ export {
13
+ isEmpty
14
+ };
package/package.json CHANGED
@@ -1,45 +1,64 @@
1
1
  {
2
2
  "name": "@bccampus/utils",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "BCcampus' utility box",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
7
7
  "exports": {
8
8
  "./array": {
9
9
  "types": "./dist/array/index.d.ts",
10
- "import": "./dist/array/index.js"
10
+ "import": "./dist/array/index.js",
11
+ "require": "./dist/array/index.cjs"
11
12
  },
12
13
  "./date": {
13
14
  "types": "./dist/date/index.d.ts",
14
- "import": "./dist/date/index.js"
15
+ "import": "./dist/date/index.js",
16
+ "require": "./dist/date/index.cjs"
15
17
  },
16
18
  "./duration": {
17
19
  "types": "./dist/duration/index.d.ts",
18
- "import": "./dist/duration/index.js"
20
+ "import": "./dist/duration/index.js",
21
+ "require": "./dist/duration/index.cjs"
19
22
  },
20
23
  "./format": {
21
24
  "types": "./dist/format/index.d.ts",
22
- "import": "./dist/format/index.js"
25
+ "import": "./dist/format/index.js",
26
+ "require": "./dist/format/index.cjs"
23
27
  },
24
28
  "./object": {
25
29
  "types": "./dist/object/index.d.ts",
26
- "import": "./dist/object/index.js"
30
+ "import": "./dist/object/index.js",
31
+ "require": "./dist/object/index.cjs"
32
+ },
33
+ "./path": {
34
+ "types": "./dist/path/index.d.ts",
35
+ "import": "./dist/path/index.js",
36
+ "require": "./dist/path/index.cjs"
27
37
  },
28
38
  "./set": {
29
39
  "types": "./dist/set/index.d.ts",
30
- "import": "./dist/set/index.js"
40
+ "import": "./dist/set/index.js",
41
+ "require": "./dist/set/index.cjs"
31
42
  },
32
43
  "./string": {
33
44
  "types": "./dist/string/index.d.ts",
34
- "import": "./dist/string/index.js"
45
+ "import": "./dist/string/index.js",
46
+ "require": "./dist/string/index.cjs"
35
47
  },
36
48
  "./string/pluralize": {
37
49
  "types": "./dist/string/pluralize/index.d.ts",
38
- "import": "./dist/string/pluralize/index.js"
50
+ "import": "./dist/string/pluralize/index.js",
51
+ "require": "./dist/string/pluralize/index.cjs"
39
52
  },
40
53
  "./string/slugify": {
41
54
  "types": "./dist/string/slugify/index.d.ts",
42
- "import": "./dist/string/slugify/index.js"
55
+ "import": "./dist/string/slugify/index.js",
56
+ "require": "./dist/string/slugify/index.cjs"
57
+ },
58
+ "./value": {
59
+ "types": "./dist/value/index.d.ts",
60
+ "import": "./dist/value/index.js",
61
+ "require": "./dist/value/index.cjs"
43
62
  }
44
63
  },
45
64
  "packageManager": "yarn@4.15.0",
@@ -1,3 +1,3 @@
1
- export * from "./formatter";
2
- export * from "./stringify";
3
- export * from "./template";
1
+ export * from "./formatter";
2
+ export * from "./stringify";
3
+ export * from "./template";
@@ -0,0 +1,12 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./normalize";
2
+ export * from "./file-url-to-path";
@@ -0,0 +1,72 @@
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
+ }
@@ -0,0 +1,7 @@
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,11 +1,44 @@
1
- export function truncate(
2
- text: string,
3
- maxlength: number = 100,
4
- ending: string = "...",
5
- ) {
6
- if (text.length > maxlength) {
7
- return text.substring(0, maxlength - ending.length) + ending;
8
- }
9
-
10
- return text;
11
- }
1
+ export interface TruncateOptions {
2
+ /**
3
+ * The maximum number of character allowed in the text before truncating it
4
+ *
5
+ * @default 100
6
+ */
7
+ maxlength?: number;
8
+
9
+ /**
10
+ * The string to use at the end of the truncated text
11
+ *
12
+ * @default "..."
13
+ */
14
+ ending?: string;
15
+
16
+ /**
17
+ * If true, truncates the text at the next space char,
18
+ * so the returned string can exceed the specified `maxLength`
19
+ *
20
+ * @default false
21
+ */
22
+ noWordBreak?: boolean;
23
+ }
24
+
25
+ const defaultOptions: Required<TruncateOptions> = {
26
+ ending: "...",
27
+ maxlength: 100,
28
+ noWordBreak: false,
29
+ };
30
+
31
+ export function truncate(text: string, options?: TruncateOptions) {
32
+ const conf = { ...defaultOptions, ...options };
33
+ text = text.trim();
34
+ if (conf.noWordBreak) {
35
+ const lastSpaceIndex = text.indexOf(" ", conf.maxlength);
36
+
37
+ if (lastSpaceIndex >= conf.maxlength)
38
+ return text.substring(0, lastSpaceIndex) + conf.ending;
39
+ } else if (text.length > conf.maxlength + conf.ending.length) {
40
+ return text.substring(0, conf.maxlength) + conf.ending;
41
+ }
42
+
43
+ return text;
44
+ }
@@ -0,0 +1 @@
1
+ export * from "./is-empty";
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Checks if the value is `empty`
3
+ *
4
+ * returns `true`, if the value
5
+ *
6
+ * - `undefined`
7
+ * - `null`
8
+ * - `string` and there is no character in the string or all white space
9
+ * - `array` and there is no item in the array
10
+ * - `map` and there is no item in the map
11
+ * - `set` and there is no item in the set
12
+ * - `object` and there is no property in the object
13
+ *
14
+ * otherwise, false
15
+ *
16
+ */
17
+ export const isEmpty = (value: unknown) => {
18
+ if (value === undefined) return true;
19
+ if (value === null) return true;
20
+
21
+ if (typeof value === "string") return value.trim() === "";
22
+ if (typeof value === "object") {
23
+ if (Array.isArray(value) && value.length === 0) return true;
24
+
25
+ if (value instanceof Map || value instanceof Set) return value.size === 0;
26
+
27
+ return Object.keys(value).length === 0;
28
+ }
29
+
30
+ return false;
31
+ };
package/tsconfig.lib.json CHANGED
@@ -1,36 +1,37 @@
1
- {
2
- "compilerOptions": {
3
- "module": "ESNext",
4
- "target": "ESNext",
5
- "lib": [
6
- "DOM",
7
- "ESNext"
8
- ],
9
- "strict": true,
10
- "esModuleInterop": true,
11
- "jsx": "preserve",
12
- "skipLibCheck": true,
13
- "moduleResolution": "bundler",
14
- "resolveJsonModule": true,
15
- "noUnusedLocals": true,
16
- "strictNullChecks": true,
17
- "allowJs": true,
18
- "forceConsistentCasingInFileNames": true,
19
- "noEmit": true,
20
- "types": [
21
- "node",
22
- ],
23
- "paths": {
24
- "@/*": [
25
- "./src/*"
26
- ],
27
- }
28
- },
29
- "include": [
30
- "src/**/*",
31
- ],
32
- "exclude": [
33
- "dist",
34
- "node_modules"
35
- ]
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist",
4
+ "module": "ESNext",
5
+ "target": "ESNext",
6
+ "lib": [
7
+ "DOM",
8
+ "ESNext"
9
+ ],
10
+ "strict": true,
11
+ "esModuleInterop": true,
12
+ "jsx": "preserve",
13
+ "skipLibCheck": true,
14
+ "moduleResolution": "bundler",
15
+ "resolveJsonModule": true,
16
+ "noUnusedLocals": true,
17
+ "strictNullChecks": true,
18
+ "allowJs": true,
19
+ "forceConsistentCasingInFileNames": true,
20
+ "noEmit": true,
21
+ "types": [
22
+ "node",
23
+ ],
24
+ "paths": {
25
+ "@/*": [
26
+ "./src/*"
27
+ ],
28
+ }
29
+ },
30
+ "include": [
31
+ "src/**/*",
32
+ ],
33
+ "exclude": [
34
+ "dist",
35
+ "node_modules"
36
+ ]
36
37
  }