@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 +25 -19
- package/dist/index.d.cts +41 -9
- package/dist/index.d.ts +41 -9
- package/dist/index.js +25 -19
- package/package.json +1 -1
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(
|
|
39
|
-
if (typeof
|
|
40
|
-
|
|
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 ===
|
|
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
|
|
232
|
+
const finalOptions = options ?? {};
|
|
233
|
+
const { startOffset = 0, maxChunks = 0 } = finalOptions;
|
|
225
234
|
const applyChunking = (str) => {
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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(
|
|
9
|
-
if (typeof
|
|
10
|
-
|
|
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 ===
|
|
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
|
|
202
|
+
const finalOptions = options ?? {};
|
|
203
|
+
const { startOffset = 0, maxChunks = 0 } = finalOptions;
|
|
195
204
|
const applyChunking = (str) => {
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|