@nyaomaru/divider 1.8.0 → 1.8.2
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 +6 -7
- package/dist/index.js +6 -7
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -69,13 +69,9 @@ var regexCache = /* @__PURE__ */ new Map();
|
|
|
69
69
|
function getRegex(separators) {
|
|
70
70
|
if (isEmptyArray(separators)) return null;
|
|
71
71
|
const key = JSON.stringify(separators);
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
""
|
|
76
|
-
);
|
|
77
|
-
regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
|
|
78
|
-
}
|
|
72
|
+
if (regexCache.has(key)) return regexCache.get(key);
|
|
73
|
+
const pattern = separators.reduce((acc, sep) => acc + escapeRegExp(sep), "");
|
|
74
|
+
regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
|
|
79
75
|
return regexCache.get(key);
|
|
80
76
|
}
|
|
81
77
|
function escapeRegExp(str) {
|
|
@@ -108,6 +104,9 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
108
104
|
if (isEmptyArray(numSeparators) && isEmptyArray(strSeparators)) {
|
|
109
105
|
return [input];
|
|
110
106
|
}
|
|
107
|
+
if (!Array.isArray(numSeparators) || !numSeparators.every(isNumber)) {
|
|
108
|
+
throw new Error("Invalid numeric separators");
|
|
109
|
+
}
|
|
111
110
|
const regex = getRegex(strSeparators);
|
|
112
111
|
const sortedNumSeparators = sortAscending(numSeparators);
|
|
113
112
|
const parts = sliceByIndexes(input, sortedNumSeparators);
|
package/dist/index.js
CHANGED
|
@@ -39,13 +39,9 @@ var regexCache = /* @__PURE__ */ new Map();
|
|
|
39
39
|
function getRegex(separators) {
|
|
40
40
|
if (isEmptyArray(separators)) return null;
|
|
41
41
|
const key = JSON.stringify(separators);
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
""
|
|
46
|
-
);
|
|
47
|
-
regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
|
|
48
|
-
}
|
|
42
|
+
if (regexCache.has(key)) return regexCache.get(key);
|
|
43
|
+
const pattern = separators.reduce((acc, sep) => acc + escapeRegExp(sep), "");
|
|
44
|
+
regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
|
|
49
45
|
return regexCache.get(key);
|
|
50
46
|
}
|
|
51
47
|
function escapeRegExp(str) {
|
|
@@ -78,6 +74,9 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
78
74
|
if (isEmptyArray(numSeparators) && isEmptyArray(strSeparators)) {
|
|
79
75
|
return [input];
|
|
80
76
|
}
|
|
77
|
+
if (!Array.isArray(numSeparators) || !numSeparators.every(isNumber)) {
|
|
78
|
+
throw new Error("Invalid numeric separators");
|
|
79
|
+
}
|
|
81
80
|
const regex = getRegex(strSeparators);
|
|
82
81
|
const sortedNumSeparators = sortAscending(numSeparators);
|
|
83
82
|
const parts = sliceByIndexes(input, sortedNumSeparators);
|