@nyaomaru/divider 1.8.13 → 1.8.15

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
@@ -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 regexCache = /* @__PURE__ */ new Map();
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 key = JSON.stringify(separators);
94
- if (regexCache.has(key)) return regexCache.get(key);
95
- const pattern = separators.reduce((acc, sep) => acc + escapeRegExp(sep), "");
96
- regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
97
- return regexCache.get(key);
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, "\\$&");
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 regexCache = /* @__PURE__ */ new Map();
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 key = JSON.stringify(separators);
64
- if (regexCache.has(key)) return regexCache.get(key);
65
- const pattern = separators.reduce((acc, sep) => acc + escapeRegExp(sep), "");
66
- regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
67
- return regexCache.get(key);
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, "\\$&");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.8.13",
4
+ "version": "1.8.15",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",