@nyaomaru/divider 1.5.2 → 1.6.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
@@ -155,6 +155,7 @@ const result3 = dividerNumberString(['abc123', '45z'], { flatten: true });
155
155
  | Option | Type | Default | Description |
156
156
  | --------- | --------- | ------- | ------------------------------------------------------------------------- |
157
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. |
158
159
 
159
160
  ### `flatten` (default: `false`)
160
161
 
@@ -167,6 +168,19 @@ const result2 = divider(words, 2, { flatten: true });
167
168
  // ['he', 'llo', 'wo', 'rld']
168
169
  ```
169
170
 
171
+ ### `trim` (default: `false`)
172
+
173
+ ```ts
174
+ const result = divider(' hello world ', 7, { trim: true });
175
+ // ['hello', 'world']
176
+
177
+ const result2 = divider([' a ', ' b c '], ' ', {
178
+ flatten: true,
179
+ trim: true,
180
+ });
181
+ // ['a', 'b', 'c']
182
+ ```
183
+
170
184
  ## 💡 Features
171
185
 
172
186
  ### 🧩 Flexible Division
@@ -174,7 +188,7 @@ const result2 = divider(words, 2, { flatten: true });
174
188
  - Supports both `index-based` and `string-based` division
175
189
  - Supports `multiple separators` (mixing indexes and characters)
176
190
  - Works with both `string` and `string[]` input
177
- - Optional `flatten` behavior to control nested results
191
+ - Optional `flatten` and `trim` behaviors to control output format
178
192
  - Includes `dividerNumberString()` to separate digits and letters
179
193
 
180
194
  ### 🎯 Targeted Extraction
package/dist/index.cjs CHANGED
@@ -42,6 +42,12 @@ function isPositiveInteger(value) {
42
42
  function isValidInput(input) {
43
43
  return isString(input) || Array.isArray(input) && input.every(isString);
44
44
  }
45
+ function isStringArray(input) {
46
+ return Array.isArray(input) && input.every(isString);
47
+ }
48
+ function isNestedStringArray(input) {
49
+ return Array.isArray(input) && input.length > 0 && Array.isArray(input[0]) && input[0].length > 0 && isStringArray(input[0]);
50
+ }
45
51
 
46
52
  // src/utils/regex.ts
47
53
  var regexCache = /* @__PURE__ */ new Map();
@@ -124,6 +130,23 @@ function classifySeparators(args) {
124
130
  );
125
131
  }
126
132
 
133
+ // src/utils/option.ts
134
+ function applyDividerOptions(result, options) {
135
+ let output = result;
136
+ if (options.trim) {
137
+ const trimPart = (s) => s.trim();
138
+ if (isNestedStringArray(result)) {
139
+ output = result.map((row) => row.map(trimPart).filter(Boolean));
140
+ } else {
141
+ output = result.map(trimPart).filter(Boolean);
142
+ }
143
+ }
144
+ if (options.flatten) {
145
+ output = output.flat();
146
+ }
147
+ return output;
148
+ }
149
+
127
150
  // src/core/divider.ts
128
151
  function divider(input, ...args) {
129
152
  if (!isValidInput(input)) {
@@ -137,13 +160,9 @@ function divider(input, ...args) {
137
160
  }
138
161
  const { cleanedArgs, options } = extractOptions(args);
139
162
  const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
140
- if (isString(input)) {
141
- return divideString(input, numSeparators, strSeparators);
142
- }
143
- const result = input.map(
144
- (item) => divideString(item, numSeparators, strSeparators)
145
- );
146
- return options.flatten ? result.flat() : result;
163
+ const applyDivision = (str) => divideString(str, numSeparators, strSeparators);
164
+ const result = isString(input) ? applyDivision(input) : input.map(applyDivision);
165
+ return applyDividerOptions(result, options);
147
166
  }
148
167
 
149
168
  // src/core/divider-first.ts
@@ -173,16 +192,13 @@ function dividerLoop(input, size, options) {
173
192
  console.warn("dividerLoop: chunk size must be a positive number");
174
193
  return [];
175
194
  }
195
+ const applyChunking = (str) => divider(str, ...generateIndexes(str, size));
176
196
  if (isString(input)) {
177
- const indexes = generateIndexes(input, size);
178
- return divider(input, ...indexes);
197
+ const result2 = applyChunking(input);
198
+ return applyDividerOptions(result2, options ?? {});
179
199
  }
180
- const result = input.map((item) => {
181
- const indexes = generateIndexes(item, size);
182
- return divider(item, ...indexes);
183
- });
184
- const flatten = options?.flatten ?? false;
185
- return flatten ? result.flat() : result;
200
+ const result = input.map(applyChunking);
201
+ return applyDividerOptions(result, options ?? {});
186
202
  }
187
203
  // Annotate the CommonJS export names for ESM import in node:
188
204
  0 && (module.exports = {
package/dist/index.d.cts CHANGED
@@ -1,7 +1,5 @@
1
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> = {
3
- flatten?: F;
4
- };
2
+ type DividerOptions<F extends boolean> = Partial<Record<'flatten' | 'trim', F>>;
5
3
  type DividerSeparators = (number | string)[];
6
4
  type DividerArgs<F extends boolean> = DividerSeparators | [...DividerSeparators, DividerOptions<F>];
7
5
 
package/dist/index.d.ts CHANGED
@@ -1,7 +1,5 @@
1
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> = {
3
- flatten?: F;
4
- };
2
+ type DividerOptions<F extends boolean> = Partial<Record<'flatten' | 'trim', F>>;
5
3
  type DividerSeparators = (number | string)[];
6
4
  type DividerArgs<F extends boolean> = DividerSeparators | [...DividerSeparators, DividerOptions<F>];
7
5
 
package/dist/index.js CHANGED
@@ -13,6 +13,12 @@ function isPositiveInteger(value) {
13
13
  function isValidInput(input) {
14
14
  return isString(input) || Array.isArray(input) && input.every(isString);
15
15
  }
16
+ function isStringArray(input) {
17
+ return Array.isArray(input) && input.every(isString);
18
+ }
19
+ function isNestedStringArray(input) {
20
+ return Array.isArray(input) && input.length > 0 && Array.isArray(input[0]) && input[0].length > 0 && isStringArray(input[0]);
21
+ }
16
22
 
17
23
  // src/utils/regex.ts
18
24
  var regexCache = /* @__PURE__ */ new Map();
@@ -95,6 +101,23 @@ function classifySeparators(args) {
95
101
  );
96
102
  }
97
103
 
104
+ // src/utils/option.ts
105
+ function applyDividerOptions(result, options) {
106
+ let output = result;
107
+ if (options.trim) {
108
+ const trimPart = (s) => s.trim();
109
+ if (isNestedStringArray(result)) {
110
+ output = result.map((row) => row.map(trimPart).filter(Boolean));
111
+ } else {
112
+ output = result.map(trimPart).filter(Boolean);
113
+ }
114
+ }
115
+ if (options.flatten) {
116
+ output = output.flat();
117
+ }
118
+ return output;
119
+ }
120
+
98
121
  // src/core/divider.ts
99
122
  function divider(input, ...args) {
100
123
  if (!isValidInput(input)) {
@@ -108,13 +131,9 @@ function divider(input, ...args) {
108
131
  }
109
132
  const { cleanedArgs, options } = extractOptions(args);
110
133
  const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
111
- if (isString(input)) {
112
- return divideString(input, numSeparators, strSeparators);
113
- }
114
- const result = input.map(
115
- (item) => divideString(item, numSeparators, strSeparators)
116
- );
117
- return options.flatten ? result.flat() : result;
134
+ const applyDivision = (str) => divideString(str, numSeparators, strSeparators);
135
+ const result = isString(input) ? applyDivision(input) : input.map(applyDivision);
136
+ return applyDividerOptions(result, options);
118
137
  }
119
138
 
120
139
  // src/core/divider-first.ts
@@ -144,16 +163,13 @@ function dividerLoop(input, size, options) {
144
163
  console.warn("dividerLoop: chunk size must be a positive number");
145
164
  return [];
146
165
  }
166
+ const applyChunking = (str) => divider(str, ...generateIndexes(str, size));
147
167
  if (isString(input)) {
148
- const indexes = generateIndexes(input, size);
149
- return divider(input, ...indexes);
168
+ const result2 = applyChunking(input);
169
+ return applyDividerOptions(result2, options ?? {});
150
170
  }
151
- const result = input.map((item) => {
152
- const indexes = generateIndexes(item, size);
153
- return divider(item, ...indexes);
154
- });
155
- const flatten = options?.flatten ?? false;
156
- return flatten ? result.flat() : result;
171
+ const result = input.map(applyChunking);
172
+ return applyDividerOptions(result, options ?? {});
157
173
  }
158
174
  export {
159
175
  divider,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.5.2",
4
+ "version": "1.6.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",