@bitbeater/ecma-utils 2.7.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/README.md +1 -1
- package/dist/collection/CircularArray.d.ts +1 -1
- package/dist/collection/CircularArray.js +1 -1
- package/dist/strings.d.ts +58 -0
- package/dist/strings.d.ts.map +1 -1
- package/dist/strings.js +101 -0
- package/dist/strings.js.map +1 -1
- package/dist/time/timer.d.ts +1 -1
- package/dist/time/timer.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* A circular array implementation that allows for wrapping around the ends.
|
|
3
3
|
* @example
|
|
4
4
|
* ```ts
|
|
5
|
-
* import { collection } from '
|
|
5
|
+
* import { collection } from '@bitbeater/ecma-utils';
|
|
6
6
|
*
|
|
7
7
|
* const circularArray = new collection.CircularArray<string>(monday, tuesday, wednesday, thursday, friday, saturday, sunday);
|
|
8
8
|
*
|
|
@@ -5,7 +5,7 @@ exports.CircularArray = void 0;
|
|
|
5
5
|
* A circular array implementation that allows for wrapping around the ends.
|
|
6
6
|
* @example
|
|
7
7
|
* ```ts
|
|
8
|
-
* import { collection } from '
|
|
8
|
+
* import { collection } from '@bitbeater/ecma-utils';
|
|
9
9
|
*
|
|
10
10
|
* const circularArray = new collection.CircularArray<string>(monday, tuesday, wednesday, thursday, friday, saturday, sunday);
|
|
11
11
|
*
|
package/dist/strings.d.ts
CHANGED
|
@@ -12,5 +12,63 @@ 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];
|
|
16
|
+
/**
|
|
17
|
+
* Separates text into an array of [whitespace, word] tuples, preserving the whitespace.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* spacedWords('\t Hello \n world ')`
|
|
21
|
+
* // Returns:
|
|
22
|
+
* // [
|
|
23
|
+
* // ['\t ', 'Hello'],
|
|
24
|
+
* // [' \n ', 'world'],
|
|
25
|
+
* // [' ', '']
|
|
26
|
+
* // ]
|
|
27
|
+
* ```
|
|
28
|
+
* @param text
|
|
29
|
+
* @returns [[whitespace, word], ...]
|
|
30
|
+
*/
|
|
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[];
|
|
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;
|
|
15
72
|
export type TemplateExpression = string | number | Array<string | number>;
|
|
73
|
+
export {};
|
|
16
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,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC"}
|
|
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
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isWhitespace = void 0;
|
|
3
4
|
exports.isJson = isJson;
|
|
4
5
|
exports.templateToString = templateToString;
|
|
6
|
+
exports.spacedWords = spacedWords;
|
|
7
|
+
exports.splitRuns = splitRuns;
|
|
8
|
+
exports.isUpperCase = isUpperCase;
|
|
9
|
+
exports.isPunctuation = isPunctuation;
|
|
10
|
+
exports.isNumber = isNumber;
|
|
5
11
|
function isJson(str) {
|
|
6
12
|
try {
|
|
7
13
|
JSON.parse(str);
|
|
@@ -31,4 +37,99 @@ function templateToString(template, ...expressions) {
|
|
|
31
37
|
}
|
|
32
38
|
return merged.join('');
|
|
33
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Separates text into an array of [whitespace, word] tuples, preserving the whitespace.
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* spacedWords('\t Hello \n world ')`
|
|
45
|
+
* // Returns:
|
|
46
|
+
* // [
|
|
47
|
+
* // ['\t ', 'Hello'],
|
|
48
|
+
* // [' \n ', 'world'],
|
|
49
|
+
* // [' ', '']
|
|
50
|
+
* // ]
|
|
51
|
+
* ```
|
|
52
|
+
* @param text
|
|
53
|
+
* @returns [[whitespace, word], ...]
|
|
54
|
+
*/
|
|
55
|
+
function spacedWords(text) {
|
|
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;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
wordBuf.push(ch);
|
|
116
|
+
inDelimiter = false;
|
|
117
|
+
}
|
|
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();
|
|
124
|
+
}
|
|
125
|
+
const isWhitespace = (char) => {
|
|
126
|
+
return /\s/.test(char);
|
|
127
|
+
};
|
|
128
|
+
exports.isWhitespace = isWhitespace;
|
|
129
|
+
function isPunctuation(char) {
|
|
130
|
+
return /[.,\/#!$%\^&\*;:{}=\-_`~()]/.test(char);
|
|
131
|
+
}
|
|
132
|
+
function isNumber(char) {
|
|
133
|
+
return /[0-9]/.test(char);
|
|
134
|
+
}
|
|
34
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":"
|
|
1
|
+
{"version":3,"file":"strings.js","sourceRoot":"","sources":["../src/strings.ts"],"names":[],"mappings":";;;AAAA,wBAOC;AAcD,4CASC;AAmBD,kCAEC;AAsCD,8BAgCC;AAED,kCAEC;AAMD,sCAEC;AAED,4BAEC;AAzID,SAAgB,MAAM,CAAC,GAAW;IACjC,IAAI,CAAC;QACJ,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC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EF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,WAAW,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC;YAC9B,qEAAqE;YACrE,IAAI,WAAW,KAAK,KAAK;gBAAE,KAAK,EAAE,CAAC;YAEnC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACtB,WAAW,GAAG,IAAI,CAAC;YACnB,SAAS;QACV,CAAC;QAED,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACjB,WAAW,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,sEAAsE;IACtE,KAAK,EAAE,CAAC;IACR,OAAO,MAAM,CAAC;AACf,CAAC;AAED,SAAgB,WAAW,CAAC,GAAW;IACtC,OAAO,GAAG,KAAK,GAAG,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC;AAEM,MAAM,YAAY,GAAG,CAAC,IAAY,EAAW,EAAE;IACrD,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEF,SAAgB,aAAa,CAAC,IAAY;IACzC,OAAO,6BAA6B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACjD,CAAC;AAED,SAAgB,QAAQ,CAAC,IAAY;IACpC,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC3B,CAAC"}
|
package/dist/time/timer.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Duration } from './time';
|
|
|
3
3
|
* A simple timer that can be started, paused, and resumed.
|
|
4
4
|
* @example
|
|
5
5
|
* ```js
|
|
6
|
-
* import { Timer } from '
|
|
6
|
+
* import { Timer } from '@bitbeater/ecma-utils/time/timer';
|
|
7
7
|
*
|
|
8
8
|
* const timer = new Timer(5000, (totalDuration) => {
|
|
9
9
|
* console.log(`Timer completed in ${totalDuration} ms`);
|
package/dist/time/timer.js
CHANGED
|
@@ -18,7 +18,7 @@ const time_1 = require("./time");
|
|
|
18
18
|
* A simple timer that can be started, paused, and resumed.
|
|
19
19
|
* @example
|
|
20
20
|
* ```js
|
|
21
|
-
* import { Timer } from '
|
|
21
|
+
* import { Timer } from '@bitbeater/ecma-utils/time/timer';
|
|
22
22
|
*
|
|
23
23
|
* const timer = new Timer(5000, (totalDuration) => {
|
|
24
24
|
* console.log(`Timer completed in ${totalDuration} ms`);
|