@nyaomaru/divider 1.3.4 → 1.3.6

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.
@@ -3,13 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.divider = divider;
4
4
  const parser_1 = require("@/core/parser");
5
5
  const is_1 = require("@/utils/is");
6
+ const array_1 = require("@/utils/array");
6
7
  function divider(input, ...args) {
7
8
  if (!(0, is_1.isString)(input) && !Array.isArray(input)) {
8
9
  console.warn("divider: 'input' must be a string or an array of strings. So returning an empty array.");
9
10
  return [];
10
11
  }
11
12
  if ((0, is_1.isEmptyArray)(args)) {
12
- return ((0, is_1.isString)(input) ? [input] : input);
13
+ return (0, array_1.ensureArray)(input);
13
14
  }
14
15
  // Extract the options from the input
15
16
  const clonedArgs = [...args];
@@ -12,7 +12,7 @@ function divideString(input, numSeparators, strSeparators) {
12
12
  const regex = (0, regex_1.getRegex)(strSeparators);
13
13
  // Divide by number delimiters
14
14
  numSeparators.sort((a, b) => a - b);
15
- let parts = (0, slice_1.sliceByIndexes)(input, numSeparators);
15
+ const parts = (0, slice_1.sliceByIndexes)(input, numSeparators);
16
16
  // Divide by string delimiters
17
17
  return regex
18
18
  ? parts.flatMap((part) => part.split(regex)).filter(Boolean)
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ensureArray = ensureArray;
4
+ function ensureArray(input) {
5
+ return Array.isArray(input) ? input : [input];
6
+ }
@@ -8,7 +8,7 @@ function sliceByIndexes(input, indexes) {
8
8
  const parts = [];
9
9
  let start = 0;
10
10
  for (const index of indexes) {
11
- if (index <= start || index >= input.length)
11
+ if (index <= start || input.length <= index)
12
12
  continue;
13
13
  parts.push(input.slice(start, index));
14
14
  start = index;
@@ -1,12 +1,13 @@
1
1
  import { divideString } from '@/core/parser';
2
2
  import { isString, isNumber, isOptions, isEmptyArray } from '@/utils/is';
3
+ import { ensureArray } from '@/utils/array';
3
4
  export function divider(input, ...args) {
4
5
  if (!isString(input) && !Array.isArray(input)) {
5
6
  console.warn("divider: 'input' must be a string or an array of strings. So returning an empty array.");
6
7
  return [];
7
8
  }
8
9
  if (isEmptyArray(args)) {
9
- return (isString(input) ? [input] : input);
10
+ return ensureArray(input);
10
11
  }
11
12
  // Extract the options from the input
12
13
  const clonedArgs = [...args];
@@ -9,7 +9,7 @@ export function divideString(input, numSeparators, strSeparators) {
9
9
  const regex = getRegex(strSeparators);
10
10
  // Divide by number delimiters
11
11
  numSeparators.sort((a, b) => a - b);
12
- let parts = sliceByIndexes(input, numSeparators);
12
+ const parts = sliceByIndexes(input, numSeparators);
13
13
  // Divide by string delimiters
14
14
  return regex
15
15
  ? parts.flatMap((part) => part.split(regex)).filter(Boolean)
@@ -0,0 +1,3 @@
1
+ export function ensureArray(input) {
2
+ return Array.isArray(input) ? input : [input];
3
+ }
@@ -5,7 +5,7 @@ export function sliceByIndexes(input, indexes) {
5
5
  const parts = [];
6
6
  let start = 0;
7
7
  for (const index of indexes) {
8
- if (index <= start || index >= input.length)
8
+ if (index <= start || input.length <= index)
9
9
  continue;
10
10
  parts.push(input.slice(start, index));
11
11
  start = index;
@@ -0,0 +1 @@
1
+ export declare function ensureArray<T>(input: T | T[]): T[];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.3.4",
4
+ "version": "1.3.6",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "dist/cjs/index.js",
7
7
  "module": "dist/esm/index.js",