@nyaomaru/divider 2.0.2 → 2.0.4

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 CHANGED
@@ -122,7 +122,10 @@ function isNoneMode(value) {
122
122
  function normalizeSeparators(separators) {
123
123
  return Array.from(new Set(separators)).filter(
124
124
  (separator) => !isEmptyString(separator)
125
- );
125
+ ).sort((left, right) => right.length - left.length);
126
+ }
127
+ function createCacheKey(normalizedSeparators) {
128
+ return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
126
129
  }
127
130
  var RegexCache = class {
128
131
  cache = /* @__PURE__ */ new Map();
@@ -138,6 +141,15 @@ var RegexCache = class {
138
141
  */
139
142
  get(separators) {
140
143
  const key = this.createKey(separators);
144
+ return this.getByKey(key);
145
+ }
146
+ /**
147
+ * Retrieves a cached RegExp for a precomputed cache key.
148
+ *
149
+ * @param key - Cache key string
150
+ * @returns Cached RegExp or null if not found
151
+ */
152
+ getByKey(key) {
141
153
  const regex = this.cache.get(key);
142
154
  if (regex) {
143
155
  this.cache.delete(key);
@@ -153,6 +165,15 @@ var RegexCache = class {
153
165
  */
154
166
  set(separators, regex) {
155
167
  const key = this.createKey(separators);
168
+ this.setByKey(key, regex);
169
+ }
170
+ /**
171
+ * Stores a RegExp in the cache for a precomputed cache key.
172
+ *
173
+ * @param key - Cache key string
174
+ * @param regex - Compiled RegExp to cache
175
+ */
176
+ setByKey(key, regex) {
156
177
  if (this.cache.has(key)) {
157
178
  this.cache.delete(key);
158
179
  }
@@ -173,7 +194,7 @@ var RegexCache = class {
173
194
  */
174
195
  createKey(separators) {
175
196
  const normalizedSeparators = normalizeSeparators(separators);
176
- return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
197
+ return createCacheKey(normalizedSeparators);
177
198
  }
178
199
  /**
179
200
  * Gets current cache size for debugging/monitoring.
@@ -194,15 +215,16 @@ var RegexCache = class {
194
215
  var regexCache = new RegexCache();
195
216
  function getRegex(separators) {
196
217
  if (isEmptyArray(separators)) return null;
197
- const cached = regexCache.get(separators);
198
- if (cached) return cached;
199
218
  const uniqueSeparators = normalizeSeparators(separators);
200
219
  if (uniqueSeparators.length === 0) {
201
220
  return null;
202
221
  }
222
+ const cacheKey = createCacheKey(uniqueSeparators);
223
+ const cached = regexCache.getByKey(cacheKey);
224
+ if (cached) return cached;
203
225
  const pattern = uniqueSeparators.map(escapeRegExp).join("|");
204
226
  const regex = new RegExp(`(?:${pattern})`, "g");
205
- regexCache.set(separators, regex);
227
+ regexCache.setByKey(cacheKey, regex);
206
228
  return regex;
207
229
  }
208
230
  function escapeRegExp(str) {
package/dist/index.js CHANGED
@@ -88,7 +88,10 @@ function isNoneMode(value) {
88
88
  function normalizeSeparators(separators) {
89
89
  return Array.from(new Set(separators)).filter(
90
90
  (separator) => !isEmptyString(separator)
91
- );
91
+ ).sort((left, right) => right.length - left.length);
92
+ }
93
+ function createCacheKey(normalizedSeparators) {
94
+ return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
92
95
  }
93
96
  var RegexCache = class {
94
97
  cache = /* @__PURE__ */ new Map();
@@ -104,6 +107,15 @@ var RegexCache = class {
104
107
  */
105
108
  get(separators) {
106
109
  const key = this.createKey(separators);
110
+ return this.getByKey(key);
111
+ }
112
+ /**
113
+ * Retrieves a cached RegExp for a precomputed cache key.
114
+ *
115
+ * @param key - Cache key string
116
+ * @returns Cached RegExp or null if not found
117
+ */
118
+ getByKey(key) {
107
119
  const regex = this.cache.get(key);
108
120
  if (regex) {
109
121
  this.cache.delete(key);
@@ -119,6 +131,15 @@ var RegexCache = class {
119
131
  */
120
132
  set(separators, regex) {
121
133
  const key = this.createKey(separators);
134
+ this.setByKey(key, regex);
135
+ }
136
+ /**
137
+ * Stores a RegExp in the cache for a precomputed cache key.
138
+ *
139
+ * @param key - Cache key string
140
+ * @param regex - Compiled RegExp to cache
141
+ */
142
+ setByKey(key, regex) {
122
143
  if (this.cache.has(key)) {
123
144
  this.cache.delete(key);
124
145
  }
@@ -139,7 +160,7 @@ var RegexCache = class {
139
160
  */
140
161
  createKey(separators) {
141
162
  const normalizedSeparators = normalizeSeparators(separators);
142
- return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
163
+ return createCacheKey(normalizedSeparators);
143
164
  }
144
165
  /**
145
166
  * Gets current cache size for debugging/monitoring.
@@ -160,15 +181,16 @@ var RegexCache = class {
160
181
  var regexCache = new RegexCache();
161
182
  function getRegex(separators) {
162
183
  if (isEmptyArray(separators)) return null;
163
- const cached = regexCache.get(separators);
164
- if (cached) return cached;
165
184
  const uniqueSeparators = normalizeSeparators(separators);
166
185
  if (uniqueSeparators.length === 0) {
167
186
  return null;
168
187
  }
188
+ const cacheKey = createCacheKey(uniqueSeparators);
189
+ const cached = regexCache.getByKey(cacheKey);
190
+ if (cached) return cached;
169
191
  const pattern = uniqueSeparators.map(escapeRegExp).join("|");
170
192
  const regex = new RegExp(`(?:${pattern})`, "g");
171
- regexCache.set(separators, regex);
193
+ regexCache.setByKey(cacheKey, regex);
172
194
  return regex;
173
195
  }
174
196
  function escapeRegExp(str) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "2.0.2",
4
+ "version": "2.0.4",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",