@nyaomaru/divider 1.8.14 → 1.8.16
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/index.cjs +86 -7
- package/dist/index.js +86 -7
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -45,6 +45,7 @@ var PERFORMANCE_CONSTANTS = {
|
|
|
45
45
|
/** Default max chunks for dividerLoop (0 = no limit) */
|
|
46
46
|
DEFAULT_MAX_CHUNKS: 0
|
|
47
47
|
};
|
|
48
|
+
var CACHE_KEY_SEPARATOR = "\0";
|
|
48
49
|
|
|
49
50
|
// src/utils/is.ts
|
|
50
51
|
function isString(arg) {
|
|
@@ -87,14 +88,90 @@ function isNoneMode(mode) {
|
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
// src/utils/regex.ts
|
|
90
|
-
var
|
|
91
|
+
var RegexCache = class {
|
|
92
|
+
cache = /* @__PURE__ */ new Map();
|
|
93
|
+
maxSize;
|
|
94
|
+
constructor(maxSize = PERFORMANCE_CONSTANTS.MAX_REGEX_CACHE_SIZE) {
|
|
95
|
+
this.maxSize = maxSize;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Retrieves a cached RegExp for the given separators.
|
|
99
|
+
*
|
|
100
|
+
* @param separators - Array of string separators
|
|
101
|
+
* @returns Cached RegExp or null if not found
|
|
102
|
+
*/
|
|
103
|
+
get(separators) {
|
|
104
|
+
const key = this.createKey(separators);
|
|
105
|
+
const regex = this.cache.get(key);
|
|
106
|
+
if (regex) {
|
|
107
|
+
this.cache.delete(key);
|
|
108
|
+
this.cache.set(key, regex);
|
|
109
|
+
}
|
|
110
|
+
return regex || null;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Stores a RegExp in the cache with LRU eviction if needed.
|
|
114
|
+
*
|
|
115
|
+
* @param separators - Array of string separators
|
|
116
|
+
* @param regex - Compiled RegExp to cache
|
|
117
|
+
*/
|
|
118
|
+
set(separators, regex) {
|
|
119
|
+
const key = this.createKey(separators);
|
|
120
|
+
if (this.cache.has(key)) {
|
|
121
|
+
this.cache.delete(key);
|
|
122
|
+
}
|
|
123
|
+
if (this.cache.size >= this.maxSize) {
|
|
124
|
+
const firstKey = this.cache.keys().next().value;
|
|
125
|
+
if (firstKey !== void 0) {
|
|
126
|
+
this.cache.delete(firstKey);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
this.cache.set(key, regex);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Creates a cache key from separators array.
|
|
133
|
+
* More efficient than JSON.stringify for large arrays.
|
|
134
|
+
*
|
|
135
|
+
* @param separators - Array of string separators
|
|
136
|
+
* @returns Cache key string
|
|
137
|
+
*/
|
|
138
|
+
createKey(separators) {
|
|
139
|
+
const normalizedSeparators = Array.from(new Set(separators)).filter(
|
|
140
|
+
(separator) => separator !== ""
|
|
141
|
+
);
|
|
142
|
+
return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Gets current cache size for debugging/monitoring.
|
|
146
|
+
*
|
|
147
|
+
* @returns Number of cached entries
|
|
148
|
+
*/
|
|
149
|
+
get size() {
|
|
150
|
+
return this.cache.size;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Clears all cached entries.
|
|
154
|
+
* Useful for testing or memory management.
|
|
155
|
+
*/
|
|
156
|
+
clear() {
|
|
157
|
+
this.cache.clear();
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var regexCache = new RegexCache();
|
|
91
161
|
function getRegex(separators) {
|
|
92
162
|
if (isEmptyArray(separators)) return null;
|
|
93
|
-
const
|
|
94
|
-
if (
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
163
|
+
const cached = regexCache.get(separators);
|
|
164
|
+
if (cached) return cached;
|
|
165
|
+
const uniqueSeparators = Array.from(new Set(separators)).filter(
|
|
166
|
+
(separator) => separator !== ""
|
|
167
|
+
);
|
|
168
|
+
if (uniqueSeparators.length === 0) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const pattern = uniqueSeparators.map(escapeRegExp).join("|");
|
|
172
|
+
const regex = new RegExp(`(?:${pattern})`, "g");
|
|
173
|
+
regexCache.set(separators, regex);
|
|
174
|
+
return regex;
|
|
98
175
|
}
|
|
99
176
|
function escapeRegExp(str) {
|
|
100
177
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -269,7 +346,9 @@ function dividerLoop(input, size, options) {
|
|
|
269
346
|
startOffset = PERFORMANCE_CONSTANTS.DEFAULT_START_OFFSET,
|
|
270
347
|
maxChunks = PERFORMANCE_CONSTANTS.DEFAULT_MAX_CHUNKS
|
|
271
348
|
} = finalOptions;
|
|
272
|
-
const result = isString(input) ? createChunksFromString(input, size, startOffset, maxChunks) : input.map(
|
|
349
|
+
const result = isString(input) ? createChunksFromString(input, size, startOffset, maxChunks) : input.map(
|
|
350
|
+
(str) => createChunksFromString(str, size, startOffset, maxChunks)
|
|
351
|
+
);
|
|
273
352
|
return applyDividerOptions(result, finalOptions);
|
|
274
353
|
}
|
|
275
354
|
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var PERFORMANCE_CONSTANTS = {
|
|
|
15
15
|
/** Default max chunks for dividerLoop (0 = no limit) */
|
|
16
16
|
DEFAULT_MAX_CHUNKS: 0
|
|
17
17
|
};
|
|
18
|
+
var CACHE_KEY_SEPARATOR = "\0";
|
|
18
19
|
|
|
19
20
|
// src/utils/is.ts
|
|
20
21
|
function isString(arg) {
|
|
@@ -57,14 +58,90 @@ function isNoneMode(mode) {
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
// src/utils/regex.ts
|
|
60
|
-
var
|
|
61
|
+
var RegexCache = class {
|
|
62
|
+
cache = /* @__PURE__ */ new Map();
|
|
63
|
+
maxSize;
|
|
64
|
+
constructor(maxSize = PERFORMANCE_CONSTANTS.MAX_REGEX_CACHE_SIZE) {
|
|
65
|
+
this.maxSize = maxSize;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves a cached RegExp for the given separators.
|
|
69
|
+
*
|
|
70
|
+
* @param separators - Array of string separators
|
|
71
|
+
* @returns Cached RegExp or null if not found
|
|
72
|
+
*/
|
|
73
|
+
get(separators) {
|
|
74
|
+
const key = this.createKey(separators);
|
|
75
|
+
const regex = this.cache.get(key);
|
|
76
|
+
if (regex) {
|
|
77
|
+
this.cache.delete(key);
|
|
78
|
+
this.cache.set(key, regex);
|
|
79
|
+
}
|
|
80
|
+
return regex || null;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Stores a RegExp in the cache with LRU eviction if needed.
|
|
84
|
+
*
|
|
85
|
+
* @param separators - Array of string separators
|
|
86
|
+
* @param regex - Compiled RegExp to cache
|
|
87
|
+
*/
|
|
88
|
+
set(separators, regex) {
|
|
89
|
+
const key = this.createKey(separators);
|
|
90
|
+
if (this.cache.has(key)) {
|
|
91
|
+
this.cache.delete(key);
|
|
92
|
+
}
|
|
93
|
+
if (this.cache.size >= this.maxSize) {
|
|
94
|
+
const firstKey = this.cache.keys().next().value;
|
|
95
|
+
if (firstKey !== void 0) {
|
|
96
|
+
this.cache.delete(firstKey);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
this.cache.set(key, regex);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Creates a cache key from separators array.
|
|
103
|
+
* More efficient than JSON.stringify for large arrays.
|
|
104
|
+
*
|
|
105
|
+
* @param separators - Array of string separators
|
|
106
|
+
* @returns Cache key string
|
|
107
|
+
*/
|
|
108
|
+
createKey(separators) {
|
|
109
|
+
const normalizedSeparators = Array.from(new Set(separators)).filter(
|
|
110
|
+
(separator) => separator !== ""
|
|
111
|
+
);
|
|
112
|
+
return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Gets current cache size for debugging/monitoring.
|
|
116
|
+
*
|
|
117
|
+
* @returns Number of cached entries
|
|
118
|
+
*/
|
|
119
|
+
get size() {
|
|
120
|
+
return this.cache.size;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Clears all cached entries.
|
|
124
|
+
* Useful for testing or memory management.
|
|
125
|
+
*/
|
|
126
|
+
clear() {
|
|
127
|
+
this.cache.clear();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
var regexCache = new RegexCache();
|
|
61
131
|
function getRegex(separators) {
|
|
62
132
|
if (isEmptyArray(separators)) return null;
|
|
63
|
-
const
|
|
64
|
-
if (
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
133
|
+
const cached = regexCache.get(separators);
|
|
134
|
+
if (cached) return cached;
|
|
135
|
+
const uniqueSeparators = Array.from(new Set(separators)).filter(
|
|
136
|
+
(separator) => separator !== ""
|
|
137
|
+
);
|
|
138
|
+
if (uniqueSeparators.length === 0) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const pattern = uniqueSeparators.map(escapeRegExp).join("|");
|
|
142
|
+
const regex = new RegExp(`(?:${pattern})`, "g");
|
|
143
|
+
regexCache.set(separators, regex);
|
|
144
|
+
return regex;
|
|
68
145
|
}
|
|
69
146
|
function escapeRegExp(str) {
|
|
70
147
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -239,7 +316,9 @@ function dividerLoop(input, size, options) {
|
|
|
239
316
|
startOffset = PERFORMANCE_CONSTANTS.DEFAULT_START_OFFSET,
|
|
240
317
|
maxChunks = PERFORMANCE_CONSTANTS.DEFAULT_MAX_CHUNKS
|
|
241
318
|
} = finalOptions;
|
|
242
|
-
const result = isString(input) ? createChunksFromString(input, size, startOffset, maxChunks) : input.map(
|
|
319
|
+
const result = isString(input) ? createChunksFromString(input, size, startOffset, maxChunks) : input.map(
|
|
320
|
+
(str) => createChunksFromString(str, size, startOffset, maxChunks)
|
|
321
|
+
);
|
|
243
322
|
return applyDividerOptions(result, finalOptions);
|
|
244
323
|
}
|
|
245
324
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nyaomaru/divider",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.8.
|
|
4
|
+
"version": "1.8.16",
|
|
5
5
|
"description": "To divide string or string[] with a given separator",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -36,12 +36,12 @@
|
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@eslint/js": "^9.26.0",
|
|
39
|
-
"@types/jest": "^
|
|
39
|
+
"@types/jest": "^30.0.0",
|
|
40
40
|
"@types/node": "^22.15.12",
|
|
41
41
|
"eslint": "^9.26.0",
|
|
42
42
|
"eslint-config-prettier": "^10.1.2",
|
|
43
43
|
"eslint-plugin-prettier": "^5.4.0",
|
|
44
|
-
"jest": "^
|
|
44
|
+
"jest": "^30.0.0",
|
|
45
45
|
"prettier": "^3.5.3",
|
|
46
46
|
"ts-jest": "^29.3.2",
|
|
47
47
|
"ts-node": "^10.9.2",
|