@nyaomaru/divider 1.8.2 → 1.8.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/dist/index.cjs CHANGED
@@ -28,6 +28,13 @@ __export(index_exports, {
28
28
  });
29
29
  module.exports = __toCommonJS(index_exports);
30
30
 
31
+ // src/constants/index.ts
32
+ var DividerExcludeModes = {
33
+ NONE: "none",
34
+ EMPTY: "empty",
35
+ WHITESPACE: "whitespace"
36
+ };
37
+
31
38
  // src/utils/is.ts
32
39
  function isString(arg) {
33
40
  return typeof arg === "string";
@@ -35,9 +42,10 @@ function isString(arg) {
35
42
  function isNumber(arg) {
36
43
  return typeof arg === "number";
37
44
  }
38
- function isOptions(arg) {
39
- if (typeof arg !== "object" || arg === null) return false;
40
- return "flatten" in arg || "trim" in arg || "exclude" in arg;
45
+ function isOptions(value) {
46
+ if (typeof value !== "object" || value === null) return false;
47
+ const options = value;
48
+ return "flatten" in options || "trim" in options || "exclude" in options;
41
49
  }
42
50
  function isEmptyArray(input) {
43
51
  return Array.isArray(input) && input.length === 0;
@@ -61,7 +69,7 @@ function isEmptyString(s) {
61
69
  return s === "";
62
70
  }
63
71
  function isNoneMode(mode) {
64
- return mode === "none";
72
+ return mode === DividerExcludeModes.NONE;
65
73
  }
66
74
 
67
75
  // src/utils/regex.ts
@@ -221,23 +229,21 @@ function dividerLoop(input, size, options) {
221
229
  console.warn("dividerLoop: chunk size must be a positive number");
222
230
  return [];
223
231
  }
224
- const { startOffset = 0, maxChunks } = options ?? {};
232
+ const finalOptions = options ?? {};
233
+ const { startOffset = 0, maxChunks = 0 } = finalOptions;
225
234
  const applyChunking = (str) => {
226
- let chunks = divider(str, ...generateIndexes(str, size, startOffset));
227
- const shouldTruncateChunks = isNumber(maxChunks) && maxChunks > 0 && chunks.length > maxChunks;
228
- if (shouldTruncateChunks) {
229
- const head = chunks.slice(0, maxChunks - 1);
230
- const tail = chunks.slice(maxChunks - 1).join("");
231
- chunks = [...head, tail];
232
- }
233
- return chunks;
235
+ const chunks = divider(str, ...generateIndexes(str, size, startOffset));
236
+ return needsTruncation(chunks) ? truncateChunks(chunks) : chunks;
234
237
  };
235
- if (isString(input)) {
236
- const result2 = applyChunking(input);
237
- return applyDividerOptions(result2, options ?? {});
238
- }
239
- const result = input.map(applyChunking);
240
- return applyDividerOptions(result, options ?? {});
238
+ const needsTruncation = (chunks) => isNumber(maxChunks) && 0 < maxChunks && maxChunks < chunks.length;
239
+ const truncateChunks = (chunks) => {
240
+ const HEAD_COUNT = maxChunks - 1;
241
+ const head = chunks.slice(0, HEAD_COUNT);
242
+ const tail = chunks.slice(HEAD_COUNT).join("");
243
+ return [...head, tail];
244
+ };
245
+ const result = isString(input) ? applyChunking(input) : input.map(applyChunking);
246
+ return applyDividerOptions(result, finalOptions);
241
247
  }
242
248
 
243
249
  // src/utils/divide.ts
package/dist/index.d.cts CHANGED
@@ -1,18 +1,50 @@
1
- type DividerExcludeMode = 'none' | 'empty' | 'whitespace';
2
- type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
3
- type DividerOptions = {
1
+ declare const DividerExcludeModes: {
2
+ readonly NONE: "none";
3
+ readonly EMPTY: "empty";
4
+ readonly WHITESPACE: "whitespace";
5
+ };
6
+
7
+ type DividerExcludeMode = (typeof DividerExcludeModes)[keyof typeof DividerExcludeModes];
8
+ type StringInput = string;
9
+ type StringArrayInput = string[];
10
+ type DividerInput = StringInput | StringArrayInput;
11
+ type DividerStringResult = string[];
12
+ type DividerArrayResult = string[][];
13
+ type DividerResult<T extends DividerInput> = T extends StringInput ? DividerStringResult : DividerArrayResult;
14
+ interface DividerOptions {
15
+ /** If true, flattens nested arrays into a single array */
4
16
  flatten?: boolean;
17
+ /** If true, trims whitespace from each divided segment */
5
18
  trim?: boolean;
19
+ /** Controls how empty or whitespace segments are handled */
6
20
  exclude?: DividerExcludeMode;
7
- };
8
- type DividerLoopOptions = DividerOptions & {
21
+ }
22
+ interface DividerLoopOptions extends DividerOptions {
23
+ /** Starting position for the division (0-based) */
9
24
  startOffset?: number;
25
+ /** Maximum number of chunks to produce */
10
26
  maxChunks?: number;
11
- };
12
- type DividerSeparators = (number | string)[];
27
+ }
28
+ type NumericSeparator = number;
29
+ type StringSeparator = string;
30
+ type DividerSeparator = NumericSeparator | StringSeparator;
31
+ type DividerSeparators = DividerSeparator[];
13
32
  type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
14
33
 
15
- declare function divider<T extends string | string[]>(input: T, ...args: DividerArgs): DividerResult<T>;
34
+ /**
35
+ * Main divider function that splits input based on numeric positions or string delimiters.
36
+ *
37
+ * This function can:
38
+ * - Split a string or array of strings
39
+ * - Use numeric positions and/or string delimiters
40
+ * - Apply various options like flattening and trimming
41
+ *
42
+ * @param input - String or array of strings to divide
43
+ * @param args - Array of separators (numbers/strings) and optional options object
44
+ * @returns Divided string segments based on input type and options
45
+ * @throws {DividerValidationError} If input or separators are invalid
46
+ */
47
+ declare function divider<T extends DividerInput>(input: T, ...args: DividerArgs): DividerResult<T>;
16
48
 
17
49
  declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
18
50
 
@@ -22,4 +54,4 @@ declare function dividerLoop<T extends string | string[]>(input: T, size: number
22
54
 
23
55
  declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
24
56
 
25
- export { type DividerArgs, type DividerExcludeMode, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
57
+ export { type DividerArgs, type DividerArrayResult, type DividerExcludeMode, type DividerInput, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparator, type DividerSeparators, type DividerStringResult, type NumericSeparator, type StringArrayInput, type StringInput, type StringSeparator, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,50 @@
1
- type DividerExcludeMode = 'none' | 'empty' | 'whitespace';
2
- type DividerResult<T extends string | string[]> = T extends string ? string[] : string[][];
3
- type DividerOptions = {
1
+ declare const DividerExcludeModes: {
2
+ readonly NONE: "none";
3
+ readonly EMPTY: "empty";
4
+ readonly WHITESPACE: "whitespace";
5
+ };
6
+
7
+ type DividerExcludeMode = (typeof DividerExcludeModes)[keyof typeof DividerExcludeModes];
8
+ type StringInput = string;
9
+ type StringArrayInput = string[];
10
+ type DividerInput = StringInput | StringArrayInput;
11
+ type DividerStringResult = string[];
12
+ type DividerArrayResult = string[][];
13
+ type DividerResult<T extends DividerInput> = T extends StringInput ? DividerStringResult : DividerArrayResult;
14
+ interface DividerOptions {
15
+ /** If true, flattens nested arrays into a single array */
4
16
  flatten?: boolean;
17
+ /** If true, trims whitespace from each divided segment */
5
18
  trim?: boolean;
19
+ /** Controls how empty or whitespace segments are handled */
6
20
  exclude?: DividerExcludeMode;
7
- };
8
- type DividerLoopOptions = DividerOptions & {
21
+ }
22
+ interface DividerLoopOptions extends DividerOptions {
23
+ /** Starting position for the division (0-based) */
9
24
  startOffset?: number;
25
+ /** Maximum number of chunks to produce */
10
26
  maxChunks?: number;
11
- };
12
- type DividerSeparators = (number | string)[];
27
+ }
28
+ type NumericSeparator = number;
29
+ type StringSeparator = string;
30
+ type DividerSeparator = NumericSeparator | StringSeparator;
31
+ type DividerSeparators = DividerSeparator[];
13
32
  type DividerArgs = DividerSeparators | [...DividerSeparators, DividerOptions];
14
33
 
15
- declare function divider<T extends string | string[]>(input: T, ...args: DividerArgs): DividerResult<T>;
34
+ /**
35
+ * Main divider function that splits input based on numeric positions or string delimiters.
36
+ *
37
+ * This function can:
38
+ * - Split a string or array of strings
39
+ * - Use numeric positions and/or string delimiters
40
+ * - Apply various options like flattening and trimming
41
+ *
42
+ * @param input - String or array of strings to divide
43
+ * @param args - Array of separators (numbers/strings) and optional options object
44
+ * @returns Divided string segments based on input type and options
45
+ * @throws {DividerValidationError} If input or separators are invalid
46
+ */
47
+ declare function divider<T extends DividerInput>(input: T, ...args: DividerArgs): DividerResult<T>;
16
48
 
17
49
  declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
18
50
 
@@ -22,4 +54,4 @@ declare function dividerLoop<T extends string | string[]>(input: T, size: number
22
54
 
23
55
  declare function dividerNumberString<T extends string | string[]>(input: T, options?: DividerOptions): DividerResult<T>;
24
56
 
25
- export { type DividerArgs, type DividerExcludeMode, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparators, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
57
+ export { type DividerArgs, type DividerArrayResult, type DividerExcludeMode, type DividerInput, type DividerLoopOptions, type DividerOptions, type DividerResult, type DividerSeparator, type DividerSeparators, type DividerStringResult, type NumericSeparator, type StringArrayInput, type StringInput, type StringSeparator, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString };
package/dist/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ // src/constants/index.ts
2
+ var DividerExcludeModes = {
3
+ NONE: "none",
4
+ EMPTY: "empty",
5
+ WHITESPACE: "whitespace"
6
+ };
7
+
1
8
  // src/utils/is.ts
2
9
  function isString(arg) {
3
10
  return typeof arg === "string";
@@ -5,9 +12,10 @@ function isString(arg) {
5
12
  function isNumber(arg) {
6
13
  return typeof arg === "number";
7
14
  }
8
- function isOptions(arg) {
9
- if (typeof arg !== "object" || arg === null) return false;
10
- return "flatten" in arg || "trim" in arg || "exclude" in arg;
15
+ function isOptions(value) {
16
+ if (typeof value !== "object" || value === null) return false;
17
+ const options = value;
18
+ return "flatten" in options || "trim" in options || "exclude" in options;
11
19
  }
12
20
  function isEmptyArray(input) {
13
21
  return Array.isArray(input) && input.length === 0;
@@ -31,7 +39,7 @@ function isEmptyString(s) {
31
39
  return s === "";
32
40
  }
33
41
  function isNoneMode(mode) {
34
- return mode === "none";
42
+ return mode === DividerExcludeModes.NONE;
35
43
  }
36
44
 
37
45
  // src/utils/regex.ts
@@ -191,23 +199,21 @@ function dividerLoop(input, size, options) {
191
199
  console.warn("dividerLoop: chunk size must be a positive number");
192
200
  return [];
193
201
  }
194
- const { startOffset = 0, maxChunks } = options ?? {};
202
+ const finalOptions = options ?? {};
203
+ const { startOffset = 0, maxChunks = 0 } = finalOptions;
195
204
  const applyChunking = (str) => {
196
- let chunks = divider(str, ...generateIndexes(str, size, startOffset));
197
- const shouldTruncateChunks = isNumber(maxChunks) && maxChunks > 0 && chunks.length > maxChunks;
198
- if (shouldTruncateChunks) {
199
- const head = chunks.slice(0, maxChunks - 1);
200
- const tail = chunks.slice(maxChunks - 1).join("");
201
- chunks = [...head, tail];
202
- }
203
- return chunks;
205
+ const chunks = divider(str, ...generateIndexes(str, size, startOffset));
206
+ return needsTruncation(chunks) ? truncateChunks(chunks) : chunks;
204
207
  };
205
- if (isString(input)) {
206
- const result2 = applyChunking(input);
207
- return applyDividerOptions(result2, options ?? {});
208
- }
209
- const result = input.map(applyChunking);
210
- return applyDividerOptions(result, options ?? {});
208
+ const needsTruncation = (chunks) => isNumber(maxChunks) && 0 < maxChunks && maxChunks < chunks.length;
209
+ const truncateChunks = (chunks) => {
210
+ const HEAD_COUNT = maxChunks - 1;
211
+ const head = chunks.slice(0, HEAD_COUNT);
212
+ const tail = chunks.slice(HEAD_COUNT).join("");
213
+ return [...head, tail];
214
+ };
215
+ const result = isString(input) ? applyChunking(input) : input.map(applyChunking);
216
+ return applyDividerOptions(result, finalOptions);
211
217
  }
212
218
 
213
219
  // src/utils/divide.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.8.2",
4
+ "version": "1.8.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",