@nyaomaru/divider 1.7.2 → 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 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
@@ -187,12 +187,16 @@ function dividerLast(input, ...args) {
187
187
  }
188
188
 
189
189
  // src/utils/chunk.ts
190
- function generateIndexes(text, size) {
191
- const indexes = [];
192
- for (let i = size; i < text.length; i += size) {
193
- indexes.push(i);
190
+ function generateIndexes(str, size, startOffset = 0) {
191
+ if (!isPositiveInteger(size)) {
192
+ console.warn("generateIndexes: size must be a positive integer");
193
+ return [];
194
194
  }
195
- return indexes;
195
+ const result = [];
196
+ for (let i = startOffset + size; i < str.length; i += size) {
197
+ result.push(i);
198
+ }
199
+ return result;
196
200
  }
197
201
 
198
202
  // src/core/divider-loop.ts
@@ -201,7 +205,17 @@ function dividerLoop(input, size, options) {
201
205
  console.warn("dividerLoop: chunk size must be a positive number");
202
206
  return [];
203
207
  }
204
- const applyChunking = (str) => divider(str, ...generateIndexes(str, size));
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
+ };
205
219
  if (isString(input)) {
206
220
  const result2 = applyChunking(input);
207
221
  return applyDividerOptions(result2, options ?? {});
package/dist/index.d.cts CHANGED
@@ -3,6 +3,10 @@ declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
3
3
  type DividerOptionKey = (typeof dividerOptionKeys)[number];
4
4
  type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
5
5
  type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
6
+ type DividerLoopOptions = DividerOptions & {
7
+ startOffset?: number;
8
+ maxChunks?: number;
9
+ };
6
10
  type DividerSeparators = (number | string)[];
7
11
  type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
8
12
 
@@ -12,8 +16,8 @@ declare function dividerFirst(input: string | string[], ...args: DividerSeparato
12
16
 
13
17
  declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
14
18
 
15
- declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerOptions): DividerResult<T>;
19
+ declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerLoopOptions): DividerResult<T>;
16
20
 
17
21
  declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
18
22
 
19
- export { type DividerArgs, type DividerOptionKey, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
23
+ export { type DividerArgs, type DividerLoopOptions, type DividerOptionKey, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,10 @@ declare const dividerOptionKeys: readonly ["flatten", "trim", "excludeEmpty"];
3
3
  type DividerOptionKey = (typeof dividerOptionKeys)[number];
4
4
  type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
5
5
  type DividerOptions = Partial<Record<DividerOptionKey, boolean>>;
6
+ type DividerLoopOptions = DividerOptions & {
7
+ startOffset?: number;
8
+ maxChunks?: number;
9
+ };
6
10
  type DividerSeparators = (number | string)[];
7
11
  type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
8
12
 
@@ -12,8 +16,8 @@ declare function dividerFirst(input: string | string[], ...args: DividerSeparato
12
16
 
13
17
  declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
14
18
 
15
- declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerOptions): DividerResult<T>;
19
+ declare function dividerLoop<T extends string | string[]>(input: T, size: number, options?: DividerLoopOptions): DividerResult<T>;
16
20
 
17
21
  declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
18
22
 
19
- export { type DividerArgs, type DividerOptionKey, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
23
+ export { type DividerArgs, type DividerLoopOptions, type DividerOptionKey, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.js CHANGED
@@ -157,12 +157,16 @@ function dividerLast(input, ...args) {
157
157
  }
158
158
 
159
159
  // src/utils/chunk.ts
160
- function generateIndexes(text, size) {
161
- const indexes = [];
162
- for (let i = size; i < text.length; i += size) {
163
- indexes.push(i);
160
+ function generateIndexes(str, size, startOffset = 0) {
161
+ if (!isPositiveInteger(size)) {
162
+ console.warn("generateIndexes: size must be a positive integer");
163
+ return [];
164
164
  }
165
- return indexes;
165
+ const result = [];
166
+ for (let i = startOffset + size; i < str.length; i += size) {
167
+ result.push(i);
168
+ }
169
+ return result;
166
170
  }
167
171
 
168
172
  // src/core/divider-loop.ts
@@ -171,7 +175,17 @@ function dividerLoop(input, size, options) {
171
175
  console.warn("dividerLoop: chunk size must be a positive number");
172
176
  return [];
173
177
  }
174
- const applyChunking = (str) => divider(str, ...generateIndexes(str, size));
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
+ };
175
189
  if (isString(input)) {
176
190
  const result2 = applyChunking(input);
177
191
  return applyDividerOptions(result2, options ?? {});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.7.2",
4
+ "version": "1.7.4",
5
5
  "description": "To divide string or string[] with a given separator",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -35,19 +35,19 @@
35
35
  "author": "nyaomaru",
36
36
  "license": "MIT",
37
37
  "devDependencies": {
38
- "@eslint/js": "^9.22.0",
38
+ "@eslint/js": "^9.26.0",
39
39
  "@types/jest": "^29.5.14",
40
- "@types/node": "^22.13.4",
41
- "eslint": "^9.22.0",
42
- "eslint-config-prettier": "^10.0.1",
43
- "eslint-plugin-prettier": "^5.2.3",
40
+ "@types/node": "^22.15.12",
41
+ "eslint": "^9.26.0",
42
+ "eslint-config-prettier": "^10.1.2",
43
+ "eslint-plugin-prettier": "^5.4.0",
44
44
  "jest": "^29.7.0",
45
- "prettier": "^3.5.1",
46
- "ts-jest": "^29.2.5",
45
+ "prettier": "^3.5.3",
46
+ "ts-jest": "^29.3.2",
47
47
  "ts-node": "^10.9.2",
48
48
  "tsup": "^8.4.0",
49
- "tsx": "^4.19.3",
50
- "typescript": "^5.7.3"
49
+ "tsx": "^4.19.4",
50
+ "typescript": "^5.8.3"
51
51
  },
52
52
  "engines": {
53
53
  "node": ">=18"