@nyaomaru/divider 1.0.6 → 1.0.8
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/README.md +16 -2
- package/dist/cjs/index.js +22 -41
- package/dist/cjs/utils/regex.js +16 -0
- package/dist/cjs/utils/slice.js +18 -0
- package/dist/cjs/utils/types.js +2 -0
- package/dist/esm/index.js +21 -40
- package/dist/esm/utils/regex.js +13 -0
- package/dist/esm/utils/slice.js +15 -0
- package/dist/esm/utils/types.js +1 -0
- package/dist/index.d.ts +1 -2
- package/dist/utils/regex.d.ts +1 -0
- package/dist/utils/slice.d.ts +1 -0
- package/dist/utils/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,13 +23,27 @@ A simple utility to divide a `string` or `string[]` based on given indexes or de
|
|
|
23
23
|
|
|
24
24
|
## 🚀 Installation
|
|
25
25
|
|
|
26
|
+
You can install `@nyaomaru/divider` using your favorite package manager:
|
|
27
|
+
|
|
26
28
|
```sh
|
|
29
|
+
# Using pnpm (recommended)
|
|
27
30
|
pnpm install @nyaomaru/divider
|
|
31
|
+
|
|
32
|
+
# Using npm
|
|
33
|
+
npm install @nyaomaru/divider
|
|
34
|
+
|
|
35
|
+
# Using bun
|
|
36
|
+
bun add @nyaomaru/divider
|
|
37
|
+
|
|
38
|
+
# Using yarn
|
|
39
|
+
yarn add @nyaomaru/divider
|
|
28
40
|
```
|
|
29
41
|
|
|
30
42
|
## 📖 Usage
|
|
31
43
|
|
|
32
|
-
|
|
44
|
+
👉 [Check out the full documentation here!](https://divider-docs.vercel.app/)
|
|
45
|
+
|
|
46
|
+
### 📌 Basic Usage
|
|
33
47
|
|
|
34
48
|
```ts
|
|
35
49
|
import { divider } from '@nyaomaru/divider';
|
|
@@ -58,7 +72,7 @@ const dividedWordsWithFlattenOption = divider(words, 2, { flatten: true });
|
|
|
58
72
|
// ['he', 'llo', 'wo', 'rld']
|
|
59
73
|
```
|
|
60
74
|
|
|
61
|
-
### Advanced
|
|
75
|
+
### 📌 Advanced Usage
|
|
62
76
|
|
|
63
77
|
```ts
|
|
64
78
|
// Mixed usage of indexes and characters
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,62 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.divider = divider;
|
|
4
|
+
const slice_1 = require("@/utils/slice");
|
|
5
|
+
const regex_1 = require("@/utils/regex");
|
|
4
6
|
function divider(input, ...args) {
|
|
5
|
-
//
|
|
7
|
+
// Extract the options from the input
|
|
6
8
|
const lastArg = args[args.length - 1];
|
|
7
9
|
const options = isOptions(lastArg) ? lastArg : {};
|
|
8
|
-
//
|
|
9
|
-
const
|
|
10
|
+
// Filter out only numbers and strings
|
|
11
|
+
const numSeparators = [];
|
|
12
|
+
const strSeparators = [];
|
|
13
|
+
for (const arg of args) {
|
|
14
|
+
if (typeof arg === 'number') {
|
|
15
|
+
numSeparators.push(arg);
|
|
16
|
+
}
|
|
17
|
+
else if (typeof arg === 'string') {
|
|
18
|
+
strSeparators.push(arg);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
10
21
|
if (typeof input === 'string') {
|
|
11
|
-
return divideString(input,
|
|
22
|
+
return divideString(input, numSeparators, strSeparators);
|
|
12
23
|
}
|
|
13
|
-
const result = input.map((item) => divideString(item,
|
|
24
|
+
const result = input.map((item) => divideString(item, numSeparators, strSeparators));
|
|
14
25
|
return (options.flatten ? result.flat() : result);
|
|
15
26
|
}
|
|
16
|
-
function divideString(input,
|
|
17
|
-
if (
|
|
27
|
+
function divideString(input, numSeparators, strSeparators) {
|
|
28
|
+
if (numSeparators.length === 0 && strSeparators.length === 0) {
|
|
18
29
|
return [input];
|
|
19
30
|
}
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
}
|
|
31
|
+
// Precompile regex for string separators
|
|
32
|
+
const regex = (0, regex_1.getRegex)(strSeparators);
|
|
32
33
|
// Divide by number delimiters
|
|
33
|
-
let parts = sliceByIndexes(input, numSeparators);
|
|
34
|
+
let parts = (0, slice_1.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;
|
|
40
40
|
}
|
|
41
|
-
function sliceByIndexes(input, indexes) {
|
|
42
|
-
if (!indexes.length)
|
|
43
|
-
return [input];
|
|
44
|
-
const sortedIndexes = indexes.slice().sort((a, b) => a - b);
|
|
45
|
-
const parts = new Array(sortedIndexes.length + 1).fill(null);
|
|
46
|
-
let start = 0;
|
|
47
|
-
for (let i = 0; i < sortedIndexes.length; i++) {
|
|
48
|
-
const index = sortedIndexes[i];
|
|
49
|
-
if (index > start && index < input.length) {
|
|
50
|
-
parts[i] = input.slice(start, index);
|
|
51
|
-
start = index;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
parts[sortedIndexes.length] = input.slice(start);
|
|
55
|
-
return parts.filter(Boolean);
|
|
56
|
-
}
|
|
57
41
|
function isOptions(arg) {
|
|
58
42
|
return typeof arg === 'object' && arg !== null && 'flatten' in arg;
|
|
59
43
|
}
|
|
60
|
-
function escapeRegExp(str) {
|
|
61
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
62
|
-
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRegex = getRegex;
|
|
4
|
+
const regexCache = new Map();
|
|
5
|
+
function getRegex(separators) {
|
|
6
|
+
if (separators.length === 0)
|
|
7
|
+
return null;
|
|
8
|
+
const key = separators.join('');
|
|
9
|
+
if (!regexCache.has(key)) {
|
|
10
|
+
regexCache.set(key, new RegExp(`[${separators.map(escapeRegExp).join('')}]`, 'g'));
|
|
11
|
+
}
|
|
12
|
+
return regexCache.get(key);
|
|
13
|
+
}
|
|
14
|
+
function escapeRegExp(str) {
|
|
15
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
16
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sliceByIndexes = sliceByIndexes;
|
|
4
|
+
function sliceByIndexes(input, indexes) {
|
|
5
|
+
if (!indexes.length)
|
|
6
|
+
return [input];
|
|
7
|
+
const sortedIndexes = indexes.slice().sort((a, b) => a - b);
|
|
8
|
+
const parts = [];
|
|
9
|
+
let start = 0;
|
|
10
|
+
for (const index of sortedIndexes) {
|
|
11
|
+
if (index > start && index < input.length) {
|
|
12
|
+
parts.push(input.slice(start, index));
|
|
13
|
+
start = index;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
parts.push(input.slice(start));
|
|
17
|
+
return parts.filter(Boolean);
|
|
18
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,59 +1,40 @@
|
|
|
1
|
+
import { sliceByIndexes } from '@/utils/slice';
|
|
2
|
+
import { getRegex } from '@/utils/regex';
|
|
1
3
|
export function divider(input, ...args) {
|
|
2
|
-
//
|
|
4
|
+
// Extract the options from the input
|
|
3
5
|
const lastArg = args[args.length - 1];
|
|
4
6
|
const options = isOptions(lastArg) ? lastArg : {};
|
|
5
|
-
//
|
|
6
|
-
const
|
|
7
|
+
// Filter out only numbers and strings
|
|
8
|
+
const numSeparators = [];
|
|
9
|
+
const strSeparators = [];
|
|
10
|
+
for (const arg of args) {
|
|
11
|
+
if (typeof arg === 'number') {
|
|
12
|
+
numSeparators.push(arg);
|
|
13
|
+
}
|
|
14
|
+
else if (typeof arg === 'string') {
|
|
15
|
+
strSeparators.push(arg);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
7
18
|
if (typeof input === 'string') {
|
|
8
|
-
return divideString(input,
|
|
19
|
+
return divideString(input, numSeparators, strSeparators);
|
|
9
20
|
}
|
|
10
|
-
const result = input.map((item) => divideString(item,
|
|
21
|
+
const result = input.map((item) => divideString(item, numSeparators, strSeparators));
|
|
11
22
|
return (options.flatten ? result.flat() : result);
|
|
12
23
|
}
|
|
13
|
-
function divideString(input,
|
|
14
|
-
if (
|
|
24
|
+
function divideString(input, numSeparators, strSeparators) {
|
|
25
|
+
if (numSeparators.length === 0 && strSeparators.length === 0) {
|
|
15
26
|
return [input];
|
|
16
27
|
}
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
}
|
|
28
|
+
// Precompile regex for string separators
|
|
29
|
+
const regex = getRegex(strSeparators);
|
|
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;
|
|
37
37
|
}
|
|
38
|
-
function sliceByIndexes(input, indexes) {
|
|
39
|
-
if (!indexes.length)
|
|
40
|
-
return [input];
|
|
41
|
-
const sortedIndexes = indexes.slice().sort((a, b) => a - b);
|
|
42
|
-
const parts = new Array(sortedIndexes.length + 1).fill(null);
|
|
43
|
-
let start = 0;
|
|
44
|
-
for (let i = 0; i < sortedIndexes.length; i++) {
|
|
45
|
-
const index = sortedIndexes[i];
|
|
46
|
-
if (index > start && index < input.length) {
|
|
47
|
-
parts[i] = input.slice(start, index);
|
|
48
|
-
start = index;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
parts[sortedIndexes.length] = input.slice(start);
|
|
52
|
-
return parts.filter(Boolean);
|
|
53
|
-
}
|
|
54
38
|
function isOptions(arg) {
|
|
55
39
|
return typeof arg === 'object' && arg !== null && 'flatten' in arg;
|
|
56
40
|
}
|
|
57
|
-
function escapeRegExp(str) {
|
|
58
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
59
|
-
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const regexCache = new Map();
|
|
2
|
+
export function getRegex(separators) {
|
|
3
|
+
if (separators.length === 0)
|
|
4
|
+
return null;
|
|
5
|
+
const key = separators.join('');
|
|
6
|
+
if (!regexCache.has(key)) {
|
|
7
|
+
regexCache.set(key, new RegExp(`[${separators.map(escapeRegExp).join('')}]`, 'g'));
|
|
8
|
+
}
|
|
9
|
+
return regexCache.get(key);
|
|
10
|
+
}
|
|
11
|
+
function escapeRegExp(str) {
|
|
12
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function sliceByIndexes(input, indexes) {
|
|
2
|
+
if (!indexes.length)
|
|
3
|
+
return [input];
|
|
4
|
+
const sortedIndexes = indexes.slice().sort((a, b) => a - b);
|
|
5
|
+
const parts = [];
|
|
6
|
+
let start = 0;
|
|
7
|
+
for (const index of sortedIndexes) {
|
|
8
|
+
if (index > start && index < input.length) {
|
|
9
|
+
parts.push(input.slice(start, index));
|
|
10
|
+
start = index;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
parts.push(input.slice(start));
|
|
14
|
+
return parts.filter(Boolean);
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
type DividerResult
|
|
1
|
+
import type { DividerResult } from '@/utils/types';
|
|
2
2
|
export declare function divider<T extends string | string[]>(input: string | string[], ...args: (number | string | {
|
|
3
3
|
flatten?: boolean;
|
|
4
4
|
})[]): DividerResult<T, boolean>;
|
|
5
|
-
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getRegex(separators: string[]): RegExp | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sliceByIndexes(input: string, indexes: number[]): string[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type DividerResult<T extends string | string[], F extends boolean = false> = T extends string ? string[] : F extends true ? string[] : string[][];
|