@nyaomaru/divider 1.0.7 → 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 +4 -23
- 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 +3 -22
- 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,6 +1,8 @@
|
|
|
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];
|
|
@@ -27,36 +29,15 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
27
29
|
return [input];
|
|
28
30
|
}
|
|
29
31
|
// Precompile regex for string separators
|
|
30
|
-
const regex = strSeparators
|
|
31
|
-
? new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g')
|
|
32
|
-
: null;
|
|
32
|
+
const regex = (0, regex_1.getRegex)(strSeparators);
|
|
33
33
|
// Divide by number delimiters
|
|
34
|
-
let parts = sliceByIndexes(input, numSeparators);
|
|
34
|
+
let parts = (0, slice_1.sliceByIndexes)(input, numSeparators);
|
|
35
35
|
// Divide by string delimiters
|
|
36
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,3 +1,5 @@
|
|
|
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];
|
|
@@ -24,9 +26,7 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
24
26
|
return [input];
|
|
25
27
|
}
|
|
26
28
|
// Precompile regex for string separators
|
|
27
|
-
const regex = strSeparators
|
|
28
|
-
? new RegExp(`[${strSeparators.map(escapeRegExp).join('')}]`, 'g')
|
|
29
|
-
: null;
|
|
29
|
+
const regex = getRegex(strSeparators);
|
|
30
30
|
// Divide by number delimiters
|
|
31
31
|
let parts = sliceByIndexes(input, numSeparators);
|
|
32
32
|
// Divide by string delimiters
|
|
@@ -35,25 +35,6 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
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[][];
|