@nyaomaru/divider 1.0.6 → 1.0.7

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/cjs/index.js CHANGED
@@ -2,38 +2,38 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.divider = divider;
4
4
  function divider(input, ...args) {
5
- // extract the options from the input
5
+ // Extract the options from the input
6
6
  const lastArg = args[args.length - 1];
7
7
  const options = isOptions(lastArg) ? lastArg : {};
8
- // filter out only numbers and strings
9
- const separators = args.filter((arg) => typeof arg === 'number' || typeof arg === 'string');
8
+ // Filter out only numbers and strings
9
+ const numSeparators = [];
10
+ const strSeparators = [];
11
+ for (const arg of args) {
12
+ if (typeof arg === 'number') {
13
+ numSeparators.push(arg);
14
+ }
15
+ else if (typeof arg === 'string') {
16
+ strSeparators.push(arg);
17
+ }
18
+ }
10
19
  if (typeof input === 'string') {
11
- return divideString(input, separators);
20
+ return divideString(input, numSeparators, strSeparators);
12
21
  }
13
- const result = input.map((item) => divideString(item, separators));
22
+ const result = input.map((item) => divideString(item, numSeparators, strSeparators));
14
23
  return (options.flatten ? result.flat() : result);
15
24
  }
16
- function divideString(input, separators) {
17
- if (separators.length === 0) {
25
+ function divideString(input, numSeparators, strSeparators) {
26
+ if (numSeparators.length === 0 && strSeparators.length === 0) {
18
27
  return [input];
19
28
  }
20
- const { numSeparators, strSeparators } = separators.reduce((acc, separator) => {
21
- typeof separator === 'number'
22
- ? acc.numSeparators.push(separator)
23
- : acc.strSeparators.push(separator);
24
- return acc;
25
- }, { numSeparators: [], strSeparators: [] });
26
- // If there are no number delimiters, split by string delimiters only
27
- if (numSeparators.length === 0 && strSeparators.length > 0) {
28
- return input
29
- .split(new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g'))
30
- .filter(Boolean);
31
- }
29
+ // Precompile regex for string separators
30
+ const regex = strSeparators.length
31
+ ? new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g')
32
+ : null;
32
33
  // Divide by number delimiters
33
34
  let parts = sliceByIndexes(input, numSeparators);
34
35
  // Divide by string delimiters
35
- if (strSeparators.length) {
36
- const regex = new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g');
36
+ if (regex) {
37
37
  parts = parts.flatMap((part) => part.split(regex)).filter(Boolean);
38
38
  }
39
39
  return parts;
package/dist/esm/index.js CHANGED
@@ -1,36 +1,36 @@
1
1
  export function divider(input, ...args) {
2
- // extract the options from the input
2
+ // Extract the options from the input
3
3
  const lastArg = args[args.length - 1];
4
4
  const options = isOptions(lastArg) ? lastArg : {};
5
- // filter out only numbers and strings
6
- const separators = args.filter((arg) => typeof arg === 'number' || typeof arg === 'string');
5
+ // Filter out only numbers and strings
6
+ const numSeparators = [];
7
+ const strSeparators = [];
8
+ for (const arg of args) {
9
+ if (typeof arg === 'number') {
10
+ numSeparators.push(arg);
11
+ }
12
+ else if (typeof arg === 'string') {
13
+ strSeparators.push(arg);
14
+ }
15
+ }
7
16
  if (typeof input === 'string') {
8
- return divideString(input, separators);
17
+ return divideString(input, numSeparators, strSeparators);
9
18
  }
10
- const result = input.map((item) => divideString(item, separators));
19
+ const result = input.map((item) => divideString(item, numSeparators, strSeparators));
11
20
  return (options.flatten ? result.flat() : result);
12
21
  }
13
- function divideString(input, separators) {
14
- if (separators.length === 0) {
22
+ function divideString(input, numSeparators, strSeparators) {
23
+ if (numSeparators.length === 0 && strSeparators.length === 0) {
15
24
  return [input];
16
25
  }
17
- const { numSeparators, strSeparators } = separators.reduce((acc, separator) => {
18
- typeof separator === 'number'
19
- ? acc.numSeparators.push(separator)
20
- : acc.strSeparators.push(separator);
21
- return acc;
22
- }, { numSeparators: [], strSeparators: [] });
23
- // If there are no number delimiters, split by string delimiters only
24
- if (numSeparators.length === 0 && strSeparators.length > 0) {
25
- return input
26
- .split(new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g'))
27
- .filter(Boolean);
28
- }
26
+ // Precompile regex for string separators
27
+ const regex = strSeparators.length
28
+ ? new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g')
29
+ : null;
29
30
  // Divide by number delimiters
30
31
  let parts = sliceByIndexes(input, numSeparators);
31
32
  // Divide by string delimiters
32
- if (strSeparators.length) {
33
- const regex = new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g');
33
+ if (regex) {
34
34
  parts = parts.flatMap((part) => part.split(regex)).filter(Boolean);
35
35
  }
36
36
  return parts;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.0.6",
4
+ "version": "1.0.7",
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",