@nyaomaru/divider 1.6.2 โ 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 +20 -6
- package/dist/index.d.cts +9 -6
- package/dist/index.d.ts +9 -6
- package/dist/index.js +20 -6
- 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,8 +112,8 @@ 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
119
|
// src/utils/option.ts
|
|
@@ -127,6 +138,9 @@ function applyDividerOptions(result, options) {
|
|
|
127
138
|
if (options.flatten) {
|
|
128
139
|
output = output.flat();
|
|
129
140
|
}
|
|
141
|
+
if (options.excludeEmpty) {
|
|
142
|
+
output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
|
|
143
|
+
}
|
|
130
144
|
return output;
|
|
131
145
|
}
|
|
132
146
|
|
|
@@ -151,7 +165,7 @@ function divider(input, ...args) {
|
|
|
151
165
|
return [];
|
|
152
166
|
}
|
|
153
167
|
if (isEmptyArray(args)) {
|
|
154
|
-
return
|
|
168
|
+
return ensureStringArray(input);
|
|
155
169
|
}
|
|
156
170
|
const { cleanedArgs, options } = extractOptions(args);
|
|
157
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,8 +82,8 @@ 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
89
|
// src/utils/option.ts
|
|
@@ -97,6 +108,9 @@ function applyDividerOptions(result, options) {
|
|
|
97
108
|
if (options.flatten) {
|
|
98
109
|
output = output.flat();
|
|
99
110
|
}
|
|
111
|
+
if (options.excludeEmpty) {
|
|
112
|
+
output = isNestedStringArray(output) ? output.filter((arr) => arr.some((s) => !isWhitespaceOnly(s))) : output.filter((s) => !isWhitespaceOnly(s));
|
|
113
|
+
}
|
|
100
114
|
return output;
|
|
101
115
|
}
|
|
102
116
|
|
|
@@ -121,7 +135,7 @@ function divider(input, ...args) {
|
|
|
121
135
|
return [];
|
|
122
136
|
}
|
|
123
137
|
if (isEmptyArray(args)) {
|
|
124
|
-
return
|
|
138
|
+
return ensureStringArray(input);
|
|
125
139
|
}
|
|
126
140
|
const { cleanedArgs, options } = extractOptions(args);
|
|
127
141
|
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|