@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 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 (!regexCache.has(key)) {
73
- const pattern = separators.reduce(
74
- (acc, sep) => acc + escapeRegExp(sep),
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 (!regexCache.has(key)) {
43
- const pattern = separators.reduce(
44
- (acc, sep) => acc + escapeRegExp(sep),
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);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.8.0",
4
+ "version": "1.8.2",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",