@nyaomaru/divider 1.0.4 → 1.0.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.
- package/dist/cjs/index.js +24 -12
- package/dist/esm/index.js +24 -12
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -4,8 +4,9 @@ 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
|
-
const options =
|
|
8
|
-
|
|
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');
|
|
9
10
|
if (typeof input === 'string') {
|
|
10
11
|
return divideString(input, separators);
|
|
11
12
|
}
|
|
@@ -22,29 +23,40 @@ function divideString(input, separators) {
|
|
|
22
23
|
: acc.strSeparators.push(separator);
|
|
23
24
|
return acc;
|
|
24
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
|
+
}
|
|
25
32
|
// Divide by number delimiters
|
|
26
33
|
let parts = sliceByIndexes(input, numSeparators);
|
|
27
34
|
// Divide by string delimiters
|
|
28
35
|
if (strSeparators.length) {
|
|
29
|
-
const regex = new RegExp(`[${strSeparators.join('')}]`, 'g');
|
|
30
|
-
parts = parts
|
|
31
|
-
.flatMap((part) => part.split(regex))
|
|
32
|
-
.filter((part) => part !== '');
|
|
36
|
+
const regex = new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g');
|
|
37
|
+
parts = parts.flatMap((part) => part.split(regex)).filter(Boolean);
|
|
33
38
|
}
|
|
34
39
|
return parts;
|
|
35
40
|
}
|
|
36
41
|
function sliceByIndexes(input, indexes) {
|
|
37
42
|
if (!indexes.length)
|
|
38
43
|
return [input];
|
|
39
|
-
indexes.sort((a, b) => a - b);
|
|
40
|
-
|
|
44
|
+
const sortedIndexes = indexes.slice().sort((a, b) => a - b);
|
|
45
|
+
const parts = new Array(sortedIndexes.length + 1).fill(null);
|
|
41
46
|
let start = 0;
|
|
42
|
-
for (
|
|
47
|
+
for (let i = 0; i < sortedIndexes.length; i++) {
|
|
48
|
+
const index = sortedIndexes[i];
|
|
43
49
|
if (index > start && index < input.length) {
|
|
44
|
-
parts
|
|
50
|
+
parts[i] = input.slice(start, index);
|
|
45
51
|
start = index;
|
|
46
52
|
}
|
|
47
53
|
}
|
|
48
|
-
parts.
|
|
49
|
-
return parts.filter(
|
|
54
|
+
parts[sortedIndexes.length] = input.slice(start);
|
|
55
|
+
return parts.filter(Boolean);
|
|
56
|
+
}
|
|
57
|
+
function isOptions(arg) {
|
|
58
|
+
return typeof arg === 'object' && arg !== null && 'flatten' in arg;
|
|
59
|
+
}
|
|
60
|
+
function escapeRegExp(str) {
|
|
61
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
50
62
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export function divider(input, ...args) {
|
|
2
2
|
// extract the options from the input
|
|
3
3
|
const lastArg = args[args.length - 1];
|
|
4
|
-
const options =
|
|
5
|
-
|
|
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');
|
|
6
7
|
if (typeof input === 'string') {
|
|
7
8
|
return divideString(input, separators);
|
|
8
9
|
}
|
|
@@ -19,29 +20,40 @@ function divideString(input, separators) {
|
|
|
19
20
|
: acc.strSeparators.push(separator);
|
|
20
21
|
return acc;
|
|
21
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
|
+
}
|
|
22
29
|
// Divide by number delimiters
|
|
23
30
|
let parts = sliceByIndexes(input, numSeparators);
|
|
24
31
|
// Divide by string delimiters
|
|
25
32
|
if (strSeparators.length) {
|
|
26
|
-
const regex = new RegExp(`[${strSeparators.join('')}]`, 'g');
|
|
27
|
-
parts = parts
|
|
28
|
-
.flatMap((part) => part.split(regex))
|
|
29
|
-
.filter((part) => part !== '');
|
|
33
|
+
const regex = new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g');
|
|
34
|
+
parts = parts.flatMap((part) => part.split(regex)).filter(Boolean);
|
|
30
35
|
}
|
|
31
36
|
return parts;
|
|
32
37
|
}
|
|
33
38
|
function sliceByIndexes(input, indexes) {
|
|
34
39
|
if (!indexes.length)
|
|
35
40
|
return [input];
|
|
36
|
-
indexes.sort((a, b) => a - b);
|
|
37
|
-
|
|
41
|
+
const sortedIndexes = indexes.slice().sort((a, b) => a - b);
|
|
42
|
+
const parts = new Array(sortedIndexes.length + 1).fill(null);
|
|
38
43
|
let start = 0;
|
|
39
|
-
for (
|
|
44
|
+
for (let i = 0; i < sortedIndexes.length; i++) {
|
|
45
|
+
const index = sortedIndexes[i];
|
|
40
46
|
if (index > start && index < input.length) {
|
|
41
|
-
parts
|
|
47
|
+
parts[i] = input.slice(start, index);
|
|
42
48
|
start = index;
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
|
-
parts.
|
|
46
|
-
return parts.filter(
|
|
51
|
+
parts[sortedIndexes.length] = input.slice(start);
|
|
52
|
+
return parts.filter(Boolean);
|
|
53
|
+
}
|
|
54
|
+
function isOptions(arg) {
|
|
55
|
+
return typeof arg === 'object' && arg !== null && 'flatten' in arg;
|
|
56
|
+
}
|
|
57
|
+
function escapeRegExp(str) {
|
|
58
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
47
59
|
}
|