@nyaomaru/divider 1.6.1 โ†’ 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,11 +112,11 @@ 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
- // src/utils/options.ts
119
+ // src/utils/option.ts
109
120
  function extractOptions(args) {
110
121
  const clonedArgs = [...args];
111
122
  const lastArg = clonedArgs.at(-1);
@@ -118,6 +129,20 @@ function extractOptions(args) {
118
129
  options
119
130
  };
120
131
  }
132
+ function applyDividerOptions(result, options) {
133
+ let output = result;
134
+ if (options.trim) {
135
+ const trim = (s) => s.trim();
136
+ output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
137
+ }
138
+ if (options.flatten) {
139
+ output = output.flat();
140
+ }
141
+ if (options.excludeEmpty) {
142
+ output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
143
+ }
144
+ return output;
145
+ }
121
146
 
122
147
  // src/utils/separator.ts
123
148
  function classifySeparators(args) {
@@ -131,19 +156,6 @@ function classifySeparators(args) {
131
156
  );
132
157
  }
133
158
 
134
- // src/utils/option.ts
135
- function applyDividerOptions(result, options) {
136
- let output = result;
137
- if (options.trim) {
138
- const trim = (s) => s.trim();
139
- output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
140
- }
141
- if (options.flatten) {
142
- output = output.flat();
143
- }
144
- return output;
145
- }
146
-
147
159
  // src/core/divider.ts
148
160
  function divider(input, ...args) {
149
161
  if (!isValidInput(input)) {
@@ -153,7 +165,7 @@ function divider(input, ...args) {
153
165
  return [];
154
166
  }
155
167
  if (isEmptyArray(args)) {
156
- return ensureArray(input);
168
+ return ensureStringArray(input);
157
169
  }
158
170
  const { cleanedArgs, options } = extractOptions(args);
159
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,11 +82,11 @@ 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
- // src/utils/options.ts
89
+ // src/utils/option.ts
79
90
  function extractOptions(args) {
80
91
  const clonedArgs = [...args];
81
92
  const lastArg = clonedArgs.at(-1);
@@ -88,6 +99,20 @@ function extractOptions(args) {
88
99
  options
89
100
  };
90
101
  }
102
+ function applyDividerOptions(result, options) {
103
+ let output = result;
104
+ if (options.trim) {
105
+ const trim = (s) => s.trim();
106
+ output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
107
+ }
108
+ if (options.flatten) {
109
+ output = output.flat();
110
+ }
111
+ if (options.excludeEmpty) {
112
+ output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
113
+ }
114
+ return output;
115
+ }
91
116
 
92
117
  // src/utils/separator.ts
93
118
  function classifySeparators(args) {
@@ -101,19 +126,6 @@ function classifySeparators(args) {
101
126
  );
102
127
  }
103
128
 
104
- // src/utils/option.ts
105
- function applyDividerOptions(result, options) {
106
- let output = result;
107
- if (options.trim) {
108
- const trim = (s) => s.trim();
109
- output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
110
- }
111
- if (options.flatten) {
112
- output = output.flat();
113
- }
114
- return output;
115
- }
116
-
117
129
  // src/core/divider.ts
118
130
  function divider(input, ...args) {
119
131
  if (!isValidInput(input)) {
@@ -123,7 +135,7 @@ function divider(input, ...args) {
123
135
  return [];
124
136
  }
125
137
  if (isEmptyArray(args)) {
126
- return ensureArray(input);
138
+ return ensureStringArray(input);
127
139
  }
128
140
  const { cleanedArgs, options } = extractOptions(args);
129
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.1",
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",