@nyaomaru/divider 1.0.3 → 1.0.5
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 +18 -7
- package/dist/esm/index.js +18 -7
- package/dist/index.d.ts +4 -2
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -4,13 +4,14 @@ 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
|
}
|
|
12
13
|
const result = input.map((item) => divideString(item, separators));
|
|
13
|
-
return options.flatten ? result.flat() : result;
|
|
14
|
+
return (options.flatten ? result.flat() : result);
|
|
14
15
|
}
|
|
15
16
|
function divideString(input, separators) {
|
|
16
17
|
if (separators.length === 0) {
|
|
@@ -22,14 +23,18 @@ 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
|
}
|
|
@@ -48,3 +53,9 @@ function sliceByIndexes(input, indexes) {
|
|
|
48
53
|
parts.push(input.slice(start));
|
|
49
54
|
return parts.filter((part) => part !== '');
|
|
50
55
|
}
|
|
56
|
+
function isOptions(arg) {
|
|
57
|
+
return typeof arg === 'object' && arg !== null && 'flatten' in arg;
|
|
58
|
+
}
|
|
59
|
+
function escapeRegExp(str) {
|
|
60
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
61
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
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
|
}
|
|
9
10
|
const result = input.map((item) => divideString(item, separators));
|
|
10
|
-
return options.flatten ? result.flat() : result;
|
|
11
|
+
return (options.flatten ? result.flat() : result);
|
|
11
12
|
}
|
|
12
13
|
function divideString(input, separators) {
|
|
13
14
|
if (separators.length === 0) {
|
|
@@ -19,14 +20,18 @@ 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
|
}
|
|
@@ -45,3 +50,9 @@ function sliceByIndexes(input, indexes) {
|
|
|
45
50
|
parts.push(input.slice(start));
|
|
46
51
|
return parts.filter((part) => part !== '');
|
|
47
52
|
}
|
|
53
|
+
function isOptions(arg) {
|
|
54
|
+
return typeof arg === 'object' && arg !== null && 'flatten' in arg;
|
|
55
|
+
}
|
|
56
|
+
function escapeRegExp(str) {
|
|
57
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
58
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
type DividerResult<T extends string | string[], F extends boolean | undefined> = T extends string ? string[] : F extends true ? string[] : string[][];
|
|
2
|
+
export declare function divider<T extends string | string[]>(input: string | string[], ...args: (number | string | {
|
|
2
3
|
flatten?: boolean;
|
|
3
|
-
})[]):
|
|
4
|
+
})[]): DividerResult<T, boolean>;
|
|
5
|
+
export {};
|