@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 +21 -21
- package/dist/esm/index.js +21 -21
- package/package.json +1 -1
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
|
-
//
|
|
5
|
+
// Extract the options from the input
|
|
6
6
|
const lastArg = args[args.length - 1];
|
|
7
7
|
const options = isOptions(lastArg) ? lastArg : {};
|
|
8
|
-
//
|
|
9
|
-
const
|
|
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,
|
|
20
|
+
return divideString(input, numSeparators, strSeparators);
|
|
12
21
|
}
|
|
13
|
-
const result = input.map((item) => divideString(item,
|
|
22
|
+
const result = input.map((item) => divideString(item, numSeparators, strSeparators));
|
|
14
23
|
return (options.flatten ? result.flat() : result);
|
|
15
24
|
}
|
|
16
|
-
function divideString(input,
|
|
17
|
-
if (
|
|
25
|
+
function divideString(input, numSeparators, strSeparators) {
|
|
26
|
+
if (numSeparators.length === 0 && strSeparators.length === 0) {
|
|
18
27
|
return [input];
|
|
19
28
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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 (
|
|
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
|
-
//
|
|
2
|
+
// Extract the options from the input
|
|
3
3
|
const lastArg = args[args.length - 1];
|
|
4
4
|
const options = isOptions(lastArg) ? lastArg : {};
|
|
5
|
-
//
|
|
6
|
-
const
|
|
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,
|
|
17
|
+
return divideString(input, numSeparators, strSeparators);
|
|
9
18
|
}
|
|
10
|
-
const result = input.map((item) => divideString(item,
|
|
19
|
+
const result = input.map((item) => divideString(item, numSeparators, strSeparators));
|
|
11
20
|
return (options.flatten ? result.flat() : result);
|
|
12
21
|
}
|
|
13
|
-
function divideString(input,
|
|
14
|
-
if (
|
|
22
|
+
function divideString(input, numSeparators, strSeparators) {
|
|
23
|
+
if (numSeparators.length === 0 && strSeparators.length === 0) {
|
|
15
24
|
return [input];
|
|
16
25
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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 (
|
|
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;
|