@nyaomaru/divider 1.6.2 โ†’ 1.7.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
@@ -152,10 +152,11 @@ const result3 = dividerNumberString(['abc123', '45z'], { flatten: true });
152
152
 
153
153
  ## ๐ŸŽฏ Options
154
154
 
155
- | Option | Type | Default | Description |
156
- | --------- | --------- | ------- | ------------------------------------------------------------------------- |
157
- | `flatten` | `boolean` | `false` | If `true`, the resulting nested arrays are flattened into a single array. |
158
- | `trim` | `boolean` | `false` | If `true`, trims whitespace from each divided segment. |
155
+ | Option | Type | Default | Description |
156
+ | -------------- | ----------| ------- | ------------------------------------------------------------------------- |
157
+ | `flatten` | `boolean` | `false` | If `true`, the resulting nested arrays are flattened into a single array. |
158
+ | `trim` | `boolean` | `false` | If `true`, trims whitespace from each divided segment. |
159
+ | `excludeEmpty` | `boolean` | `false` | If `true`, removes empty strings or strings with only whitespace. |
159
160
 
160
161
  ### `flatten` (default: `false`)
161
162
 
@@ -181,24 +182,29 @@ const result2 = divider([' a ', ' b c '], ' ', {
181
182
  // ['a', 'b', 'c']
182
183
  ```
183
184
 
184
- ## ๐Ÿ’ก Features
185
-
186
- ### ๐Ÿงฉ Flexible Division
187
-
188
- - Supports both `index-based` and `string-based` division
189
- - Supports `multiple separators` (mixing indexes and characters)
190
- - Works with both `string` and `string[]` input
191
- - Optional `flatten` and `trim` behaviors to control output format
192
- - Includes `dividerNumberString()` to separate digits and letters
185
+ ### `excludeEmpty` (default: `false`)
193
186
 
194
- ### ๐ŸŽฏ Targeted Extraction
187
+ ```ts
188
+ // Remove empty strings or strings with only spaces
189
+ const result = divider('a, ,b', ',', { trim: true, excludeEmpty: true });
190
+ // ['a', 'b']
195
191
 
196
- - `dividerFirst()`: Get only the first divided element
197
- - `dividerLast()`: Get only the last divided element
192
+ const result2 = divider([' a ', ' ', ' b'], ' ', {
193
+ flatten: true,
194
+ trim: true,
195
+ excludeEmpty: true,
196
+ });
197
+ // ['a', 'b']
198
+ ```
198
199
 
199
- ### ๐Ÿ” Repeated Division
200
+ ## ๐Ÿ’ก Features
200
201
 
201
- - `dividerLoop()`: Automatically divide into fixed-size chunks
202
+ - ๐Ÿงฉ Flexible Division: Index-based and string-based separators
203
+ - ๐Ÿงต Handles Nested Input: Supports both string and string[]
204
+ - ๐ŸŽ›๏ธ Optional Behaviors: flatten, trim, excludeEmpty
205
+ - ๐ŸŽฏ Targeted Extractors: dividerFirst(), dividerLast()
206
+ - ๐Ÿ” Loop Support: dividerLoop() for chunked division
207
+ - ๐Ÿ”ข Digit-Letter Splitter: dividerNumberString()
202
208
 
203
209
  ## ๐Ÿ›  Contributing
204
210
 
package/dist/index.cjs CHANGED
@@ -28,11 +28,19 @@ __export(index_exports, {
28
28
  });
29
29
  module.exports = __toCommonJS(index_exports);
30
30
 
31
+ // src/core/constants.ts
32
+ var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
33
+
31
34
  // src/utils/is.ts
32
- var isString = (arg) => typeof arg === "string";
33
- var isNumber = (arg) => typeof arg === "number";
35
+ function isString(arg) {
36
+ return typeof arg === "string";
37
+ }
38
+ function isNumber(arg) {
39
+ return typeof arg === "number";
40
+ }
34
41
  function isOptions(arg) {
35
- return typeof arg === "object" && arg !== null && "flatten" in arg;
42
+ if (typeof arg !== "object" || arg === null) return false;
43
+ return dividerOptionKeys.some((key) => key in arg);
36
44
  }
37
45
  function isEmptyArray(input) {
38
46
  return Array.isArray(input) && input.length === 0;
@@ -49,6 +57,9 @@ function isStringArray(input) {
49
57
  function isNestedStringArray(input) {
50
58
  return Array.isArray(input) && input.length > 0 && Array.isArray(input[0]) && input[0].length > 0 && isStringArray(input[0]);
51
59
  }
60
+ function isWhitespaceOnly(s) {
61
+ return s.trim() === "";
62
+ }
52
63
 
53
64
  // src/utils/regex.ts
54
65
  var regexCache = /* @__PURE__ */ new Map();
@@ -101,8 +112,8 @@ function divideString(input, numSeparators, strSeparators) {
101
112
  }
102
113
 
103
114
  // src/utils/array.ts
104
- function ensureArray(input) {
105
- return Array.isArray(input) ? input : [input];
115
+ function ensureStringArray(input) {
116
+ return typeof input === "string" ? [input] : input;
106
117
  }
107
118
 
108
119
  // src/utils/option.ts
@@ -127,6 +138,9 @@ function applyDividerOptions(result, options) {
127
138
  if (options.flatten) {
128
139
  output = output.flat();
129
140
  }
141
+ if (options.excludeEmpty) {
142
+ output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
143
+ }
130
144
  return output;
131
145
  }
132
146
 
@@ -151,7 +165,7 @@ function divider(input, ...args) {
151
165
  return [];
152
166
  }
153
167
  if (isEmptyArray(args)) {
154
- return ensureArray(input);
168
+ return ensureStringArray(input);
155
169
  }
156
170
  const { cleanedArgs, options } = extractOptions(args);
157
171
  const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
package/dist/index.d.cts CHANGED
@@ -1,16 +1,19 @@
1
- type DividerResult<T extends string | string[], F extends boolean = false> = T extends string ? string[] : F extends true ? string[] : string[][];
2
- type DividerOptions<F extends boolean> = Partial<Record<'flatten' | 'trim', F>>;
1
+ declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
2
+
3
+ type DividerOptionKey = (typeof dividerOptionKeys)[number];
4
+ type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
5
+ type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
3
6
  type DividerSeparators = (number | string)[];
4
- type DividerArgs<F extends boolean> = DividerSeparators | [...DividerSeparators, DividerOptions<F>];
7
+ type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
5
8
 
6
- declare function divider<T extends string | string[], F extends boolean>(input: T, ...args: DividerArgs<F>): DividerResult<T, F>;
9
+ declare function divider<T extends string | string[]>(input: T, ...args: DividerArgs): DividerResult<T>;
7
10
 
8
11
  declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
9
12
 
10
13
  declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
11
14
 
12
- declare function dividerLoop<T extends string | string[], F extends boolean>(input: T, size: number, options?: DividerOptions<F>): DividerResult<T, F>;
15
+ declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerOptions): DividerResult<T>;
13
16
 
14
- declare function dividerNumberString<T extends string | string[], F extends boolean = false>(input: T, options?: DividerOptions<F>): DividerResult<T, F>;
17
+ declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
15
18
 
16
19
  export { type DividerResult, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.d.ts CHANGED
@@ -1,16 +1,19 @@
1
- type DividerResult<T extends string | string[], F extends boolean = false> = T extends string ? string[] : F extends true ? string[] : string[][];
2
- type DividerOptions<F extends boolean> = Partial<Record<'flatten' | 'trim', F>>;
1
+ declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
2
+
3
+ type DividerOptionKey = (typeof dividerOptionKeys)[number];
4
+ type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
5
+ type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
3
6
  type DividerSeparators = (number | string)[];
4
- type DividerArgs<F extends boolean> = DividerSeparators | [...DividerSeparators, DividerOptions<F>];
7
+ type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
5
8
 
6
- declare function divider<T extends string | string[], F extends boolean>(input: T, ...args: DividerArgs<F>): DividerResult<T, F>;
9
+ declare function divider<T extends string | string[]>(input: T, ...args: DividerArgs): DividerResult<T>;
7
10
 
8
11
  declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
9
12
 
10
13
  declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
11
14
 
12
- declare function dividerLoop<T extends string | string[], F extends boolean>(input: T, size: number, options?: DividerOptions<F>): DividerResult<T, F>;
15
+ declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerOptions): DividerResult<T>;
13
16
 
14
- declare function dividerNumberString<T extends string | string[], F extends boolean = false>(input: T, options?: DividerOptions<F>): DividerResult<T, F>;
17
+ declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
15
18
 
16
19
  export { type DividerResult, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.js CHANGED
@@ -1,8 +1,16 @@
1
+ // src/core/constants.ts
2
+ var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
3
+
1
4
  // src/utils/is.ts
2
- var isString = (arg) => typeof arg === "string";
3
- var isNumber = (arg) => typeof arg === "number";
5
+ function isString(arg) {
6
+ return typeof arg === "string";
7
+ }
8
+ function isNumber(arg) {
9
+ return typeof arg === "number";
10
+ }
4
11
  function isOptions(arg) {
5
- return typeof arg === "object" && arg !== null && "flatten" in arg;
12
+ if (typeof arg !== "object" || arg === null) return false;
13
+ return dividerOptionKeys.some((key) => key in arg);
6
14
  }
7
15
  function isEmptyArray(input) {
8
16
  return Array.isArray(input) && input.length === 0;
@@ -19,6 +27,9 @@ function isStringArray(input) {
19
27
  function isNestedStringArray(input) {
20
28
  return Array.isArray(input) && input.length > 0 && Array.isArray(input[0]) && input[0].length > 0 && isStringArray(input[0]);
21
29
  }
30
+ function isWhitespaceOnly(s) {
31
+ return s.trim() === "";
32
+ }
22
33
 
23
34
  // src/utils/regex.ts
24
35
  var regexCache = /* @__PURE__ */ new Map();
@@ -71,8 +82,8 @@ function divideString(input, numSeparators, strSeparators) {
71
82
  }
72
83
 
73
84
  // src/utils/array.ts
74
- function ensureArray(input) {
75
- return Array.isArray(input) ? input : [input];
85
+ function ensureStringArray(input) {
86
+ return typeof input === "string" ? [input] : input;
76
87
  }
77
88
 
78
89
  // src/utils/option.ts
@@ -97,6 +108,9 @@ function applyDividerOptions(result, options) {
97
108
  if (options.flatten) {
98
109
  output = output.flat();
99
110
  }
111
+ if (options.excludeEmpty) {
112
+ output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
113
+ }
100
114
  return output;
101
115
  }
102
116
 
@@ -121,7 +135,7 @@ function divider(input, ...args) {
121
135
  return [];
122
136
  }
123
137
  if (isEmptyArray(args)) {
124
- return ensureArray(input);
138
+ return ensureStringArray(input);
125
139
  }
126
140
  const { cleanedArgs, options } = extractOptions(args);
127
141
  const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.6.2",
4
+ "version": "1.7.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",