@bitbeater/ecma-utils 2.8.0 → 2.9.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/strings.d.ts +42 -2
- package/dist/strings.d.ts.map +1 -1
- package/dist/strings.js +73 -18
- package/dist/strings.js.map +1 -1
- package/package.json +1 -1
package/dist/strings.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export declare function isJson(str: string): boolean;
|
|
|
12
12
|
* @returns
|
|
13
13
|
*/
|
|
14
14
|
export declare function templateToString(template: TemplateStringsArray, ...expressions: TemplateExpression[]): string;
|
|
15
|
+
type SplitTuple = [delimiterRun: string, token: string];
|
|
15
16
|
/**
|
|
16
17
|
* Separates text into an array of [whitespace, word] tuples, preserving the whitespace.
|
|
17
18
|
* @example
|
|
@@ -27,8 +28,47 @@ export declare function templateToString(template: TemplateStringsArray, ...expr
|
|
|
27
28
|
* @param text
|
|
28
29
|
* @returns [[whitespace, word], ...]
|
|
29
30
|
*/
|
|
30
|
-
export declare function spacedWords(text: string): [
|
|
31
|
-
|
|
31
|
+
export declare function spacedWords(text: string): SplitTuple[];
|
|
32
|
+
/**
|
|
33
|
+
* Splits a string into pairs of `[delimiterRun, token]`, preserving contiguous
|
|
34
|
+
* delimiter runs.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} text - The input string to split.
|
|
37
|
+
* @param {(char: string, index: number, text: string) => boolean} isDelimiter
|
|
38
|
+
* Predicate to determine whether a character is a delimiter.
|
|
39
|
+
* @returns {[string, string][]} Array of tuples: `[delimiterRun, token]`.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* // Split on spaces and punctuation
|
|
44
|
+
* const isDelim = (ch) => ch === ' ' || ch === '-' || ch === '!';
|
|
45
|
+
*
|
|
46
|
+
* splitRuns('hello world', isDelim);
|
|
47
|
+
* [
|
|
48
|
+
* ["", "hello"],
|
|
49
|
+
* [" ", "world"],
|
|
50
|
+
* ]
|
|
51
|
+
*
|
|
52
|
+
* splitRuns('hello world!', isDelim);
|
|
53
|
+
* [
|
|
54
|
+
* ["", "hello"],
|
|
55
|
+
* [" ", "world"],
|
|
56
|
+
* ["!", ""]
|
|
57
|
+
* ]
|
|
58
|
+
*
|
|
59
|
+
* splitRuns('- hello world!', isDelim);
|
|
60
|
+
* [
|
|
61
|
+
* ["- ", "hello"],
|
|
62
|
+
* [" ", "world"],
|
|
63
|
+
* ["!", ""]
|
|
64
|
+
* ]
|
|
65
|
+
* ```
|
|
66
|
+
* */
|
|
67
|
+
export declare function splitRuns(text: string, isDelimiter: (char: string, index?: number, text?: string) => boolean): SplitTuple[];
|
|
32
68
|
export declare function isUpperCase(str: string): boolean;
|
|
69
|
+
export declare const isWhitespace: (char: string) => boolean;
|
|
70
|
+
export declare function isPunctuation(char: string): boolean;
|
|
71
|
+
export declare function isNumber(char: string): boolean;
|
|
33
72
|
export type TemplateExpression = string | number | Array<string | number>;
|
|
73
|
+
export {};
|
|
34
74
|
//# sourceMappingURL=strings.d.ts.map
|
package/dist/strings.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../src/strings.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAO3C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,oBAAoB,EAAE,GAAG,WAAW,EAAE,kBAAkB,EAAE,GAAG,MAAM,CAS7G;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../src/strings.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAO3C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,oBAAoB,EAAE,GAAG,WAAW,EAAE,kBAAkB,EAAE,GAAG,MAAM,CAS7G;AAED,KAAK,UAAU,GAAG,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AAExD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,EAAE,CAEtD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAkCK;AAEL,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,KAAK,OAAO,GAAG,UAAU,EAAE,CAgC3H;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,OAE3C,CAAC;AAEF,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEnD;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE9C;AAED,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC"}
|
package/dist/strings.js
CHANGED
|
@@ -4,7 +4,10 @@ exports.isWhitespace = void 0;
|
|
|
4
4
|
exports.isJson = isJson;
|
|
5
5
|
exports.templateToString = templateToString;
|
|
6
6
|
exports.spacedWords = spacedWords;
|
|
7
|
+
exports.splitRuns = splitRuns;
|
|
7
8
|
exports.isUpperCase = isUpperCase;
|
|
9
|
+
exports.isPunctuation = isPunctuation;
|
|
10
|
+
exports.isNumber = isNumber;
|
|
8
11
|
function isJson(str) {
|
|
9
12
|
try {
|
|
10
13
|
JSON.parse(str);
|
|
@@ -50,31 +53,83 @@ function templateToString(template, ...expressions) {
|
|
|
50
53
|
* @returns [[whitespace, word], ...]
|
|
51
54
|
*/
|
|
52
55
|
function spacedWords(text) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
56
|
+
return splitRuns(text, exports.isWhitespace);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Splits a string into pairs of `[delimiterRun, token]`, preserving contiguous
|
|
60
|
+
* delimiter runs.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} text - The input string to split.
|
|
63
|
+
* @param {(char: string, index: number, text: string) => boolean} isDelimiter
|
|
64
|
+
* Predicate to determine whether a character is a delimiter.
|
|
65
|
+
* @returns {[string, string][]} Array of tuples: `[delimiterRun, token]`.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* // Split on spaces and punctuation
|
|
70
|
+
* const isDelim = (ch) => ch === ' ' || ch === '-' || ch === '!';
|
|
71
|
+
*
|
|
72
|
+
* splitRuns('hello world', isDelim);
|
|
73
|
+
* [
|
|
74
|
+
* ["", "hello"],
|
|
75
|
+
* [" ", "world"],
|
|
76
|
+
* ]
|
|
77
|
+
*
|
|
78
|
+
* splitRuns('hello world!', isDelim);
|
|
79
|
+
* [
|
|
80
|
+
* ["", "hello"],
|
|
81
|
+
* [" ", "world"],
|
|
82
|
+
* ["!", ""]
|
|
83
|
+
* ]
|
|
84
|
+
*
|
|
85
|
+
* splitRuns('- hello world!', isDelim);
|
|
86
|
+
* [
|
|
87
|
+
* ["- ", "hello"],
|
|
88
|
+
* [" ", "world"],
|
|
89
|
+
* ["!", ""]
|
|
90
|
+
* ]
|
|
91
|
+
* ```
|
|
92
|
+
* */
|
|
93
|
+
function splitRuns(text, isDelimiter) {
|
|
94
|
+
const result = [];
|
|
95
|
+
if (!text)
|
|
96
|
+
return result;
|
|
97
|
+
let inDelimiter = null;
|
|
98
|
+
const delimiterBuf = [];
|
|
99
|
+
const wordBuf = [];
|
|
100
|
+
const flush = () => {
|
|
101
|
+
result.push([delimiterBuf.join(''), wordBuf.join('')]);
|
|
102
|
+
delimiterBuf.length = 0;
|
|
103
|
+
wordBuf.length = 0;
|
|
104
|
+
};
|
|
105
|
+
for (let i = 0; i < text.length; i++) {
|
|
106
|
+
const ch = text[i];
|
|
107
|
+
if (isDelimiter(ch, i, text)) {
|
|
108
|
+
// entering a delimiter run after a word => flush the word+match pair
|
|
109
|
+
if (inDelimiter === false)
|
|
110
|
+
flush();
|
|
111
|
+
delimiterBuf.push(ch);
|
|
112
|
+
inDelimiter = true;
|
|
66
113
|
continue;
|
|
67
114
|
}
|
|
68
|
-
|
|
69
|
-
|
|
115
|
+
wordBuf.push(ch);
|
|
116
|
+
inDelimiter = false;
|
|
70
117
|
}
|
|
71
|
-
|
|
118
|
+
// flush trailing tuple (captures trailing delimiter run or last word)
|
|
119
|
+
flush();
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
function isUpperCase(str) {
|
|
123
|
+
return str === str.toUpperCase();
|
|
72
124
|
}
|
|
73
125
|
const isWhitespace = (char) => {
|
|
74
126
|
return /\s/.test(char);
|
|
75
127
|
};
|
|
76
128
|
exports.isWhitespace = isWhitespace;
|
|
77
|
-
function
|
|
78
|
-
return
|
|
129
|
+
function isPunctuation(char) {
|
|
130
|
+
return /[.,\/#!$%\^&\*;:{}=\-_`~()]/.test(char);
|
|
131
|
+
}
|
|
132
|
+
function isNumber(char) {
|
|
133
|
+
return /[0-9]/.test(char);
|
|
79
134
|
}
|
|
80
135
|
//# sourceMappingURL=strings.js.map
|
package/dist/strings.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strings.js","sourceRoot":"","sources":["../src/strings.ts"],"names":[],"mappings":";;;AAAA,wBAO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