@nyaomaru/divider 1.7.4 → 1.8.0

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/README.md CHANGED
@@ -168,20 +168,20 @@ const result3 = dividerNumberString(['abc123', '45z'], { flatten: true });
168
168
 
169
169
  ## 🎯 General Options
170
170
 
171
- | Option | Type | Default | Description |
172
- | -------------- | --------- | ------- | ------------------------------------------------------------------------- |
173
- | `flatten` | `boolean` | `false` | If `true`, the resulting nested arrays are flattened into a single array. |
174
- | `trim` | `boolean` | `false` | If `true`, trims whitespace from each divided segment. |
175
- | `excludeEmpty` | `boolean` | `false` | If `true`, removes empty strings or strings with only whitespace. |
171
+ | Option | Type | Default | Description |
172
+ | --------- | ------------------------------------ | -------- | ------------------------------------------------------------------------- |
173
+ | `flatten` | `boolean` | `false` | If `true`, the resulting nested arrays are flattened into a single array. |
174
+ | `trim` | `boolean` | `false` | If `true`, trims whitespace from each divided segment. |
175
+ | `exclude` | `'none' / 'empty' / 'whitespace'` | `'none'` | See detailed explanation below |
176
176
 
177
177
  ### `flatten` (default: `false`)
178
178
 
179
179
  ```ts
180
180
  const words = ['hello', 'world'];
181
- const result1 = divider(words, 2);
181
+ const result = divider(words, 2);
182
182
  // [['he', 'llo'], ['wo', 'rld']]
183
183
 
184
- const result2 = divider(words, 2, { flatten: true });
184
+ const result = divider(words, 2, { flatten: true });
185
185
  // ['he', 'llo', 'wo', 'rld']
186
186
  ```
187
187
 
@@ -191,24 +191,36 @@ const result2 = divider(words, 2, { flatten: true });
191
191
  const result = divider(' hello world ', 7, { trim: true });
192
192
  // ['hello', 'world']
193
193
 
194
- const result2 = divider([' a ', ' b c '], ' ', {
194
+ const result = divider([' a ', ' b c '], ' ', {
195
195
  flatten: true,
196
196
  trim: true,
197
197
  });
198
198
  // ['a', 'b', 'c']
199
199
  ```
200
200
 
201
- ### `excludeEmpty` (default: `false`)
201
+ ### `exclude` (default: `'none'`)
202
+
203
+ | Option | Description |
204
+ | -------------- | ------------------------------------------------------------------------ |
205
+ | `'none'` | Do not exclude any segments (all results are kept). |
206
+ | `'empty'` | Exclude empty strings (`''`). |
207
+ | `'whitespace'` | Exclude strings that contain only whitespace characters (e.g., `' '`). |
208
+
209
+ Control how segments like empty strings (`''`) or whitespace-only strings (`' '`) are handled.
202
210
 
203
211
  ```ts
204
- // Remove empty strings or strings with only spaces
205
- const result = divider('a, ,b', ',', { trim: true, excludeEmpty: true });
212
+ // Remove truly empty strings
213
+ const result = divider('a,,b', ',', { exclude: 'empty' });
206
214
  // ['a', 'b']
207
215
 
208
- const result2 = divider([' a ', ' ', ' b'], ' ', {
209
- flatten: true,
216
+ // Remove both empty and whitespace-only strings
217
+ const result = divider('a, ,b', ',', { exclude: 'whitespace' });
218
+ // ['a', 'b']
219
+
220
+ // You can combine with `trim` for clearer results
221
+ const result = divider('a, ,b', ',', {
210
222
  trim: true,
211
- excludeEmpty: true,
223
+ exclude: 'whitespace',
212
224
  });
213
225
  // ['a', 'b']
214
226
  ```
package/dist/index.cjs CHANGED
@@ -28,9 +28,6 @@ __export(index_exports, {
28
28
  });
29
29
  module.exports = __toCommonJS(index_exports);
30
30
 
31
- // src/utils/constants.ts
32
- var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
33
-
34
31
  // src/utils/is.ts
35
32
  function isString(arg) {
36
33
  return typeof arg === "string";
@@ -40,7 +37,7 @@ function isNumber(arg) {
40
37
  }
41
38
  function isOptions(arg) {
42
39
  if (typeof arg !== "object" || arg === null) return false;
43
- return dividerOptionKeys.some((key) => key in arg);
40
+ return "flatten" in arg || "trim" in arg || "exclude" in arg;
44
41
  }
45
42
  function isEmptyArray(input) {
46
43
  return Array.isArray(input) && input.length === 0;
@@ -60,6 +57,12 @@ function isNestedStringArray(input) {
60
57
  function isWhitespaceOnly(s) {
61
58
  return s.trim() === "";
62
59
  }
60
+ function isEmptyString(s) {
61
+ return s === "";
62
+ }
63
+ function isNoneMode(mode) {
64
+ return mode === "none";
65
+ }
63
66
 
64
67
  // src/utils/regex.ts
65
68
  var regexCache = /* @__PURE__ */ new Map();
@@ -116,6 +119,13 @@ function ensureStringArray(input) {
116
119
  return isString(input) ? [input] : input;
117
120
  }
118
121
 
122
+ // src/utils/exclude-predicate.ts
123
+ var excludePredicateMap = {
124
+ none: () => true,
125
+ empty: (s) => !isEmptyString(s),
126
+ whitespace: (s) => !isWhitespaceOnly(s)
127
+ };
128
+
119
129
  // src/utils/option.ts
120
130
  function extractOptions(args) {
121
131
  const clonedArgs = [...args];
@@ -138,8 +148,15 @@ function applyDividerOptions(result, options) {
138
148
  if (options.flatten) {
139
149
  output = output.flat();
140
150
  }
141
- if (options.excludeEmpty) {
142
- output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
151
+ if (!isNoneMode(options.exclude)) {
152
+ const exclude = options.exclude ?? "none";
153
+ let shouldKeep = () => true;
154
+ if (exclude in excludePredicateMap) {
155
+ shouldKeep = excludePredicateMap[exclude];
156
+ }
157
+ const filterNested = (arr) => arr.map((row) => row.filter(shouldKeep)).filter((row) => row.length > 0);
158
+ const filterFlat = (arr) => arr.filter(shouldKeep);
159
+ output = isNestedStringArray(output) ? filterNested(output) : filterFlat(output);
143
160
  }
144
161
  return output;
145
162
  }
package/dist/index.d.cts CHANGED
@@ -1,8 +1,10 @@
1
- declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
2
-
3
- type DividerOptionKey = (typeof dividerOptionKeys)[number];
1
+ type DividerExcludeMode = 'none' | 'empty' | 'whitespace';
4
2
  type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
5
- type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
3
+ type DividerOptions = {
4
+ flatten?: boolean;
5
+ trim?: boolean;
6
+ exclude?: DividerExcludeMode;
7
+ };
6
8
  type DividerLoopOptions = DividerOptions & {
7
9
  startOffset?: number;
8
10
  maxChunks?: number;
@@ -20,4 +22,4 @@ declare function dividerLoop<T extends string | string[]>(input: T, size: number
20
22
 
21
23
  declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
22
24
 
23
- export { type DividerArgs, type DividerLoopOptions, type DividerOptionKey, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
25
+ export { type DividerArgs, type DividerExcludeMode, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
- declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
2
-
3
- type DividerOptionKey = (typeof dividerOptionKeys)[number];
1
+ type DividerExcludeMode = 'none' | 'empty' | 'whitespace';
4
2
  type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
5
- type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
3
+ type DividerOptions = {
4
+ flatten?: boolean;
5
+ trim?: boolean;
6
+ exclude?: DividerExcludeMode;
7
+ };
6
8
  type DividerLoopOptions = DividerOptions & {
7
9
  startOffset?: number;
8
10
  maxChunks?: number;
@@ -20,4 +22,4 @@ declare function dividerLoop<T extends string | string[]>(input: T, size: number
20
22
 
21
23
  declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
22
24
 
23
- export { type DividerArgs, type DividerLoopOptions, type DividerOptionKey, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
25
+ export { type DividerArgs, type DividerExcludeMode, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.js CHANGED
@@ -1,6 +1,3 @@
1
- // src/utils/constants.ts
2
- var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
3
-
4
1
  // src/utils/is.ts
5
2
  function isString(arg) {
6
3
  return typeof arg === "string";
@@ -10,7 +7,7 @@ function isNumber(arg) {
10
7
  }
11
8
  function isOptions(arg) {
12
9
  if (typeof arg !== "object" || arg === null) return false;
13
- return dividerOptionKeys.some((key) => key in arg);
10
+ return "flatten" in arg || "trim" in arg || "exclude" in arg;
14
11
  }
15
12
  function isEmptyArray(input) {
16
13
  return Array.isArray(input) && input.length === 0;
@@ -30,6 +27,12 @@ function isNestedStringArray(input) {
30
27
  function isWhitespaceOnly(s) {
31
28
  return s.trim() === "";
32
29
  }
30
+ function isEmptyString(s) {
31
+ return s === "";
32
+ }
33
+ function isNoneMode(mode) {
34
+ return mode === "none";
35
+ }
33
36
 
34
37
  // src/utils/regex.ts
35
38
  var regexCache = /* @__PURE__ */ new Map();
@@ -86,6 +89,13 @@ function ensureStringArray(input) {
86
89
  return isString(input) ? [input] : input;
87
90
  }
88
91
 
92
+ // src/utils/exclude-predicate.ts
93
+ var excludePredicateMap = {
94
+ none: () => true,
95
+ empty: (s) => !isEmptyString(s),
96
+ whitespace: (s) => !isWhitespaceOnly(s)
97
+ };
98
+
89
99
  // src/utils/option.ts
90
100
  function extractOptions(args) {
91
101
  const clonedArgs = [...args];
@@ -108,8 +118,15 @@ function applyDividerOptions(result, options) {
108
118
  if (options.flatten) {
109
119
  output = output.flat();
110
120
  }
111
- if (options.excludeEmpty) {
112
- output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
121
+ if (!isNoneMode(options.exclude)) {
122
+ const exclude = options.exclude ?? "none";
123
+ let shouldKeep = () => true;
124
+ if (exclude in excludePredicateMap) {
125
+ shouldKeep = excludePredicateMap[exclude];
126
+ }
127
+ const filterNested = (arr) => arr.map((row) => row.filter(shouldKeep)).filter((row) => row.length > 0);
128
+ const filterFlat = (arr) => arr.filter(shouldKeep);
129
+ output = isNestedStringArray(output) ? filterNested(output) : filterFlat(output);
113
130
  }
114
131
  return output;
115
132
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.7.4",
4
+ "version": "1.8.0",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",