@bccampus/utils 0.1.0 → 0.2.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.
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Browser compatible version of `node:url.fileURLToPath`
3
+ */
4
+ export declare function fileURLToPath(fileUrl: string): string;
@@ -0,0 +1,10 @@
1
+ const windowsDriveRegExp = /^\/[a-zA-Z1-9_-]+:\//;
2
+ function fileURLToPath(fileUrl) {
3
+ const url = new URL(fileUrl);
4
+ let path = decodeURIComponent(url.pathname);
5
+ if (windowsDriveRegExp.test(path)) path = path.slice(1);
6
+ return path;
7
+ }
8
+ export {
9
+ fileURLToPath
10
+ };
@@ -0,0 +1,2 @@
1
+ export * from './normalize';
2
+ export * from './file-url-to-path';
@@ -0,0 +1,6 @@
1
+ import { normalize } from "./normalize.js";
2
+ import { fileURLToPath } from "./file-url-to-path.js";
3
+ export {
4
+ fileURLToPath,
5
+ normalize
6
+ };
@@ -0,0 +1,31 @@
1
+ export interface NormalizeOptions {
2
+ /**
3
+ * - `always`: ensure leading slash exists
4
+ * - `never`: ensure leading slash does not exist
5
+ * - `as-is`: do not modify the leading char
6
+ *
7
+ * @default "always"
8
+ */
9
+ leadingSlash?: "always" | "never" | "as-is";
10
+ /**
11
+ * - `always`: ensure trailing slash exists
12
+ * - `never`: ensure trailing slash does not exist
13
+ * - `as-is`: do not modify the trailing char
14
+ *
15
+ * @default "never"
16
+ */
17
+ trailingSlash?: "always" | "never" | "as-is";
18
+ }
19
+ /**
20
+ * Clears and converts the provided path to a Posix filepath
21
+ *
22
+ * By default, adds a leading slash and removes the trailing
23
+ *
24
+ * @example
25
+ * ```
26
+ * normalize("src/test/"); //returns: /src/test
27
+ * normalize("C:\\Users\\test\\folder\\"); //returns: C:/Users/test/folder
28
+ * normalize("file:///C:/Users/test/folder/file.ts"); //returns: C:/Users/test/folder/file.ts
29
+ * ```
30
+ */
31
+ export declare function normalize(path: string, options?: NormalizeOptions): string;
@@ -0,0 +1,32 @@
1
+ import { fileURLToPath } from "./file-url-to-path.js";
2
+ const defaultOptions = {
3
+ leadingSlash: "always",
4
+ trailingSlash: "never"
5
+ };
6
+ const windowsPathSepRegExp = /[\\]+/g;
7
+ const unixPathSepRegExp = /[/]+/g;
8
+ const windowsDriveRegExp = /^[a-zA-Z1-9_-]+:\//;
9
+ const fileProtocolRegExp = /^file:\/\//;
10
+ const leadingSlashRegExp = /^\//g;
11
+ const trailingSlashRegExp = /\/$/g;
12
+ function normalize(path, options) {
13
+ const conf = { ...defaultOptions, ...options };
14
+ const sep = "\\";
15
+ let normalPath = path.trim();
16
+ if (fileProtocolRegExp.test(normalPath))
17
+ normalPath = fileURLToPath(normalPath);
18
+ normalPath = normalPath.replace(windowsPathSepRegExp, sep).replace(unixPathSepRegExp, sep);
19
+ if (conf.leadingSlash !== "as-is") {
20
+ normalPath = normalPath.replace(leadingSlashRegExp, "");
21
+ if (conf.leadingSlash === "always" && !windowsDriveRegExp.test(normalPath))
22
+ normalPath = `${sep}${normalPath}`;
23
+ }
24
+ if (conf.trailingSlash !== "as-is") {
25
+ normalPath = normalPath.replace(trailingSlashRegExp, "");
26
+ if (conf.trailingSlash === "always") normalPath = `${normalPath}${sep}`;
27
+ }
28
+ return normalPath;
29
+ }
30
+ export {
31
+ normalize
32
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Trims white spaces and
3
+ * replaces multiple white space characters with a single space
4
+ */
5
+ export declare function normalize(text: string): string;
@@ -0,0 +1,6 @@
1
+ function normalize(text) {
2
+ return text.trim().replace(/\s+/g, " ");
3
+ }
4
+ export {
5
+ normalize
6
+ };
@@ -1,2 +1,4 @@
1
- export * from './pluralize';
1
+ import { Pluralize } from './pluralize';
2
+ declare const pluralize: Pluralize;
2
3
  export type * from './types';
4
+ export { pluralize, Pluralize };
@@ -1,4 +1,6 @@
1
1
  import { Pluralize } from "./pluralize.js";
2
+ const pluralize = new Pluralize();
2
3
  export {
3
- Pluralize
4
+ Pluralize,
5
+ pluralize
4
6
  };
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bccampus/utils",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "BCcampus' utility box",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -25,6 +25,10 @@
25
25
  "types": "./dist/object/index.d.ts",
26
26
  "import": "./dist/object/index.js"
27
27
  },
28
+ "./path": {
29
+ "types": "./dist/path/index.d.ts",
30
+ "import": "./dist/path/index.js"
31
+ },
28
32
  "./set": {
29
33
  "types": "./dist/set/index.d.ts",
30
34
  "import": "./dist/set/index.js"
@@ -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,2 +1,7 @@
1
- export * from "./pluralize";
2
- export type * from "./types";
1
+ import { Pluralize } from "./pluralize";
2
+
3
+ const pluralize = new Pluralize();
4
+
5
+ export type * from "./types";
6
+
7
+ export { pluralize, Pluralize };
@@ -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
+ }
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
  }
@@ -1 +0,0 @@
1
- {"root":["./src/array/chunk.ts","./src/array/index.ts","./src/date/index.ts","./src/date/iso-date-time.ts","./src/duration/add-durations.ts","./src/duration/index.ts","./src/duration/is-duration.ts","./src/duration/to-number.ts","./src/format/formatter.ts","./src/format/index.ts","./src/format/stringify.ts","./src/format/template.ts","./src/object/get.ts","./src/object/index.ts","./src/object/is-object.ts","./src/object/omit.ts","./src/object/pick.ts","./src/object/set.ts","./src/set/difference.ts","./src/set/index.ts","./src/set/intersection.ts","./src/set/is-superset.ts","./src/set/symmetric-difference.ts","./src/set/union.ts","./src/string/capitalize.ts","./src/string/index.ts","./src/string/strip-html-tags.ts","./src/string/to-int.ts","./src/string/to-sentence-case.ts","./src/string/trim.ts","./src/string/truncate.ts","./src/string/pluralize/index.ts","./src/string/pluralize/pluralize.ts","./src/string/pluralize/types.ts","./src/string/pluralize/rules/index.ts","./src/string/pluralize/rules/irregular.ts","./src/string/pluralize/rules/pluralization.ts","./src/string/pluralize/rules/singularization.ts","./src/string/pluralize/rules/uncountable.ts","./src/string/slugify/index.ts"],"version":"5.9.3"}