@nyaomaru/divider 1.7.4 → 1.8.1
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 +26 -14
- package/dist/index.cjs +26 -6
- package/dist/index.d.cts +7 -5
- package/dist/index.d.ts +7 -5
- package/dist/index.js +26 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -168,20 +168,20 @@ const result3 = dividerNumberString(['abc123', '45z'], { flatten: true });
|
|
|
168
168
|
|
|
169
169
|
## 🎯 General Options
|
|
170
170
|
|
|
171
|
-
| Option
|
|
172
|
-
|
|
|
173
|
-
| `flatten`
|
|
174
|
-
| `trim`
|
|
175
|
-
| `
|
|
171
|
+
| Option | Type | Default | Description |
|
|
172
|
+
| --------- | ------------------------------------ | -------- | ------------------------------------------------------------------------- |
|
|
173
|
+
| `flatten` | `boolean` | `false` | If `true`, the resulting nested arrays are flattened into a single array. |
|
|
174
|
+
| `trim` | `boolean` | `false` | If `true`, trims whitespace from each divided segment. |
|
|
175
|
+
| `exclude` | `'none' / 'empty' / 'whitespace'` | `'none'` | See detailed explanation below |
|
|
176
176
|
|
|
177
177
|
### `flatten` (default: `false`)
|
|
178
178
|
|
|
179
179
|
```ts
|
|
180
180
|
const words = ['hello', 'world'];
|
|
181
|
-
const
|
|
181
|
+
const result = divider(words, 2);
|
|
182
182
|
// [['he', 'llo'], ['wo', 'rld']]
|
|
183
183
|
|
|
184
|
-
const
|
|
184
|
+
const result = divider(words, 2, { flatten: true });
|
|
185
185
|
// ['he', 'llo', 'wo', 'rld']
|
|
186
186
|
```
|
|
187
187
|
|
|
@@ -191,24 +191,36 @@ const result2 = divider(words, 2, { flatten: true });
|
|
|
191
191
|
const result = divider(' hello world ', 7, { trim: true });
|
|
192
192
|
// ['hello', 'world']
|
|
193
193
|
|
|
194
|
-
const
|
|
194
|
+
const result = divider([' a ', ' b c '], ' ', {
|
|
195
195
|
flatten: true,
|
|
196
196
|
trim: true,
|
|
197
197
|
});
|
|
198
198
|
// ['a', 'b', 'c']
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
-
### `
|
|
201
|
+
### `exclude` (default: `'none'`)
|
|
202
|
+
|
|
203
|
+
| Option | Description |
|
|
204
|
+
| -------------- | ------------------------------------------------------------------------ |
|
|
205
|
+
| `'none'` | Do not exclude any segments (all results are kept). |
|
|
206
|
+
| `'empty'` | Exclude empty strings (`''`). |
|
|
207
|
+
| `'whitespace'` | Exclude strings that contain only whitespace characters (e.g., `' '`). |
|
|
208
|
+
|
|
209
|
+
Control how segments like empty strings (`''`) or whitespace-only strings (`' '`) are handled.
|
|
202
210
|
|
|
203
211
|
```ts
|
|
204
|
-
// Remove empty strings
|
|
205
|
-
const result = divider('a
|
|
212
|
+
// Remove truly empty strings
|
|
213
|
+
const result = divider('a,,b', ',', { exclude: 'empty' });
|
|
206
214
|
// ['a', 'b']
|
|
207
215
|
|
|
208
|
-
|
|
209
|
-
|
|
216
|
+
// Remove both empty and whitespace-only strings
|
|
217
|
+
const result = divider('a, ,b', ',', { exclude: 'whitespace' });
|
|
218
|
+
// ['a', 'b']
|
|
219
|
+
|
|
220
|
+
// You can combine with `trim` for clearer results
|
|
221
|
+
const result = divider('a, ,b', ',', {
|
|
210
222
|
trim: true,
|
|
211
|
-
|
|
223
|
+
exclude: 'whitespace',
|
|
212
224
|
});
|
|
213
225
|
// ['a', 'b']
|
|
214
226
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -28,9 +28,6 @@ __export(index_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(index_exports);
|
|
30
30
|
|
|
31
|
-
// src/utils/constants.ts
|
|
32
|
-
var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
|
|
33
|
-
|
|
34
31
|
// src/utils/is.ts
|
|
35
32
|
function isString(arg) {
|
|
36
33
|
return typeof arg === "string";
|
|
@@ -40,7 +37,7 @@ function isNumber(arg) {
|
|
|
40
37
|
}
|
|
41
38
|
function isOptions(arg) {
|
|
42
39
|
if (typeof arg !== "object" || arg === null) return false;
|
|
43
|
-
return
|
|
40
|
+
return "flatten" in arg || "trim" in arg || "exclude" in arg;
|
|
44
41
|
}
|
|
45
42
|
function isEmptyArray(input) {
|
|
46
43
|
return Array.isArray(input) && input.length === 0;
|
|
@@ -60,6 +57,12 @@ function isNestedStringArray(input) {
|
|
|
60
57
|
function isWhitespaceOnly(s) {
|
|
61
58
|
return s.trim() === "";
|
|
62
59
|
}
|
|
60
|
+
function isEmptyString(s) {
|
|
61
|
+
return s === "";
|
|
62
|
+
}
|
|
63
|
+
function isNoneMode(mode) {
|
|
64
|
+
return mode === "none";
|
|
65
|
+
}
|
|
63
66
|
|
|
64
67
|
// src/utils/regex.ts
|
|
65
68
|
var regexCache = /* @__PURE__ */ new Map();
|
|
@@ -105,6 +108,9 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
105
108
|
if (isEmptyArray(numSeparators) && isEmptyArray(strSeparators)) {
|
|
106
109
|
return [input];
|
|
107
110
|
}
|
|
111
|
+
if (!Array.isArray(numSeparators) || !numSeparators.every(isNumber)) {
|
|
112
|
+
throw new Error("Invalid numeric separators");
|
|
113
|
+
}
|
|
108
114
|
const regex = getRegex(strSeparators);
|
|
109
115
|
const sortedNumSeparators = sortAscending(numSeparators);
|
|
110
116
|
const parts = sliceByIndexes(input, sortedNumSeparators);
|
|
@@ -116,6 +122,13 @@ function ensureStringArray(input) {
|
|
|
116
122
|
return isString(input) ? [input] : input;
|
|
117
123
|
}
|
|
118
124
|
|
|
125
|
+
// src/utils/exclude-predicate.ts
|
|
126
|
+
var excludePredicateMap = {
|
|
127
|
+
none: () => true,
|
|
128
|
+
empty: (s) => !isEmptyString(s),
|
|
129
|
+
whitespace: (s) => !isWhitespaceOnly(s)
|
|
130
|
+
};
|
|
131
|
+
|
|
119
132
|
// src/utils/option.ts
|
|
120
133
|
function extractOptions(args) {
|
|
121
134
|
const clonedArgs = [...args];
|
|
@@ -138,8 +151,15 @@ function applyDividerOptions(result, options) {
|
|
|
138
151
|
if (options.flatten) {
|
|
139
152
|
output = output.flat();
|
|
140
153
|
}
|
|
141
|
-
if (options.
|
|
142
|
-
|
|
154
|
+
if (!isNoneMode(options.exclude)) {
|
|
155
|
+
const exclude = options.exclude ?? "none";
|
|
156
|
+
let shouldKeep = () => true;
|
|
157
|
+
if (exclude in excludePredicateMap) {
|
|
158
|
+
shouldKeep = excludePredicateMap[exclude];
|
|
159
|
+
}
|
|
160
|
+
const filterNested = (arr) => arr.map((row) => row.filter(shouldKeep)).filter((row) => row.length > 0);
|
|
161
|
+
const filterFlat = (arr) => arr.filter(shouldKeep);
|
|
162
|
+
output = isNestedStringArray(output) ? filterNested(output) : filterFlat(output);
|
|
143
163
|
}
|
|
144
164
|
return output;
|
|
145
165
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type DividerOptionKey = (typeof dividerOptionKeys)[number];
|
|
1
|
+
type DividerExcludeMode = 'none' | 'empty' | 'whitespace';
|
|
4
2
|
type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
|
|
5
|
-
type DividerOptions =
|
|
3
|
+
type DividerOptions = {
|
|
4
|
+
flatten?: boolean;
|
|
5
|
+
trim?: boolean;
|
|
6
|
+
exclude?: DividerExcludeMode;
|
|
7
|
+
};
|
|
6
8
|
type DividerLoopOptions = DividerOptions & {
|
|
7
9
|
startOffset?: number;
|
|
8
10
|
maxChunks?: number;
|
|
@@ -20,4 +22,4 @@ declare function dividerLoop<T extends string | string[]>(input: T, size: number
|
|
|
20
22
|
|
|
21
23
|
declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
|
|
22
24
|
|
|
23
|
-
export { type DividerArgs, type
|
|
25
|
+
export { type DividerArgs, type DividerExcludeMode, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type DividerOptionKey = (typeof dividerOptionKeys)[number];
|
|
1
|
+
type DividerExcludeMode = 'none' | 'empty' | 'whitespace';
|
|
4
2
|
type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
|
|
5
|
-
type DividerOptions =
|
|
3
|
+
type DividerOptions = {
|
|
4
|
+
flatten?: boolean;
|
|
5
|
+
trim?: boolean;
|
|
6
|
+
exclude?: DividerExcludeMode;
|
|
7
|
+
};
|
|
6
8
|
type DividerLoopOptions = DividerOptions & {
|
|
7
9
|
startOffset?: number;
|
|
8
10
|
maxChunks?: number;
|
|
@@ -20,4 +22,4 @@ declare function dividerLoop<T extends string | string[]>(input: T, size: number
|
|
|
20
22
|
|
|
21
23
|
declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
|
|
22
24
|
|
|
23
|
-
export { type DividerArgs, type
|
|
25
|
+
export { type DividerArgs, type DividerExcludeMode, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// src/utils/constants.ts
|
|
2
|
-
var dividerOptionKeys = ["flatten", "trim", "excludeEmpty"];
|
|
3
|
-
|
|
4
1
|
// src/utils/is.ts
|
|
5
2
|
function isString(arg) {
|
|
6
3
|
return typeof arg === "string";
|
|
@@ -10,7 +7,7 @@ function isNumber(arg) {
|
|
|
10
7
|
}
|
|
11
8
|
function isOptions(arg) {
|
|
12
9
|
if (typeof arg !== "object" || arg === null) return false;
|
|
13
|
-
return
|
|
10
|
+
return "flatten" in arg || "trim" in arg || "exclude" in arg;
|
|
14
11
|
}
|
|
15
12
|
function isEmptyArray(input) {
|
|
16
13
|
return Array.isArray(input) && input.length === 0;
|
|
@@ -30,6 +27,12 @@ function isNestedStringArray(input) {
|
|
|
30
27
|
function isWhitespaceOnly(s) {
|
|
31
28
|
return s.trim() === "";
|
|
32
29
|
}
|
|
30
|
+
function isEmptyString(s) {
|
|
31
|
+
return s === "";
|
|
32
|
+
}
|
|
33
|
+
function isNoneMode(mode) {
|
|
34
|
+
return mode === "none";
|
|
35
|
+
}
|
|
33
36
|
|
|
34
37
|
// src/utils/regex.ts
|
|
35
38
|
var regexCache = /* @__PURE__ */ new Map();
|
|
@@ -75,6 +78,9 @@ function divideString(input, numSeparators, strSeparators) {
|
|
|
75
78
|
if (isEmptyArray(numSeparators) && isEmptyArray(strSeparators)) {
|
|
76
79
|
return [input];
|
|
77
80
|
}
|
|
81
|
+
if (!Array.isArray(numSeparators) || !numSeparators.every(isNumber)) {
|
|
82
|
+
throw new Error("Invalid numeric separators");
|
|
83
|
+
}
|
|
78
84
|
const regex = getRegex(strSeparators);
|
|
79
85
|
const sortedNumSeparators = sortAscending(numSeparators);
|
|
80
86
|
const parts = sliceByIndexes(input, sortedNumSeparators);
|
|
@@ -86,6 +92,13 @@ function ensureStringArray(input) {
|
|
|
86
92
|
return isString(input) ? [input] : input;
|
|
87
93
|
}
|
|
88
94
|
|
|
95
|
+
// src/utils/exclude-predicate.ts
|
|
96
|
+
var excludePredicateMap = {
|
|
97
|
+
none: () => true,
|
|
98
|
+
empty: (s) => !isEmptyString(s),
|
|
99
|
+
whitespace: (s) => !isWhitespaceOnly(s)
|
|
100
|
+
};
|
|
101
|
+
|
|
89
102
|
// src/utils/option.ts
|
|
90
103
|
function extractOptions(args) {
|
|
91
104
|
const clonedArgs = [...args];
|
|
@@ -108,8 +121,15 @@ function applyDividerOptions(result, options) {
|
|
|
108
121
|
if (options.flatten) {
|
|
109
122
|
output = output.flat();
|
|
110
123
|
}
|
|
111
|
-
if (options.
|
|
112
|
-
|
|
124
|
+
if (!isNoneMode(options.exclude)) {
|
|
125
|
+
const exclude = options.exclude ?? "none";
|
|
126
|
+
let shouldKeep = () => true;
|
|
127
|
+
if (exclude in excludePredicateMap) {
|
|
128
|
+
shouldKeep = excludePredicateMap[exclude];
|
|
129
|
+
}
|
|
130
|
+
const filterNested = (arr) => arr.map((row) => row.filter(shouldKeep)).filter((row) => row.length > 0);
|
|
131
|
+
const filterFlat = (arr) => arr.filter(shouldKeep);
|
|
132
|
+
output = isNestedStringArray(output) ? filterNested(output) : filterFlat(output);
|
|
113
133
|
}
|
|
114
134
|
return output;
|
|
115
135
|
}
|