@nyaomaru/divider 1.8.2 → 1.8.3

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
@@ -221,23 +221,21 @@ function dividerLoop(input, size, options) {
221
221
  console.warn("dividerLoop: chunk size must be a positive number");
222
222
  return [];
223
223
  }
224
- const { startOffset = 0, maxChunks } = options ?? {};
224
+ const finalOptions = options ?? {};
225
+ const { startOffset = 0, maxChunks = 0 } = finalOptions;
225
226
  const applyChunking = (str) => {
226
- let chunks = divider(str, ...generateIndexes(str, size, startOffset));
227
- const shouldTruncateChunks = isNumber(maxChunks) && maxChunks > 0 && chunks.length > maxChunks;
228
- if (shouldTruncateChunks) {
229
- const head = chunks.slice(0, maxChunks - 1);
230
- const tail = chunks.slice(maxChunks - 1).join("");
231
- chunks = [...head, tail];
232
- }
233
- return chunks;
227
+ const chunks = divider(str, ...generateIndexes(str, size, startOffset));
228
+ return needsTruncation(chunks) ? truncateChunks(chunks) : chunks;
234
229
  };
235
- if (isString(input)) {
236
- const result2 = applyChunking(input);
237
- return applyDividerOptions(result2, options ?? {});
238
- }
239
- const result = input.map(applyChunking);
240
- return applyDividerOptions(result, options ?? {});
230
+ const needsTruncation = (chunks) => isNumber(maxChunks) && 0 < maxChunks && maxChunks < chunks.length;
231
+ const truncateChunks = (chunks) => {
232
+ const HEAD_COUNT = maxChunks - 1;
233
+ const head = chunks.slice(0, HEAD_COUNT);
234
+ const tail = chunks.slice(HEAD_COUNT).join("");
235
+ return [...head, tail];
236
+ };
237
+ const result = isString(input) ? applyChunking(input) : input.map(applyChunking);
238
+ return applyDividerOptions(result, finalOptions);
241
239
  }
242
240
 
243
241
  // src/utils/divide.ts
package/dist/index.js CHANGED
@@ -191,23 +191,21 @@ function dividerLoop(input, size, options) {
191
191
  console.warn("dividerLoop: chunk size must be a positive number");
192
192
  return [];
193
193
  }
194
- const { startOffset = 0, maxChunks } = options ?? {};
194
+ const finalOptions = options ?? {};
195
+ const { startOffset = 0, maxChunks = 0 } = finalOptions;
195
196
  const applyChunking = (str) => {
196
- let chunks = divider(str, ...generateIndexes(str, size, startOffset));
197
- const shouldTruncateChunks = isNumber(maxChunks) && maxChunks > 0 && chunks.length > maxChunks;
198
- if (shouldTruncateChunks) {
199
- const head = chunks.slice(0, maxChunks - 1);
200
- const tail = chunks.slice(maxChunks - 1).join("");
201
- chunks = [...head, tail];
202
- }
203
- return chunks;
197
+ const chunks = divider(str, ...generateIndexes(str, size, startOffset));
198
+ return needsTruncation(chunks) ? truncateChunks(chunks) : chunks;
204
199
  };
205
- if (isString(input)) {
206
- const result2 = applyChunking(input);
207
- return applyDividerOptions(result2, options ?? {});
208
- }
209
- const result = input.map(applyChunking);
210
- return applyDividerOptions(result, options ?? {});
200
+ const needsTruncation = (chunks) => isNumber(maxChunks) && 0 < maxChunks && maxChunks < chunks.length;
201
+ const truncateChunks = (chunks) => {
202
+ const HEAD_COUNT = maxChunks - 1;
203
+ const head = chunks.slice(0, HEAD_COUNT);
204
+ const tail = chunks.slice(HEAD_COUNT).join("");
205
+ return [...head, tail];
206
+ };
207
+ const result = isString(input) ? applyChunking(input) : input.map(applyChunking);
208
+ return applyDividerOptions(result, finalOptions);
211
209
  }
212
210
 
213
211
  // src/utils/divide.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.8.2",
4
+ "version": "1.8.3",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",