@nyaomaru/divider 1.7.3 → 1.7.4
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 +25 -2
- package/dist/index.cjs +11 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +11 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -130,6 +130,22 @@ const result = dividerLoop('abcdefghij', 3);
|
|
|
130
130
|
// Supports flatten option for string[]
|
|
131
131
|
const result2 = dividerLoop(['hello', 'world'], 2, { flatten: true });
|
|
132
132
|
// ['he', 'll', 'ow', 'or', 'ld']
|
|
133
|
+
|
|
134
|
+
// You can also control where to start dividing using `startOffset`
|
|
135
|
+
const result3 = dividerLoop('abcdefghij', 3, { startOffset: 1 });
|
|
136
|
+
// ['abcd', 'efg', 'hij']
|
|
137
|
+
|
|
138
|
+
// Combine with flatten and trim
|
|
139
|
+
const result4 = dividerLoop([' hello ', 'world '], 2, {
|
|
140
|
+
flatten: true,
|
|
141
|
+
trim: true,
|
|
142
|
+
startOffset: 1,
|
|
143
|
+
});
|
|
144
|
+
// ['h', 'el', 'lo', 'wor', 'ld']
|
|
145
|
+
|
|
146
|
+
// Limit the number of chunks using maxChunks
|
|
147
|
+
const result5 = dividerLoop('abcdefghij', 3, { maxChunks: 2 });
|
|
148
|
+
// ['abc', 'defghij']
|
|
133
149
|
```
|
|
134
150
|
|
|
135
151
|
### 📌 `dividerNumberString()` Usage
|
|
@@ -150,10 +166,10 @@ const result3 = dividerNumberString(['abc123', '45z'], { flatten: true });
|
|
|
150
166
|
// ['abc', '123', '45', 'z']
|
|
151
167
|
```
|
|
152
168
|
|
|
153
|
-
## 🎯 Options
|
|
169
|
+
## 🎯 General Options
|
|
154
170
|
|
|
155
171
|
| Option | Type | Default | Description |
|
|
156
|
-
| -------------- |
|
|
172
|
+
| -------------- | --------- | ------- | ------------------------------------------------------------------------- |
|
|
157
173
|
| `flatten` | `boolean` | `false` | If `true`, the resulting nested arrays are flattened into a single array. |
|
|
158
174
|
| `trim` | `boolean` | `false` | If `true`, trims whitespace from each divided segment. |
|
|
159
175
|
| `excludeEmpty` | `boolean` | `false` | If `true`, removes empty strings or strings with only whitespace. |
|
|
@@ -197,6 +213,13 @@ const result2 = divider([' a ', ' ', ' b'], ' ', {
|
|
|
197
213
|
// ['a', 'b']
|
|
198
214
|
```
|
|
199
215
|
|
|
216
|
+
## Special Options
|
|
217
|
+
|
|
218
|
+
| Option | Type | Default | Description |
|
|
219
|
+
| ------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------- |
|
|
220
|
+
| `startOffset` | `number` | `0` | Starting index offset when dividing into chunks (only for `dividerLoop`) |
|
|
221
|
+
| `maxChunks` | `number` | `∞` | Maximum number of chunks allowed. Extra chunks are joined into the last chunk. (only for `dividerLoop`) |
|
|
222
|
+
|
|
200
223
|
## 💡 Features
|
|
201
224
|
|
|
202
225
|
- 🧩 Flexible Division: Index-based and string-based separators
|
package/dist/index.cjs
CHANGED
|
@@ -205,7 +205,17 @@ function dividerLoop(input, size, options) {
|
|
|
205
205
|
console.warn("dividerLoop: chunk size must be a positive number");
|
|
206
206
|
return [];
|
|
207
207
|
}
|
|
208
|
-
const
|
|
208
|
+
const { startOffset = 0, maxChunks } = options ?? {};
|
|
209
|
+
const applyChunking = (str) => {
|
|
210
|
+
let chunks = divider(str, ...generateIndexes(str, size, startOffset));
|
|
211
|
+
const shouldTruncateChunks = isNumber(maxChunks) && maxChunks > 0 && chunks.length > maxChunks;
|
|
212
|
+
if (shouldTruncateChunks) {
|
|
213
|
+
const head = chunks.slice(0, maxChunks - 1);
|
|
214
|
+
const tail = chunks.slice(maxChunks - 1).join("");
|
|
215
|
+
chunks = [...head, tail];
|
|
216
|
+
}
|
|
217
|
+
return chunks;
|
|
218
|
+
};
|
|
209
219
|
if (isString(input)) {
|
|
210
220
|
const result2 = applyChunking(input);
|
|
211
221
|
return applyDividerOptions(result2, options ?? {});
|
package/dist/index.d.cts
CHANGED
|
@@ -5,6 +5,7 @@ type DividerResult<T extends string | string[]> = T extends string ? string[] :
|
|
|
5
5
|
type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
|
|
6
6
|
type DividerLoopOptions = DividerOptions & {
|
|
7
7
|
startOffset?: number;
|
|
8
|
+
maxChunks?: number;
|
|
8
9
|
};
|
|
9
10
|
type DividerSeparators = (number | string)[];
|
|
10
11
|
type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ type DividerResult<T extends string | string[]> = T extends string ? string[] :
|
|
|
5
5
|
type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
|
|
6
6
|
type DividerLoopOptions = DividerOptions & {
|
|
7
7
|
startOffset?: number;
|
|
8
|
+
maxChunks?: number;
|
|
8
9
|
};
|
|
9
10
|
type DividerSeparators = (number | string)[];
|
|
10
11
|
type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
|
package/dist/index.js
CHANGED
|
@@ -175,7 +175,17 @@ function dividerLoop(input, size, options) {
|
|
|
175
175
|
console.warn("dividerLoop: chunk size must be a positive number");
|
|
176
176
|
return [];
|
|
177
177
|
}
|
|
178
|
-
const
|
|
178
|
+
const { startOffset = 0, maxChunks } = options ?? {};
|
|
179
|
+
const applyChunking = (str) => {
|
|
180
|
+
let chunks = divider(str, ...generateIndexes(str, size, startOffset));
|
|
181
|
+
const shouldTruncateChunks = isNumber(maxChunks) && maxChunks > 0 && chunks.length > maxChunks;
|
|
182
|
+
if (shouldTruncateChunks) {
|
|
183
|
+
const head = chunks.slice(0, maxChunks - 1);
|
|
184
|
+
const tail = chunks.slice(maxChunks - 1).join("");
|
|
185
|
+
chunks = [...head, tail];
|
|
186
|
+
}
|
|
187
|
+
return chunks;
|
|
188
|
+
};
|
|
179
189
|
if (isString(input)) {
|
|
180
190
|
const result2 = applyChunking(input);
|
|
181
191
|
return applyDividerOptions(result2, options ?? {});
|