@nyaomaru/divider 1.6.1 โ 1.7.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/README.md +24 -18
- package/dist/index.cjs +32 -20
- package/dist/index.d.cts +9 -6
- package/dist/index.d.ts +9 -6
- package/dist/index.js +32 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -152,10 +152,11 @@ const result3 = dividerNumberString(['abc123', '45z'], { flatten: true });
|
|
|
152
152
|
|
|
153
153
|
## ๐ฏ Options
|
|
154
154
|
|
|
155
|
-
| Option
|
|
156
|
-
|
|
|
157
|
-
| `flatten`
|
|
158
|
-
| `trim`
|
|
155
|
+
| Option | Type | Default | Description |
|
|
156
|
+
| -------------- | ----------| ------- | ------------------------------------------------------------------------- |
|
|
157
|
+
| `flatten` | `boolean` | `false` | If `true`, the resulting nested arrays are flattened into a single array. |
|
|
158
|
+
| `trim` | `boolean` | `false` | If `true`, trims whitespace from each divided segment. |
|
|
159
|
+
| `excludeEmpty` | `boolean` | `false` | If `true`, removes empty strings or strings with only whitespace. |
|
|
159
160
|
|
|
160
161
|
### `flatten` (default: `false`)
|
|
161
162
|
|
|
@@ -181,24 +182,29 @@ const result2 = divider([' a ', ' b c '], ' ', {
|
|
|
181
182
|
// ['a', 'b', 'c']
|
|
182
183
|
```
|
|
183
184
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
### ๐งฉ Flexible Division
|
|
187
|
-
|
|
188
|
-
- Supports both `index-based` and `string-based` division
|
|
189
|
-
- Supports `multiple separators` (mixing indexes and characters)
|
|
190
|
-
- Works with both `string` and `string[]` input
|
|
191
|
-
- Optional `flatten` and `trim` behaviors to control output format
|
|
192
|
-
- Includes `dividerNumberString()` to separate digits and letters
|
|
185
|
+
### `excludeEmpty` (default: `false`)
|
|
193
186
|
|
|
194
|
-
|
|
187
|
+
```ts
|
|
188
|
+
// Remove empty strings or strings with only spaces
|
|
189
|
+
const result = divider('a, ,b', ',', { trim: true, excludeEmpty: true });
|
|
190
|
+
// ['a', 'b']
|
|
195
191
|
|
|
196
|
-
|
|
197
|
-
|
|
192
|
+
const result2 = divider([' a ', ' ', ' b'], ' ', {
|
|
193
|
+
flatten: true,
|
|
194
|
+
trim: true,
|
|
195
|
+
excludeEmpty: true,
|
|
196
|
+
});
|
|
197
|
+
// ['a', 'b']
|
|
198
|
+
```
|
|
198
199
|
|
|
199
|
-
|
|
200
|
+
## ๐ก Features
|
|
200
201
|
|
|
201
|
-
-
|
|
202
|
+
- ๐งฉ Flexible Division: Index-based and string-based separators
|
|
203
|
+
- ๐งต Handles Nested Input: Supports both string and string[]
|
|
204
|
+
- ๐๏ธ Optional Behaviors: flatten, trim, excludeEmpty
|
|
205
|
+
- ๐ฏ Targeted Extractors: dividerFirst(), dividerLast()
|
|
206
|
+
- ๐ Loop Support: dividerLoop() for chunked division
|
|
207
|
+
- ๐ข Digit-Letter Splitter: dividerNumberString()
|
|
202
208
|
|
|
203
209
|
## ๐ Contributing
|
|
204
210
|
|
package/dist/index.cjs
CHANGED
|
@@ -28,11 +28,19 @@ __export(index_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(index_exports);
|
|
30
30
|
|
|
31
|
+
// src/core/constants.ts
|
|
32
|
+
var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
|
|
33
|
+
|
|
31
34
|
// src/utils/is.ts
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
function isString(arg) {
|
|
36
|
+
return typeof arg === "string";
|
|
37
|
+
}
|
|
38
|
+
function isNumber(arg) {
|
|
39
|
+
return typeof arg === "number";
|
|
40
|
+
}
|
|
34
41
|
function isOptions(arg) {
|
|
35
|
-
|
|
42
|
+
if (typeof arg !== "object" || arg === null) return false;
|
|
43
|
+
return dividerOptionKeys.some((key) => key in arg);
|
|
36
44
|
}
|
|
37
45
|
function isEmptyArray(input) {
|
|
38
46
|
return Array.isArray(input) && input.length === 0;
|
|
@@ -49,6 +57,9 @@ function isStringArray(input) {
|
|
|
49
57
|
function isNestedStringArray(input) {
|
|
50
58
|
return Array.isArray(input) && input.length > 0 && Array.isArray(input[0]) && input[0].length > 0 && isStringArray(input[0]);
|
|
51
59
|
}
|
|
60
|
+
function isWhitespaceOnly(s) {
|
|
61
|
+
return s.trim() === "";
|
|
62
|
+
}
|
|
52
63
|
|
|
53
64
|
// src/utils/regex.ts
|
|
54
65
|
var regexCache = /* @__PURE__ */ new Map();
|
|
@@ -101,11 +112,11 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
101
112
|
}
|
|
102
113
|
|
|
103
114
|
// src/utils/array.ts
|
|
104
|
-
function
|
|
105
|
-
return
|
|
115
|
+
function ensureStringArray(input) {
|
|
116
|
+
return typeof input === "string" ? [input] : input;
|
|
106
117
|
}
|
|
107
118
|
|
|
108
|
-
// src/utils/
|
|
119
|
+
// src/utils/option.ts
|
|
109
120
|
function extractOptions(args) {
|
|
110
121
|
const clonedArgs = [...args];
|
|
111
122
|
const lastArg = clonedArgs.at(-1);
|
|
@@ -118,6 +129,20 @@ function extractOptions(args) {
|
|
|
118
129
|
options
|
|
119
130
|
};
|
|
120
131
|
}
|
|
132
|
+
function applyDividerOptions(result, options) {
|
|
133
|
+
let output = result;
|
|
134
|
+
if (options.trim) {
|
|
135
|
+
const trim = (s) => s.trim();
|
|
136
|
+
output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
|
|
137
|
+
}
|
|
138
|
+
if (options.flatten) {
|
|
139
|
+
output = output.flat();
|
|
140
|
+
}
|
|
141
|
+
if (options.excludeEmpty) {
|
|
142
|
+
output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
|
|
143
|
+
}
|
|
144
|
+
return output;
|
|
145
|
+
}
|
|
121
146
|
|
|
122
147
|
// src/utils/separator.ts
|
|
123
148
|
function classifySeparators(args) {
|
|
@@ -131,19 +156,6 @@ function classifySeparators(args) {
|
|
|
131
156
|
);
|
|
132
157
|
}
|
|
133
158
|
|
|
134
|
-
// src/utils/option.ts
|
|
135
|
-
function applyDividerOptions(result, options) {
|
|
136
|
-
let output = result;
|
|
137
|
-
if (options.trim) {
|
|
138
|
-
const trim = (s) => s.trim();
|
|
139
|
-
output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
|
|
140
|
-
}
|
|
141
|
-
if (options.flatten) {
|
|
142
|
-
output = output.flat();
|
|
143
|
-
}
|
|
144
|
-
return output;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
159
|
// src/core/divider.ts
|
|
148
160
|
function divider(input, ...args) {
|
|
149
161
|
if (!isValidInput(input)) {
|
|
@@ -153,7 +165,7 @@ function divider(input, ...args) {
|
|
|
153
165
|
return [];
|
|
154
166
|
}
|
|
155
167
|
if (isEmptyArray(args)) {
|
|
156
|
-
return
|
|
168
|
+
return ensureStringArray(input);
|
|
157
169
|
}
|
|
158
170
|
const { cleanedArgs, options } = extractOptions(args);
|
|
159
171
|
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
|
|
2
|
+
|
|
3
|
+
type DividerOptionKey = (typeof dividerOptionKeys)[number];
|
|
4
|
+
type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
|
|
5
|
+
type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
|
|
3
6
|
type DividerSeparators = (number | string)[];
|
|
4
|
-
type DividerArgs
|
|
7
|
+
type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
|
|
5
8
|
|
|
6
|
-
declare function divider<T extends string | string[]
|
|
9
|
+
declare function divider<T extends string | string[]>(input: T, ...args: DividerArgs): DividerResult<T>;
|
|
7
10
|
|
|
8
11
|
declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
|
|
9
12
|
|
|
10
13
|
declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
|
|
11
14
|
|
|
12
|
-
declare function dividerLoop<T extends string | string[]
|
|
15
|
+
declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerOptions): DividerResult<T>;
|
|
13
16
|
|
|
14
|
-
declare function dividerNumberString<T extends string | string[]
|
|
17
|
+
declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
|
|
15
18
|
|
|
16
19
|
export { type DividerResult, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
|
|
2
|
+
|
|
3
|
+
type DividerOptionKey = (typeof dividerOptionKeys)[number];
|
|
4
|
+
type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
|
|
5
|
+
type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
|
|
3
6
|
type DividerSeparators = (number | string)[];
|
|
4
|
-
type DividerArgs
|
|
7
|
+
type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
|
|
5
8
|
|
|
6
|
-
declare function divider<T extends string | string[]
|
|
9
|
+
declare function divider<T extends string | string[]>(input: T, ...args: DividerArgs): DividerResult<T>;
|
|
7
10
|
|
|
8
11
|
declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
|
|
9
12
|
|
|
10
13
|
declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
|
|
11
14
|
|
|
12
|
-
declare function dividerLoop<T extends string | string[]
|
|
15
|
+
declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerOptions): DividerResult<T>;
|
|
13
16
|
|
|
14
|
-
declare function dividerNumberString<T extends string | string[]
|
|
17
|
+
declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
|
|
15
18
|
|
|
16
19
|
export { type DividerResult, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
// src/core/constants.ts
|
|
2
|
+
var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
|
|
3
|
+
|
|
1
4
|
// src/utils/is.ts
|
|
2
|
-
|
|
3
|
-
|
|
5
|
+
function isString(arg) {
|
|
6
|
+
return typeof arg === "string";
|
|
7
|
+
}
|
|
8
|
+
function isNumber(arg) {
|
|
9
|
+
return typeof arg === "number";
|
|
10
|
+
}
|
|
4
11
|
function isOptions(arg) {
|
|
5
|
-
|
|
12
|
+
if (typeof arg !== "object" || arg === null) return false;
|
|
13
|
+
return dividerOptionKeys.some((key) => key in arg);
|
|
6
14
|
}
|
|
7
15
|
function isEmptyArray(input) {
|
|
8
16
|
return Array.isArray(input) && input.length === 0;
|
|
@@ -19,6 +27,9 @@ function isStringArray(input) {
|
|
|
19
27
|
function isNestedStringArray(input) {
|
|
20
28
|
return Array.isArray(input) && input.length > 0 && Array.isArray(input[0]) && input[0].length > 0 && isStringArray(input[0]);
|
|
21
29
|
}
|
|
30
|
+
function isWhitespaceOnly(s) {
|
|
31
|
+
return s.trim() === "";
|
|
32
|
+
}
|
|
22
33
|
|
|
23
34
|
// src/utils/regex.ts
|
|
24
35
|
var regexCache = /* @__PURE__ */ new Map();
|
|
@@ -71,11 +82,11 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
71
82
|
}
|
|
72
83
|
|
|
73
84
|
// src/utils/array.ts
|
|
74
|
-
function
|
|
75
|
-
return
|
|
85
|
+
function ensureStringArray(input) {
|
|
86
|
+
return typeof input === "string" ? [input] : input;
|
|
76
87
|
}
|
|
77
88
|
|
|
78
|
-
// src/utils/
|
|
89
|
+
// src/utils/option.ts
|
|
79
90
|
function extractOptions(args) {
|
|
80
91
|
const clonedArgs = [...args];
|
|
81
92
|
const lastArg = clonedArgs.at(-1);
|
|
@@ -88,6 +99,20 @@ function extractOptions(args) {
|
|
|
88
99
|
options
|
|
89
100
|
};
|
|
90
101
|
}
|
|
102
|
+
function applyDividerOptions(result, options) {
|
|
103
|
+
let output = result;
|
|
104
|
+
if (options.trim) {
|
|
105
|
+
const trim = (s) => s.trim();
|
|
106
|
+
output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
|
|
107
|
+
}
|
|
108
|
+
if (options.flatten) {
|
|
109
|
+
output = output.flat();
|
|
110
|
+
}
|
|
111
|
+
if (options.excludeEmpty) {
|
|
112
|
+
output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
|
|
113
|
+
}
|
|
114
|
+
return output;
|
|
115
|
+
}
|
|
91
116
|
|
|
92
117
|
// src/utils/separator.ts
|
|
93
118
|
function classifySeparators(args) {
|
|
@@ -101,19 +126,6 @@ function classifySeparators(args) {
|
|
|
101
126
|
);
|
|
102
127
|
}
|
|
103
128
|
|
|
104
|
-
// src/utils/option.ts
|
|
105
|
-
function applyDividerOptions(result, options) {
|
|
106
|
-
let output = result;
|
|
107
|
-
if (options.trim) {
|
|
108
|
-
const trim = (s) => s.trim();
|
|
109
|
-
output = isNestedStringArray(output) ? output.map((row) => row.map(trim).filter(Boolean)) : output.map(trim).filter(Boolean);
|
|
110
|
-
}
|
|
111
|
-
if (options.flatten) {
|
|
112
|
-
output = output.flat();
|
|
113
|
-
}
|
|
114
|
-
return output;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
129
|
// src/core/divider.ts
|
|
118
130
|
function divider(input, ...args) {
|
|
119
131
|
if (!isValidInput(input)) {
|
|
@@ -123,7 +135,7 @@ function divider(input, ...args) {
|
|
|
123
135
|
return [];
|
|
124
136
|
}
|
|
125
137
|
if (isEmptyArray(args)) {
|
|
126
|
-
return
|
|
138
|
+
return ensureStringArray(input);
|
|
127
139
|
}
|
|
128
140
|
const { cleanedArgs, options } = extractOptions(args);
|
|
129
141
|
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|