@nyaomaru/divider 1.0.0 → 1.0.2
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 +58 -3
- package/dist/cjs/index.js +27 -36
- package/dist/esm/index.js +27 -36
- package/logo.svg +1 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,17 +1,38 @@
|
|
|
1
1
|
# Divider
|
|
2
2
|
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="logo.svg" width="200px" align="center" alt="Divider logo" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/@nyaomaru/divider">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/@nyaomaru/divider.svg?sanitize=true" alt="npm version">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/nyaomaru/divider/blob/main/LICENSE">
|
|
12
|
+
<img src="https://img.shields.io/npm/l/@nyaomaru/divider.svg?sanitize=true" alt="License">
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/@nyaomaru/divider">
|
|
15
|
+
<img src="https://img.shields.io/npm/dt/@nyaomaru/divider.svg" alt="npm downloads">
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://github.com/nyaomaru/divider/actions">
|
|
18
|
+
<img src="https://github.com/nyaomaru/divider/actions/workflows/release.yml/badge.svg" alt="Build Status">
|
|
19
|
+
</a>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
3
22
|
A simple utility to divide a `string` or `string[]` based on given indexes or delimiters.
|
|
4
23
|
|
|
5
24
|
## 🚀 Installation
|
|
6
25
|
|
|
7
26
|
```sh
|
|
8
|
-
pnpm install divider
|
|
27
|
+
pnpm install @nyaomaru/divider
|
|
9
28
|
```
|
|
10
29
|
|
|
11
30
|
## 📖 Usage
|
|
12
31
|
|
|
32
|
+
### Basic Examples
|
|
33
|
+
|
|
13
34
|
```ts
|
|
14
|
-
import divider from 'divider';
|
|
35
|
+
import { divider } from '@nyaomaru/divider';
|
|
15
36
|
|
|
16
37
|
// Divide a string by index positions
|
|
17
38
|
const helloArray = divider('hello', 1, 3);
|
|
@@ -33,13 +54,47 @@ const divideWithMultipleString = divider('hello', 'l');
|
|
|
33
54
|
const words = ['hello', 'world'];
|
|
34
55
|
const dividedWords = divider(words, 2);
|
|
35
56
|
// [['he', 'llo'], ['wo', 'rld']]
|
|
57
|
+
const dividedWordsWithFlattenOption = divider(words, 2, { flatten: true });
|
|
58
|
+
// ['he', 'llo', 'wo', 'rld']
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Advanced Examples
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
// Mixed usage of indexes and characters
|
|
65
|
+
const complexDivide = divider('hello world', 3, 'o');
|
|
66
|
+
// ['hel', 'l', ' w', 'rld']
|
|
67
|
+
|
|
68
|
+
// Nested array handling
|
|
69
|
+
const nestedArray = divider(['hello', 'new world'], ' ', 2);
|
|
70
|
+
// [['he', 'llo'], ['ne', 'w wor', 'ld']]
|
|
71
|
+
|
|
72
|
+
// Flatten option to get a single array
|
|
73
|
+
const flatArray = divider(['hello', 'new world'], ' ', 2, { flatten: true });
|
|
74
|
+
// ['he', 'llo', 'ne', 'w', 'wor', 'ld']
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## 🎯 Options
|
|
78
|
+
|
|
79
|
+
### `flatten` (default: `false`)
|
|
80
|
+
|
|
81
|
+
If `true`, the resulting nested arrays are flattened into a single array.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const words = ['hello', 'world'];
|
|
85
|
+
const result1 = divider(words, 2);
|
|
86
|
+
// [['he', 'llo'], ['wo', 'rld']]
|
|
87
|
+
|
|
88
|
+
const result2 = divider(words, 2, { flatten: true });
|
|
89
|
+
// ['he', 'llo', 'wo', 'rld']
|
|
36
90
|
```
|
|
37
91
|
|
|
38
92
|
## 💡 Features
|
|
39
93
|
|
|
40
94
|
- Supports both `index-based` and `string-based` division
|
|
41
95
|
- Works with both `strings` and `arrays of strings`
|
|
42
|
-
-
|
|
96
|
+
- Supports `multiple separators` (mixing indexes and characters).
|
|
97
|
+
- Provides an `optional flattening` feature for array results.
|
|
43
98
|
|
|
44
99
|
## 🛠 Contributing
|
|
45
100
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -16,44 +16,35 @@ function divideString(input, separators) {
|
|
|
16
16
|
if (separators.length === 0) {
|
|
17
17
|
return [input];
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
}
|
|
19
|
+
const { numSeparators, strSeparators } = separators.reduce((acc, separator) => {
|
|
20
|
+
typeof separator === 'number'
|
|
21
|
+
? acc.numSeparators.push(separator)
|
|
22
|
+
: acc.strSeparators.push(separator);
|
|
23
|
+
return acc;
|
|
24
|
+
}, { numSeparators: [], strSeparators: [] });
|
|
25
|
+
// Divide by number delimiters
|
|
26
|
+
let parts = sliceByIndexes(input, numSeparators);
|
|
29
27
|
// Divide by string delimiters
|
|
28
|
+
if (strSeparators.length) {
|
|
29
|
+
const regex = new RegExp(`[${strSeparators.join('')}]`, 'g');
|
|
30
|
+
parts = parts
|
|
31
|
+
.flatMap((part) => part.split(regex))
|
|
32
|
+
.filter((part) => part !== '');
|
|
33
|
+
}
|
|
34
|
+
return parts;
|
|
35
|
+
}
|
|
36
|
+
function sliceByIndexes(input, indexes) {
|
|
37
|
+
if (!indexes.length)
|
|
38
|
+
return [input];
|
|
39
|
+
indexes.sort((a, b) => a - b);
|
|
30
40
|
let parts = [];
|
|
31
|
-
let
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (part) {
|
|
37
|
-
parts.push(part);
|
|
38
|
-
startIndices.push(currentIndex);
|
|
39
|
-
}
|
|
40
|
-
currentIndex += part.length + 1;
|
|
41
|
-
});
|
|
42
|
-
// Divide by number delimiters
|
|
43
|
-
const sortedNumSeparators = [...new Set(numSeparators)].sort((a, b) => a - b);
|
|
44
|
-
let finalParts = [];
|
|
45
|
-
for (let i = 0; i < parts.length; i++) {
|
|
46
|
-
const part = parts[i];
|
|
47
|
-
const partStart = startIndices[i];
|
|
48
|
-
let prevIndex = 0;
|
|
49
|
-
const localSeparators = sortedNumSeparators
|
|
50
|
-
.filter((num) => num > partStart && num < partStart + part.length)
|
|
51
|
-
.map((num) => num - partStart);
|
|
52
|
-
for (const index of localSeparators) {
|
|
53
|
-
finalParts.push(part.slice(prevIndex, index));
|
|
54
|
-
prevIndex = index;
|
|
41
|
+
let start = 0;
|
|
42
|
+
for (const index of indexes) {
|
|
43
|
+
if (index > start && index < input.length) {
|
|
44
|
+
parts.push(input.slice(start, index));
|
|
45
|
+
start = index;
|
|
55
46
|
}
|
|
56
|
-
finalParts.push(part.slice(prevIndex));
|
|
57
47
|
}
|
|
58
|
-
|
|
48
|
+
parts.push(input.slice(start));
|
|
49
|
+
return parts.filter((part) => part !== '');
|
|
59
50
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -13,44 +13,35 @@ function divideString(input, separators) {
|
|
|
13
13
|
if (separators.length === 0) {
|
|
14
14
|
return [input];
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
}
|
|
16
|
+
const { numSeparators, strSeparators } = separators.reduce((acc, separator) => {
|
|
17
|
+
typeof separator === 'number'
|
|
18
|
+
? acc.numSeparators.push(separator)
|
|
19
|
+
: acc.strSeparators.push(separator);
|
|
20
|
+
return acc;
|
|
21
|
+
}, { numSeparators: [], strSeparators: [] });
|
|
22
|
+
// Divide by number delimiters
|
|
23
|
+
let parts = sliceByIndexes(input, numSeparators);
|
|
26
24
|
// Divide by string delimiters
|
|
25
|
+
if (strSeparators.length) {
|
|
26
|
+
const regex = new RegExp(`[${strSeparators.join('')}]`, 'g');
|
|
27
|
+
parts = parts
|
|
28
|
+
.flatMap((part) => part.split(regex))
|
|
29
|
+
.filter((part) => part !== '');
|
|
30
|
+
}
|
|
31
|
+
return parts;
|
|
32
|
+
}
|
|
33
|
+
function sliceByIndexes(input, indexes) {
|
|
34
|
+
if (!indexes.length)
|
|
35
|
+
return [input];
|
|
36
|
+
indexes.sort((a, b) => a - b);
|
|
27
37
|
let parts = [];
|
|
28
|
-
let
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (part) {
|
|
34
|
-
parts.push(part);
|
|
35
|
-
startIndices.push(currentIndex);
|
|
36
|
-
}
|
|
37
|
-
currentIndex += part.length + 1;
|
|
38
|
-
});
|
|
39
|
-
// Divide by number delimiters
|
|
40
|
-
const sortedNumSeparators = [...new Set(numSeparators)].sort((a, b) => a - b);
|
|
41
|
-
let finalParts = [];
|
|
42
|
-
for (let i = 0; i < parts.length; i++) {
|
|
43
|
-
const part = parts[i];
|
|
44
|
-
const partStart = startIndices[i];
|
|
45
|
-
let prevIndex = 0;
|
|
46
|
-
const localSeparators = sortedNumSeparators
|
|
47
|
-
.filter((num) => num > partStart && num < partStart + part.length)
|
|
48
|
-
.map((num) => num - partStart);
|
|
49
|
-
for (const index of localSeparators) {
|
|
50
|
-
finalParts.push(part.slice(prevIndex, index));
|
|
51
|
-
prevIndex = index;
|
|
38
|
+
let start = 0;
|
|
39
|
+
for (const index of indexes) {
|
|
40
|
+
if (index > start && index < input.length) {
|
|
41
|
+
parts.push(input.slice(start, index));
|
|
42
|
+
start = index;
|
|
52
43
|
}
|
|
53
|
-
finalParts.push(part.slice(prevIndex));
|
|
54
44
|
}
|
|
55
|
-
|
|
45
|
+
parts.push(input.slice(start));
|
|
46
|
+
return parts.filter((part) => part !== '');
|
|
56
47
|
}
|
package/logo.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?><svg id="_レイヤー_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 202.68 226.44"><defs><style>.cls-1{fill:#8ff476;}.cls-2{fill:#fff;}</style></defs><path class="cls-2" d="M5.99,196.51h12.52c9.24,0,14.75,4.83,14.75,12.48,0,10.56-7.57,17.45-18.64,17.45H0l5.99-29.93ZM14.84,220.76c7.18,0,11.42-4.88,11.42-11.38,0-4.44-2.87-7.18-8.51-7.18h-5.95l-3.72,18.56h6.76Z"/><path class="cls-2" d="M42.62,196.51h6.97l-5.99,29.93h-6.97l5.99-29.93Z"/><path class="cls-2" d="M88.39,196.51l-18.94,29.93h-6.8l-6.97-29.93h7.14l4.83,21.34,13.55-21.34h7.18Z"/><path class="cls-2" d="M94.73,196.51h6.97l-5.99,29.93h-6.97l5.99-29.93Z"/><path class="cls-2" d="M110.9,196.51h12.52c9.24,0,14.75,4.83,14.75,12.48,0,10.56-7.57,17.45-18.64,17.45h-14.62l5.99-29.93ZM119.74,220.76c7.18,0,11.42-4.88,11.42-11.38,0-4.44-2.87-7.18-8.51-7.18h-5.95l-3.72,18.56h6.76Z"/><path class="cls-2" d="M153.71,202.07l-1.28,6.5h13.9l-1.06,5.39h-13.94l-1.37,6.93h16.33l-1.15,5.56h-23.17l5.99-29.93h22.62l-1.16,5.56h-15.69Z"/><path class="cls-2" d="M194.13,217.2l5.21,9.24h-7.31l-4.7-8.38h-5.9l-1.67,8.38h-6.97l5.99-29.93h12.18c7.44,0,11.72,3.42,11.72,9.28s-3.21,9.75-8.55,11.41ZM195.67,206.43c0-2.91-2.05-4.27-5.6-4.27h-5.47l-2.09,10.39h5.9c4.57,0,7.27-2.14,7.27-6.12Z"/><polygon class="cls-1" points="186.42 3.89 135.99 168.36 106.58 168.36 106.58 168.46 54.61 168.46 54.61 161.35 87 161.35 160.49 0 170.69 11.22 186.42 3.89"/></svg>
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nyaomaru/divider",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"description": "To divide string or string[] with a given separator",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
7
7
|
"module": "dist/esm/index.js",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"dist",
|
|
23
23
|
"README.md",
|
|
24
24
|
"package.json",
|
|
25
|
-
"LICENSE"
|
|
25
|
+
"LICENSE",
|
|
26
|
+
"logo.svg"
|
|
26
27
|
],
|
|
27
28
|
"repository": {
|
|
28
29
|
"type": "git",
|