@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.
- package/dist/path/file-url-to-path.d.ts +4 -0
- package/dist/path/file-url-to-path.js +10 -0
- package/dist/path/index.d.ts +2 -0
- package/dist/path/index.js +6 -0
- package/dist/path/normalize.d.ts +31 -0
- package/dist/path/normalize.js +32 -0
- package/dist/string/normalize.d.ts +5 -0
- package/dist/string/normalize.js +6 -0
- package/dist/string/pluralize/index.d.ts +3 -1
- package/dist/string/pluralize/index.js +3 -1
- package/dist/string/truncate.d.ts +22 -1
- package/dist/string/truncate.js +14 -3
- package/package.json +5 -1
- package/src/path/file-url-to-path.ts +12 -0
- package/src/path/index.ts +2 -0
- package/src/path/normalize.ts +72 -0
- package/src/string/normalize.ts +7 -0
- package/src/string/pluralize/index.ts +7 -2
- package/src/string/truncate.ts +44 -11
- package/tsconfig.lib.json +36 -35
- package/tsconfig.lib.tsbuildinfo +0 -1
|
@@ -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,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
|
+
};
|
|
@@ -1 +1,22 @@
|
|
|
1
|
-
export
|
|
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;
|
package/dist/string/truncate.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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.
|
|
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,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
|
+
}
|
package/src/string/truncate.ts
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
"
|
|
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
|
-
|
|
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
|
}
|
package/tsconfig.lib.tsbuildinfo
DELETED
|
@@ -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"}
|