@augment-vir/common 31.39.0 → 31.41.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,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sanitize a file path for use within Linux, macOS, or Windows file systems.
|
|
3
|
+
*
|
|
4
|
+
* @category Path : Common
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export declare function sanitizeFilePath(original: string | null | undefined): string | undefined;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/* eslint-disable unicorn/prefer-code-point */
|
|
2
|
+
/* eslint-disable sonarjs/no-control-regex */
|
|
3
|
+
/* eslint-disable no-control-regex */
|
|
4
|
+
import { getByteLength } from '../string/length.js';
|
|
5
|
+
import { safeSplit } from '../string/split.js';
|
|
6
|
+
import { collapseWhiteSpace } from '../string/white-space.js';
|
|
7
|
+
import { extractExtension } from './universal-path.js';
|
|
8
|
+
/**
|
|
9
|
+
* Sanitize a file path for use within Linux, macOS, or Windows file systems.
|
|
10
|
+
*
|
|
11
|
+
* @category Path : Common
|
|
12
|
+
* @category Package : @augment-vir/common
|
|
13
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
14
|
+
*/
|
|
15
|
+
export function sanitizeFilePath(original) {
|
|
16
|
+
if (!original) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const sanitized = sanitizeFileName(collapseWhiteSpace(original)
|
|
20
|
+
.replaceAll(' ', '_')
|
|
21
|
+
/** This ESLint error is wrong. */
|
|
22
|
+
.replaceAll(/['()*"![\]{}\s?=&<>:/\-\\|]/g, '_')
|
|
23
|
+
.replaceAll(/_{2,}/g, '_')
|
|
24
|
+
.replace(/_$/, '')
|
|
25
|
+
.replace(/\.$/, '')
|
|
26
|
+
.replaceAll(/_\./g, '.')
|
|
27
|
+
.replace(/^_+/, '')
|
|
28
|
+
.replace(/^\.+/, '')
|
|
29
|
+
.toLowerCase()).trim();
|
|
30
|
+
const extension = extractExtension(sanitized).extension;
|
|
31
|
+
if (extension) {
|
|
32
|
+
return [
|
|
33
|
+
safeSplit(sanitized, extension)[0],
|
|
34
|
+
extension.replaceAll('_', ''),
|
|
35
|
+
].join('');
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return sanitized || undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** The following code is not written by us, we do not care about code coverage on it. */
|
|
42
|
+
/* node:coverage disable */
|
|
43
|
+
/**
|
|
44
|
+
* The following code is copied from the
|
|
45
|
+
* [`sanitize-filename`](https://www.npmjs.com/package/sanitize-filename/v/1.6.3) package at
|
|
46
|
+
* https://github.com/parshap/node-sanitize-filename/blob/88bba56da0bad941ea5c8302983dc66af331c7e9/index.js
|
|
47
|
+
* and modified to match our conventions and make it browser friendly.
|
|
48
|
+
*/
|
|
49
|
+
const illegalRe = /[/?<>\\:*|"]/g;
|
|
50
|
+
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
|
|
51
|
+
const reservedRe = /^\.+$/;
|
|
52
|
+
const windowsReservedRe = /^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$/i;
|
|
53
|
+
function internalSanitizeFileName(input, replacement) {
|
|
54
|
+
/**
|
|
55
|
+
* These as casts are necessary because `.replace` is not typed correctly in the TypeScript
|
|
56
|
+
* library.
|
|
57
|
+
*
|
|
58
|
+
* @see https://github.com/microsoft/TypeScript/issues/54387
|
|
59
|
+
*/
|
|
60
|
+
const sanitized = input
|
|
61
|
+
.replace(illegalRe, replacement)
|
|
62
|
+
.replace(controlRe, replacement)
|
|
63
|
+
.replace(reservedRe, replacement)
|
|
64
|
+
.replace(windowsReservedRe, replacement);
|
|
65
|
+
return truncate(sanitized, 255);
|
|
66
|
+
}
|
|
67
|
+
function sanitizeFileName(input, { replacement, } = {}) {
|
|
68
|
+
return internalSanitizeFileName(internalSanitizeFileName(input, replacement || ''), '');
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The following code is copied from the
|
|
72
|
+
* [`truncate-utf8-bytes`](https://www.npmjs.com/package/truncate-utf8-bytes/v/1.0.2) package at
|
|
73
|
+
* https://github.com/parshap/truncate-utf8-bytes/blob/c8fcebc8be093c8bd8db1e7d75c09b9fce7e4708/lib/truncate.js
|
|
74
|
+
* and modified to match our conventions and make it browser friendly.
|
|
75
|
+
*/
|
|
76
|
+
function isHighSurrogate(charCode) {
|
|
77
|
+
return charCode >= 0xd8_00 && charCode <= 0xdb_ff;
|
|
78
|
+
}
|
|
79
|
+
function isLowSurrogate(charCode) {
|
|
80
|
+
return charCode >= 0xdc_00 && charCode <= 0xdf_ff;
|
|
81
|
+
}
|
|
82
|
+
function truncate(string, byteLength) {
|
|
83
|
+
const charLength = string.length;
|
|
84
|
+
let curByteLength = 0;
|
|
85
|
+
let codePoint;
|
|
86
|
+
let segment;
|
|
87
|
+
for (let i = 0; i < charLength; i += 1) {
|
|
88
|
+
codePoint = string.charCodeAt(i);
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
90
|
+
segment = string[i];
|
|
91
|
+
if (isHighSurrogate(codePoint) && isLowSurrogate(string.charCodeAt(i + 1))) {
|
|
92
|
+
// eslint-disable-next-line sonarjs/updated-loop-counter
|
|
93
|
+
i += 1;
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
95
|
+
segment += string[i];
|
|
96
|
+
}
|
|
97
|
+
curByteLength += getByteLength(segment);
|
|
98
|
+
if (curByteLength === byteLength) {
|
|
99
|
+
return string.slice(0, i + 1);
|
|
100
|
+
}
|
|
101
|
+
else if (curByteLength > byteLength) {
|
|
102
|
+
return string.slice(0, i - segment.length + 1);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return string;
|
|
106
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a string's length in bytes.
|
|
3
|
+
*
|
|
4
|
+
* @category String
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {getStringByteLength} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* const results = getStringByteLength('hello 🌍'); // 10
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
15
|
+
*/
|
|
16
|
+
export declare function getByteLength(value: string): number;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a string's length in bytes.
|
|
3
|
+
*
|
|
4
|
+
* @category String
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {getStringByteLength} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* const results = getStringByteLength('hello 🌍'); // 10
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
15
|
+
*/
|
|
16
|
+
export function getByteLength(value) {
|
|
17
|
+
const byteLength = new TextEncoder().encode(value).length;
|
|
18
|
+
return byteLength;
|
|
19
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ export * from './augments/object/object-keys.js';
|
|
|
59
59
|
export * from './augments/object/object-sort.js';
|
|
60
60
|
export * from './augments/object/object-values.js';
|
|
61
61
|
export * from './augments/path/esm-path.js';
|
|
62
|
+
export * from './augments/path/sanitize-path.js';
|
|
62
63
|
export * from './augments/path/universal-path.js';
|
|
63
64
|
export * from './augments/prisma/base-prisma-types.js';
|
|
64
65
|
export * from './augments/prisma/prisma-basic-model.js';
|
|
@@ -81,6 +82,7 @@ export * from './augments/string/casing/casing.js';
|
|
|
81
82
|
export * from './augments/string/casing/kebab-and-camel.js';
|
|
82
83
|
export * from './augments/string/comma.js';
|
|
83
84
|
export * from './augments/string/join.js';
|
|
85
|
+
export * from './augments/string/length.js';
|
|
84
86
|
export * from './augments/string/prefix.js';
|
|
85
87
|
export * from './augments/string/replace.js';
|
|
86
88
|
export * from './augments/string/split.js';
|
package/dist/index.js
CHANGED
|
@@ -59,6 +59,7 @@ export * from './augments/object/object-keys.js';
|
|
|
59
59
|
export * from './augments/object/object-sort.js';
|
|
60
60
|
export * from './augments/object/object-values.js';
|
|
61
61
|
export * from './augments/path/esm-path.js';
|
|
62
|
+
export * from './augments/path/sanitize-path.js';
|
|
62
63
|
export * from './augments/path/universal-path.js';
|
|
63
64
|
export * from './augments/prisma/base-prisma-types.js';
|
|
64
65
|
export * from './augments/prisma/prisma-basic-model.js';
|
|
@@ -81,6 +82,7 @@ export * from './augments/string/casing/casing.js';
|
|
|
81
82
|
export * from './augments/string/casing/kebab-and-camel.js';
|
|
82
83
|
export * from './augments/string/comma.js';
|
|
83
84
|
export * from './augments/string/join.js';
|
|
85
|
+
export * from './augments/string/length.js';
|
|
84
86
|
export * from './augments/string/prefix.js';
|
|
85
87
|
export * from './augments/string/replace.js';
|
|
86
88
|
export * from './augments/string/split.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.41.0",
|
|
4
4
|
"description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"augment",
|
|
@@ -40,13 +40,13 @@
|
|
|
40
40
|
"test:web": "virmator --no-deps test web"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@augment-vir/assert": "^31.
|
|
44
|
-
"@augment-vir/core": "^31.
|
|
45
|
-
"@date-vir/duration": "^7.4.
|
|
43
|
+
"@augment-vir/assert": "^31.41.0",
|
|
44
|
+
"@augment-vir/core": "^31.41.0",
|
|
45
|
+
"@date-vir/duration": "^7.4.3",
|
|
46
46
|
"ansi-styles": "^6.2.3",
|
|
47
47
|
"deepcopy-esm": "^2.1.1",
|
|
48
48
|
"json5": "^2.2.3",
|
|
49
|
-
"type-fest": "^5.0
|
|
49
|
+
"type-fest": "^5.1.0",
|
|
50
50
|
"typed-event-target": "^4.1.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|