@nyaomaru/divider 1.0.0
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/LICENSE +21 -0
- package/README.md +64 -0
- package/dist/cjs/index.js +59 -0
- package/dist/esm/index.js +56 -0
- package/dist/index.d.ts +3 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 @nyaomaru
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Divider
|
|
2
|
+
|
|
3
|
+
A simple utility to divide a `string` or `string[]` based on given indexes or delimiters.
|
|
4
|
+
|
|
5
|
+
## 🚀 Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm install divider
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 📖 Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import divider from 'divider';
|
|
15
|
+
|
|
16
|
+
// Divide a string by index positions
|
|
17
|
+
const helloArray = divider('hello', 1, 3);
|
|
18
|
+
// ['h', 'el', 'lo']
|
|
19
|
+
|
|
20
|
+
const [hello1, hello2, ...restHello] = divider('hello', 1, 3, 4);
|
|
21
|
+
// hello1 = 'h'
|
|
22
|
+
// hello2 = 'el'
|
|
23
|
+
// restHello = ['l', 'o']
|
|
24
|
+
|
|
25
|
+
// Divide a string using a character separator
|
|
26
|
+
const divideWithString = divider('hello', 'e');
|
|
27
|
+
// ['h', 'llo']
|
|
28
|
+
|
|
29
|
+
const divideWithMultipleString = divider('hello', 'l');
|
|
30
|
+
// ['he', 'o']
|
|
31
|
+
|
|
32
|
+
// Divide an array of strings
|
|
33
|
+
const words = ['hello', 'world'];
|
|
34
|
+
const dividedWords = divider(words, 2);
|
|
35
|
+
// [['he', 'llo'], ['wo', 'rld']]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 💡 Features
|
|
39
|
+
|
|
40
|
+
- Supports both `index-based` and `string-based` division
|
|
41
|
+
- Works with both `strings` and `arrays of strings`
|
|
42
|
+
- Optional `flattening` for array results
|
|
43
|
+
|
|
44
|
+
## 🛠Contributing
|
|
45
|
+
|
|
46
|
+
Welcome your contributions! If you want to add features or fix issues, feel free to submit a PR!
|
|
47
|
+
|
|
48
|
+
### Setup
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
pnpm install
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Test
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
pnpm test
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Contribution Guidelines
|
|
61
|
+
|
|
62
|
+
- If you add new functions, please add corresponding tests in the `tests` directory.
|
|
63
|
+
- No strict rules—just keep it clean and readable!
|
|
64
|
+
- Thank you for your contribution. 😺
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.divider = divider;
|
|
4
|
+
function divider(input, ...args) {
|
|
5
|
+
// extract the options from the input
|
|
6
|
+
const lastArg = args[args.length - 1];
|
|
7
|
+
const options = typeof lastArg === 'object' ? args.pop() : {};
|
|
8
|
+
const separators = args;
|
|
9
|
+
if (typeof input === 'string') {
|
|
10
|
+
return divideString(input, separators);
|
|
11
|
+
}
|
|
12
|
+
const result = input.map((item) => divideString(item, separators));
|
|
13
|
+
return options.flatten ? result.flat() : result;
|
|
14
|
+
}
|
|
15
|
+
function divideString(input, separators) {
|
|
16
|
+
if (separators.length === 0) {
|
|
17
|
+
return [input];
|
|
18
|
+
}
|
|
19
|
+
let numSeparators = [];
|
|
20
|
+
let strSeparators = [];
|
|
21
|
+
for (const separator of separators) {
|
|
22
|
+
if (typeof separator === 'number') {
|
|
23
|
+
numSeparators.push(separator);
|
|
24
|
+
}
|
|
25
|
+
else if (typeof separator === 'string') {
|
|
26
|
+
strSeparators.push(separator);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Divide by string delimiters
|
|
30
|
+
let parts = [];
|
|
31
|
+
let startIndices = [];
|
|
32
|
+
let currentIndex = 0;
|
|
33
|
+
input
|
|
34
|
+
.split(new RegExp(`[${strSeparators.join('')}]`, 'g'))
|
|
35
|
+
.forEach((part) => {
|
|
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;
|
|
55
|
+
}
|
|
56
|
+
finalParts.push(part.slice(prevIndex));
|
|
57
|
+
}
|
|
58
|
+
return finalParts;
|
|
59
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export function divider(input, ...args) {
|
|
2
|
+
// extract the options from the input
|
|
3
|
+
const lastArg = args[args.length - 1];
|
|
4
|
+
const options = typeof lastArg === 'object' ? args.pop() : {};
|
|
5
|
+
const separators = args;
|
|
6
|
+
if (typeof input === 'string') {
|
|
7
|
+
return divideString(input, separators);
|
|
8
|
+
}
|
|
9
|
+
const result = input.map((item) => divideString(item, separators));
|
|
10
|
+
return options.flatten ? result.flat() : result;
|
|
11
|
+
}
|
|
12
|
+
function divideString(input, separators) {
|
|
13
|
+
if (separators.length === 0) {
|
|
14
|
+
return [input];
|
|
15
|
+
}
|
|
16
|
+
let numSeparators = [];
|
|
17
|
+
let strSeparators = [];
|
|
18
|
+
for (const separator of separators) {
|
|
19
|
+
if (typeof separator === 'number') {
|
|
20
|
+
numSeparators.push(separator);
|
|
21
|
+
}
|
|
22
|
+
else if (typeof separator === 'string') {
|
|
23
|
+
strSeparators.push(separator);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// Divide by string delimiters
|
|
27
|
+
let parts = [];
|
|
28
|
+
let startIndices = [];
|
|
29
|
+
let currentIndex = 0;
|
|
30
|
+
input
|
|
31
|
+
.split(new RegExp(`[${strSeparators.join('')}]`, 'g'))
|
|
32
|
+
.forEach((part) => {
|
|
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;
|
|
52
|
+
}
|
|
53
|
+
finalParts.push(part.slice(prevIndex));
|
|
54
|
+
}
|
|
55
|
+
return finalParts;
|
|
56
|
+
}
|
package/dist/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nyaomaru/divider",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "To divide string or string[] with a given separator",
|
|
6
|
+
"main": "dist/cjs/index.js",
|
|
7
|
+
"module": "dist/esm/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./dist/esm/index.js",
|
|
11
|
+
"require": "./dist/cjs/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"divider",
|
|
16
|
+
"divide",
|
|
17
|
+
"split",
|
|
18
|
+
"string",
|
|
19
|
+
"array"
|
|
20
|
+
],
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"package.json",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/nyaomaru/divider.git"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/nyaomaru/divider.git",
|
|
32
|
+
"author": "nyaomaru",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@eslint/js": "^9.21.0",
|
|
36
|
+
"@types/jest": "^29.5.14",
|
|
37
|
+
"@types/node": "^22.13.4",
|
|
38
|
+
"eslint": "^9.20.1",
|
|
39
|
+
"eslint-config-prettier": "^10.0.1",
|
|
40
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
41
|
+
"jest": "^29.7.0",
|
|
42
|
+
"prettier": "^3.5.1",
|
|
43
|
+
"ts-jest": "^29.2.5",
|
|
44
|
+
"ts-node": "^10.9.2",
|
|
45
|
+
"typescript": "^5.7.3"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"test": "jest",
|
|
49
|
+
"lint": "eslint .",
|
|
50
|
+
"build": "tsc --module ESNext --outDir dist/esm && tsc --module CommonJS --outDir dist/cjs"
|
|
51
|
+
}
|
|
52
|
+
}
|