@nyaomaru/divider 2.0.20 → 2.0.22

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.
Files changed (3) hide show
  1. package/dist/index.cjs +23 -15
  2. package/dist/index.js +23 -15
  3. package/package.json +16 -13
package/dist/index.cjs CHANGED
@@ -277,12 +277,6 @@ function isValidInput(value) {
277
277
  function ensureStringArray(input) {
278
278
  return isString(input) ? [input] : input;
279
279
  }
280
- function getFirstElement(array, fallback) {
281
- return array[0] ?? fallback;
282
- }
283
- function getLastElement(array, fallback) {
284
- return array.at(-1) ?? fallback;
285
- }
286
280
 
287
281
  // src/utils/exclude-predicate.ts
288
282
  var excludePredicateMap = {
@@ -427,16 +421,20 @@ function divider(input, ...args) {
427
421
  return transformDividerInput(input, applyDivision, options);
428
422
  }
429
423
 
424
+ // src/core/divider-segment.ts
425
+ function selectDividerSegment(input, args, select) {
426
+ const result = divider(input, ...args, { flatten: true });
427
+ return select(result) ?? "";
428
+ }
429
+
430
430
  // src/core/divider-first.ts
431
431
  function dividerFirst(input, ...args) {
432
- const result = divider(input, ...args, { flatten: true });
433
- return getFirstElement(result, "");
432
+ return selectDividerSegment(input, args, (segments) => segments[0]);
434
433
  }
435
434
 
436
435
  // src/core/divider-last.ts
437
436
  function dividerLast(input, ...args) {
438
- const result = divider(input, ...args, { flatten: true });
439
- return getLastElement(result, "");
437
+ return selectDividerSegment(input, args, (segments) => segments.at(-1));
440
438
  }
441
439
 
442
440
  // src/utils/generate-indexes.ts
@@ -463,22 +461,32 @@ function truncateChunksToMax(chunks, maxChunks) {
463
461
  const tail = chunks.slice(headCount).join("");
464
462
  return [...head, tail];
465
463
  }
464
+ function applyMaxChunks(chunks, maxChunks) {
465
+ return shouldTruncateChunks(chunks, maxChunks) ? truncateChunksToMax(chunks, maxChunks) : [...chunks];
466
+ }
466
467
 
467
468
  // src/core/divider-loop.ts
469
+ var DEFAULT_DIVIDER_LOOP_OPTIONS = {
470
+ startOffset: PERFORMANCE_CONSTANTS.DEFAULT_START_OFFSET,
471
+ maxChunks: PERFORMANCE_CONSTANTS.DEFAULT_MAX_CHUNKS
472
+ };
473
+ function resolveDividerLoopOptions(options) {
474
+ return {
475
+ startOffset: options?.startOffset ?? DEFAULT_DIVIDER_LOOP_OPTIONS.startOffset,
476
+ maxChunks: options?.maxChunks ?? DEFAULT_DIVIDER_LOOP_OPTIONS.maxChunks
477
+ };
478
+ }
468
479
  function createChunksFromString(str, size, startOffset, maxChunks) {
469
480
  const indexes = generateIndexes(str, size, startOffset);
470
481
  const chunks = divider(str, ...indexes);
471
- return shouldTruncateChunks(chunks, maxChunks) ? truncateChunksToMax(chunks, maxChunks) : chunks;
482
+ return applyMaxChunks(chunks, maxChunks);
472
483
  }
473
484
  function dividerLoop(input, size, options) {
474
485
  if (!isPositiveInteger(size)) {
475
486
  console.warn("dividerLoop: chunk size must be a positive number");
476
487
  return [];
477
488
  }
478
- const {
479
- startOffset = PERFORMANCE_CONSTANTS.DEFAULT_START_OFFSET,
480
- maxChunks = PERFORMANCE_CONSTANTS.DEFAULT_MAX_CHUNKS
481
- } = options ?? {};
489
+ const { startOffset, maxChunks } = resolveDividerLoopOptions(options);
482
490
  return transformDividerInput(
483
491
  input,
484
492
  (str) => createChunksFromString(str, size, startOffset, maxChunks),
package/dist/index.js CHANGED
@@ -243,12 +243,6 @@ function isValidInput(value) {
243
243
  function ensureStringArray(input) {
244
244
  return isString(input) ? [input] : input;
245
245
  }
246
- function getFirstElement(array, fallback) {
247
- return array[0] ?? fallback;
248
- }
249
- function getLastElement(array, fallback) {
250
- return array.at(-1) ?? fallback;
251
- }
252
246
 
253
247
  // src/utils/exclude-predicate.ts
254
248
  var excludePredicateMap = {
@@ -393,16 +387,20 @@ function divider(input, ...args) {
393
387
  return transformDividerInput(input, applyDivision, options);
394
388
  }
395
389
 
390
+ // src/core/divider-segment.ts
391
+ function selectDividerSegment(input, args, select) {
392
+ const result = divider(input, ...args, { flatten: true });
393
+ return select(result) ?? "";
394
+ }
395
+
396
396
  // src/core/divider-first.ts
397
397
  function dividerFirst(input, ...args) {
398
- const result = divider(input, ...args, { flatten: true });
399
- return getFirstElement(result, "");
398
+ return selectDividerSegment(input, args, (segments) => segments[0]);
400
399
  }
401
400
 
402
401
  // src/core/divider-last.ts
403
402
  function dividerLast(input, ...args) {
404
- const result = divider(input, ...args, { flatten: true });
405
- return getLastElement(result, "");
403
+ return selectDividerSegment(input, args, (segments) => segments.at(-1));
406
404
  }
407
405
 
408
406
  // src/utils/generate-indexes.ts
@@ -429,22 +427,32 @@ function truncateChunksToMax(chunks, maxChunks) {
429
427
  const tail = chunks.slice(headCount).join("");
430
428
  return [...head, tail];
431
429
  }
430
+ function applyMaxChunks(chunks, maxChunks) {
431
+ return shouldTruncateChunks(chunks, maxChunks) ? truncateChunksToMax(chunks, maxChunks) : [...chunks];
432
+ }
432
433
 
433
434
  // src/core/divider-loop.ts
435
+ var DEFAULT_DIVIDER_LOOP_OPTIONS = {
436
+ startOffset: PERFORMANCE_CONSTANTS.DEFAULT_START_OFFSET,
437
+ maxChunks: PERFORMANCE_CONSTANTS.DEFAULT_MAX_CHUNKS
438
+ };
439
+ function resolveDividerLoopOptions(options) {
440
+ return {
441
+ startOffset: options?.startOffset ?? DEFAULT_DIVIDER_LOOP_OPTIONS.startOffset,
442
+ maxChunks: options?.maxChunks ?? DEFAULT_DIVIDER_LOOP_OPTIONS.maxChunks
443
+ };
444
+ }
434
445
  function createChunksFromString(str, size, startOffset, maxChunks) {
435
446
  const indexes = generateIndexes(str, size, startOffset);
436
447
  const chunks = divider(str, ...indexes);
437
- return shouldTruncateChunks(chunks, maxChunks) ? truncateChunksToMax(chunks, maxChunks) : chunks;
448
+ return applyMaxChunks(chunks, maxChunks);
438
449
  }
439
450
  function dividerLoop(input, size, options) {
440
451
  if (!isPositiveInteger(size)) {
441
452
  console.warn("dividerLoop: chunk size must be a positive number");
442
453
  return [];
443
454
  }
444
- const {
445
- startOffset = PERFORMANCE_CONSTANTS.DEFAULT_START_OFFSET,
446
- maxChunks = PERFORMANCE_CONSTANTS.DEFAULT_MAX_CHUNKS
447
- } = options ?? {};
455
+ const { startOffset, maxChunks } = resolveDividerLoopOptions(options);
448
456
  return transformDividerInput(
449
457
  input,
450
458
  (str) => createChunksFromString(str, size, startOffset, maxChunks),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "2.0.20",
4
+ "version": "2.0.22",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -15,6 +15,19 @@
15
15
  "default": "./dist/index.cjs"
16
16
  }
17
17
  },
18
+ "scripts": {
19
+ "test": "jest",
20
+ "test:integration": "tsx tests/integration/usage.test.ts",
21
+ "test:types": "tsc -p tests/tsconfig.json",
22
+ "test:performance": "jest --config jest.performance.config.cjs",
23
+ "test:unit": "jest",
24
+ "lint": "eslint .",
25
+ "build": "tsup",
26
+ "prepack": "pnpm run build",
27
+ "typedoc": "typedoc",
28
+ "prepare": "lefthook install",
29
+ "knip": "knip"
30
+ },
18
31
  "keywords": [
19
32
  "divider",
20
33
  "divide",
@@ -62,15 +75,5 @@
62
75
  "bugs": {
63
76
  "url": "https://github.com/nyaomaru/divider/issues"
64
77
  },
65
- "scripts": {
66
- "test": "jest",
67
- "test:integration": "tsx tests/integration/usage.test.ts",
68
- "test:types": "tsc -p tests/tsconfig.json",
69
- "test:performance": "jest --config jest.performance.config.cjs",
70
- "test:unit": "jest",
71
- "lint": "eslint .",
72
- "build": "tsup",
73
- "typedoc": "typedoc",
74
- "knip": "knip"
75
- }
76
- }
78
+ "packageManager": "pnpm@11.2.2"
79
+ }