@nyaomaru/divider 1.5.0 → 1.5.2

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.
Files changed (47) hide show
  1. package/dist/index.cjs +193 -0
  2. package/dist/index.d.cts +16 -0
  3. package/dist/index.d.ts +16 -0
  4. package/dist/index.js +163 -0
  5. package/package.json +14 -8
  6. package/dist/cjs/core/divider-first.js +0 -9
  7. package/dist/cjs/core/divider-last.js +0 -9
  8. package/dist/cjs/core/divider-loop.js +0 -23
  9. package/dist/cjs/core/divider-number-string.js +0 -15
  10. package/dist/cjs/core/divider.js +0 -34
  11. package/dist/cjs/core/parser.js +0 -21
  12. package/dist/cjs/core/types.js +0 -2
  13. package/dist/cjs/index.js +0 -11
  14. package/dist/cjs/utils/array.js +0 -6
  15. package/dist/cjs/utils/chunk.js +0 -10
  16. package/dist/cjs/utils/is.js +0 -19
  17. package/dist/cjs/utils/regex.js +0 -18
  18. package/dist/cjs/utils/slice.js +0 -20
  19. package/dist/cjs/utils/sort.js +0 -6
  20. package/dist/esm/core/divider-first.js +0 -6
  21. package/dist/esm/core/divider-last.js +0 -6
  22. package/dist/esm/core/divider-loop.js +0 -20
  23. package/dist/esm/core/divider-number-string.js +0 -12
  24. package/dist/esm/core/divider.js +0 -31
  25. package/dist/esm/core/parser.js +0 -18
  26. package/dist/esm/core/types.js +0 -1
  27. package/dist/esm/index.js +0 -4
  28. package/dist/esm/utils/array.js +0 -3
  29. package/dist/esm/utils/chunk.js +0 -7
  30. package/dist/esm/utils/is.js +0 -11
  31. package/dist/esm/utils/regex.js +0 -15
  32. package/dist/esm/utils/slice.js +0 -17
  33. package/dist/esm/utils/sort.js +0 -3
  34. package/dist/types/core/divider-first.d.ts +0 -2
  35. package/dist/types/core/divider-last.d.ts +0 -2
  36. package/dist/types/core/divider-loop.d.ts +0 -2
  37. package/dist/types/core/divider-number-string.d.ts +0 -2
  38. package/dist/types/core/divider.d.ts +0 -2
  39. package/dist/types/core/parser.d.ts +0 -1
  40. package/dist/types/core/types.d.ts +0 -6
  41. package/dist/types/index.d.ts +0 -5
  42. package/dist/types/utils/array.d.ts +0 -1
  43. package/dist/types/utils/chunk.d.ts +0 -1
  44. package/dist/types/utils/is.d.ts +0 -7
  45. package/dist/types/utils/regex.d.ts +0 -1
  46. package/dist/types/utils/slice.d.ts +0 -1
  47. package/dist/types/utils/sort.d.ts +0 -1
package/dist/index.cjs ADDED
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ divider: () => divider,
24
+ dividerFirst: () => dividerFirst,
25
+ dividerLast: () => dividerLast,
26
+ dividerLoop: () => dividerLoop
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/utils/is.ts
31
+ var isString = (arg) => typeof arg === "string";
32
+ var isNumber = (arg) => typeof arg === "number";
33
+ function isOptions(arg) {
34
+ return typeof arg === "object" && arg !== null && "flatten" in arg;
35
+ }
36
+ function isEmptyArray(input) {
37
+ return Array.isArray(input) && input.length === 0;
38
+ }
39
+ function isPositiveInteger(value) {
40
+ return Number.isInteger(value) && value > 0;
41
+ }
42
+ function isValidInput(input) {
43
+ return isString(input) || Array.isArray(input) && input.every(isString);
44
+ }
45
+
46
+ // src/utils/regex.ts
47
+ var regexCache = /* @__PURE__ */ new Map();
48
+ function getRegex(separators) {
49
+ if (isEmptyArray(separators)) return null;
50
+ const key = JSON.stringify(separators);
51
+ if (!regexCache.has(key)) {
52
+ const pattern = separators.reduce(
53
+ (acc, sep) => acc + escapeRegExp(sep),
54
+ ""
55
+ );
56
+ regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
57
+ }
58
+ return regexCache.get(key);
59
+ }
60
+ function escapeRegExp(str) {
61
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
62
+ }
63
+
64
+ // src/utils/slice.ts
65
+ function sliceByIndexes(input, indexes) {
66
+ if (isEmptyArray(indexes)) return [input];
67
+ const parts = [];
68
+ let start = 0;
69
+ for (const index of indexes) {
70
+ if (index <= start || input.length <= index) continue;
71
+ parts.push(input.slice(start, index));
72
+ start = index;
73
+ }
74
+ if (start < input.length) {
75
+ parts.push(input.slice(start));
76
+ }
77
+ return parts;
78
+ }
79
+
80
+ // src/utils/sort.ts
81
+ function sortAscending(numbers) {
82
+ return [...numbers].sort((a, b) => a - b);
83
+ }
84
+
85
+ // src/core/parser.ts
86
+ function divideString(input, numSeparators, strSeparators) {
87
+ if (isEmptyArray(numSeparators) && isEmptyArray(strSeparators)) {
88
+ return [input];
89
+ }
90
+ const regex = getRegex(strSeparators);
91
+ const sortedNumSeparators = sortAscending(numSeparators);
92
+ const parts = sliceByIndexes(input, sortedNumSeparators);
93
+ return regex ? parts.flatMap((part) => part.split(regex)).filter(Boolean) : parts;
94
+ }
95
+
96
+ // src/utils/array.ts
97
+ function ensureArray(input) {
98
+ return Array.isArray(input) ? input : [input];
99
+ }
100
+
101
+ // src/utils/options.ts
102
+ function extractOptions(args) {
103
+ const clonedArgs = [...args];
104
+ const lastArg = clonedArgs.at(-1);
105
+ const options = isOptions(lastArg) ? (clonedArgs.pop(), lastArg) : {};
106
+ const cleanedArgs = clonedArgs.filter(
107
+ (arg) => isString(arg) || isNumber(arg)
108
+ );
109
+ return {
110
+ cleanedArgs,
111
+ options
112
+ };
113
+ }
114
+
115
+ // src/utils/separator.ts
116
+ function classifySeparators(args) {
117
+ return args.reduce(
118
+ (acc, arg) => {
119
+ if (isNumber(arg)) acc.numSeparators.push(arg);
120
+ else if (isString(arg)) acc.strSeparators.push(arg);
121
+ return acc;
122
+ },
123
+ { numSeparators: [], strSeparators: [] }
124
+ );
125
+ }
126
+
127
+ // src/core/divider.ts
128
+ function divider(input, ...args) {
129
+ if (!isValidInput(input)) {
130
+ console.warn(
131
+ "divider: 'input' must be a string or an array of strings. So returning an empty array."
132
+ );
133
+ return [];
134
+ }
135
+ if (isEmptyArray(args)) {
136
+ return ensureArray(input);
137
+ }
138
+ const { cleanedArgs, options } = extractOptions(args);
139
+ const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
140
+ if (isString(input)) {
141
+ return divideString(input, numSeparators, strSeparators);
142
+ }
143
+ const result = input.map(
144
+ (item) => divideString(item, numSeparators, strSeparators)
145
+ );
146
+ return options.flatten ? result.flat() : result;
147
+ }
148
+
149
+ // src/core/divider-first.ts
150
+ function dividerFirst(input, ...args) {
151
+ const result = divider(input, ...args, { flatten: true });
152
+ return result[0] ?? "";
153
+ }
154
+
155
+ // src/core/divider-last.ts
156
+ function dividerLast(input, ...args) {
157
+ const result = divider(input, ...args, { flatten: true });
158
+ return result.at(-1) ?? "";
159
+ }
160
+
161
+ // src/utils/chunk.ts
162
+ function generateIndexes(text, size) {
163
+ const indexes = [];
164
+ for (let i = size; i < text.length; i += size) {
165
+ indexes.push(i);
166
+ }
167
+ return indexes;
168
+ }
169
+
170
+ // src/core/divider-loop.ts
171
+ function dividerLoop(input, size, options) {
172
+ if (!isPositiveInteger(size)) {
173
+ console.warn("dividerLoop: chunk size must be a positive number");
174
+ return [];
175
+ }
176
+ if (isString(input)) {
177
+ const indexes = generateIndexes(input, size);
178
+ return divider(input, ...indexes);
179
+ }
180
+ const result = input.map((item) => {
181
+ const indexes = generateIndexes(item, size);
182
+ return divider(item, ...indexes);
183
+ });
184
+ const flatten = options?.flatten ?? false;
185
+ return flatten ? result.flat() : result;
186
+ }
187
+ // Annotate the CommonJS export names for ESM import in node:
188
+ 0 && (module.exports = {
189
+ divider,
190
+ dividerFirst,
191
+ dividerLast,
192
+ dividerLoop
193
+ });
@@ -0,0 +1,16 @@
1
+ type DividerResult<T extends string | string[], F extends boolean = false> = T extends string ? string[] : F extends true ? string[] : string[][];
2
+ type DividerOptions<F extends boolean> = {
3
+ flatten?: F;
4
+ };
5
+ type DividerSeparators = (number | string)[];
6
+ type DividerArgs<F extends boolean> = DividerSeparators | [...DividerSeparators, DividerOptions<F>];
7
+
8
+ declare function divider<T extends string | string[], F extends boolean>(input: T, ...args: DividerArgs<F>): DividerResult<T, F>;
9
+
10
+ declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
11
+
12
+ declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
13
+
14
+ declare function dividerLoop<T extends string | string[], F extends boolean>(input: T, size: number, options?: DividerOptions<F>): DividerResult<T, F>;
15
+
16
+ export { type DividerResult, divider, dividerFirst, dividerLast, dividerLoop };
@@ -0,0 +1,16 @@
1
+ type DividerResult<T extends string | string[], F extends boolean = false> = T extends string ? string[] : F extends true ? string[] : string[][];
2
+ type DividerOptions<F extends boolean> = {
3
+ flatten?: F;
4
+ };
5
+ type DividerSeparators = (number | string)[];
6
+ type DividerArgs<F extends boolean> = DividerSeparators | [...DividerSeparators, DividerOptions<F>];
7
+
8
+ declare function divider<T extends string | string[], F extends boolean>(input: T, ...args: DividerArgs<F>): DividerResult<T, F>;
9
+
10
+ declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
11
+
12
+ declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
13
+
14
+ declare function dividerLoop<T extends string | string[], F extends boolean>(input: T, size: number, options?: DividerOptions<F>): DividerResult<T, F>;
15
+
16
+ export { type DividerResult, divider, dividerFirst, dividerLast, dividerLoop };
package/dist/index.js ADDED
@@ -0,0 +1,163 @@
1
+ // src/utils/is.ts
2
+ var isString = (arg) => typeof arg === "string";
3
+ var isNumber = (arg) => typeof arg === "number";
4
+ function isOptions(arg) {
5
+ return typeof arg === "object" && arg !== null && "flatten" in arg;
6
+ }
7
+ function isEmptyArray(input) {
8
+ return Array.isArray(input) && input.length === 0;
9
+ }
10
+ function isPositiveInteger(value) {
11
+ return Number.isInteger(value) && value > 0;
12
+ }
13
+ function isValidInput(input) {
14
+ return isString(input) || Array.isArray(input) && input.every(isString);
15
+ }
16
+
17
+ // src/utils/regex.ts
18
+ var regexCache = /* @__PURE__ */ new Map();
19
+ function getRegex(separators) {
20
+ if (isEmptyArray(separators)) return null;
21
+ const key = JSON.stringify(separators);
22
+ if (!regexCache.has(key)) {
23
+ const pattern = separators.reduce(
24
+ (acc, sep) => acc + escapeRegExp(sep),
25
+ ""
26
+ );
27
+ regexCache.set(key, new RegExp(`[${pattern}]`, "g"));
28
+ }
29
+ return regexCache.get(key);
30
+ }
31
+ function escapeRegExp(str) {
32
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33
+ }
34
+
35
+ // src/utils/slice.ts
36
+ function sliceByIndexes(input, indexes) {
37
+ if (isEmptyArray(indexes)) return [input];
38
+ const parts = [];
39
+ let start = 0;
40
+ for (const index of indexes) {
41
+ if (index <= start || input.length <= index) continue;
42
+ parts.push(input.slice(start, index));
43
+ start = index;
44
+ }
45
+ if (start < input.length) {
46
+ parts.push(input.slice(start));
47
+ }
48
+ return parts;
49
+ }
50
+
51
+ // src/utils/sort.ts
52
+ function sortAscending(numbers) {
53
+ return [...numbers].sort((a, b) => a - b);
54
+ }
55
+
56
+ // src/core/parser.ts
57
+ function divideString(input, numSeparators, strSeparators) {
58
+ if (isEmptyArray(numSeparators) && isEmptyArray(strSeparators)) {
59
+ return [input];
60
+ }
61
+ const regex = getRegex(strSeparators);
62
+ const sortedNumSeparators = sortAscending(numSeparators);
63
+ const parts = sliceByIndexes(input, sortedNumSeparators);
64
+ return regex ? parts.flatMap((part) => part.split(regex)).filter(Boolean) : parts;
65
+ }
66
+
67
+ // src/utils/array.ts
68
+ function ensureArray(input) {
69
+ return Array.isArray(input) ? input : [input];
70
+ }
71
+
72
+ // src/utils/options.ts
73
+ function extractOptions(args) {
74
+ const clonedArgs = [...args];
75
+ const lastArg = clonedArgs.at(-1);
76
+ const options = isOptions(lastArg) ? (clonedArgs.pop(), lastArg) : {};
77
+ const cleanedArgs = clonedArgs.filter(
78
+ (arg) => isString(arg) || isNumber(arg)
79
+ );
80
+ return {
81
+ cleanedArgs,
82
+ options
83
+ };
84
+ }
85
+
86
+ // src/utils/separator.ts
87
+ function classifySeparators(args) {
88
+ return args.reduce(
89
+ (acc, arg) => {
90
+ if (isNumber(arg)) acc.numSeparators.push(arg);
91
+ else if (isString(arg)) acc.strSeparators.push(arg);
92
+ return acc;
93
+ },
94
+ { numSeparators: [], strSeparators: [] }
95
+ );
96
+ }
97
+
98
+ // src/core/divider.ts
99
+ function divider(input, ...args) {
100
+ if (!isValidInput(input)) {
101
+ console.warn(
102
+ "divider: 'input' must be a string or an array of strings. So returning an empty array."
103
+ );
104
+ return [];
105
+ }
106
+ if (isEmptyArray(args)) {
107
+ return ensureArray(input);
108
+ }
109
+ const { cleanedArgs, options } = extractOptions(args);
110
+ const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
111
+ if (isString(input)) {
112
+ return divideString(input, numSeparators, strSeparators);
113
+ }
114
+ const result = input.map(
115
+ (item) => divideString(item, numSeparators, strSeparators)
116
+ );
117
+ return options.flatten ? result.flat() : result;
118
+ }
119
+
120
+ // src/core/divider-first.ts
121
+ function dividerFirst(input, ...args) {
122
+ const result = divider(input, ...args, { flatten: true });
123
+ return result[0] ?? "";
124
+ }
125
+
126
+ // src/core/divider-last.ts
127
+ function dividerLast(input, ...args) {
128
+ const result = divider(input, ...args, { flatten: true });
129
+ return result.at(-1) ?? "";
130
+ }
131
+
132
+ // src/utils/chunk.ts
133
+ function generateIndexes(text, size) {
134
+ const indexes = [];
135
+ for (let i = size; i < text.length; i += size) {
136
+ indexes.push(i);
137
+ }
138
+ return indexes;
139
+ }
140
+
141
+ // src/core/divider-loop.ts
142
+ function dividerLoop(input, size, options) {
143
+ if (!isPositiveInteger(size)) {
144
+ console.warn("dividerLoop: chunk size must be a positive number");
145
+ return [];
146
+ }
147
+ if (isString(input)) {
148
+ const indexes = generateIndexes(input, size);
149
+ return divider(input, ...indexes);
150
+ }
151
+ const result = input.map((item) => {
152
+ const indexes = generateIndexes(item, size);
153
+ return divider(item, ...indexes);
154
+ });
155
+ const flatten = options?.flatten ?? false;
156
+ return flatten ? result.flat() : result;
157
+ }
158
+ export {
159
+ divider,
160
+ dividerFirst,
161
+ dividerLast,
162
+ dividerLoop
163
+ };
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@nyaomaru/divider",
3
3
  "type": "module",
4
- "version": "1.5.0",
4
+ "version": "1.5.2",
5
5
  "description": "To divide string or string[] with a given separator",
6
- "main": "dist/cjs/index.js",
7
- "module": "dist/esm/index.js",
8
- "types": "dist/types/index.d.ts",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
9
8
  "exports": {
10
- "import": "./dist/esm/index.js",
11
- "require": "./dist/cjs/index.js",
12
- "types": "./dist/types/index.d.ts"
9
+ "import": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.cts",
15
+ "default": "./dist/index.cjs"
16
+ }
13
17
  },
14
18
  "keywords": [
15
19
  "divider",
@@ -41,6 +45,8 @@
41
45
  "prettier": "^3.5.1",
42
46
  "ts-jest": "^29.2.5",
43
47
  "ts-node": "^10.9.2",
48
+ "tsup": "^8.4.0",
49
+ "tsx": "^4.19.3",
44
50
  "typescript": "^5.7.3"
45
51
  },
46
52
  "engines": {
@@ -53,6 +59,6 @@
53
59
  "scripts": {
54
60
  "test": "jest",
55
61
  "lint": "eslint .",
56
- "build": "tsc --project tsconfig.json && tsc --project tsconfig.cjs.json"
62
+ "build": "tsup"
57
63
  }
58
64
  }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dividerFirst = dividerFirst;
4
- const divider_1 = require("@/core/divider");
5
- function dividerFirst(input, ...args) {
6
- var _a;
7
- const result = (0, divider_1.divider)(input, ...args, { flatten: true });
8
- return (_a = result[0]) !== null && _a !== void 0 ? _a : '';
9
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dividerLast = dividerLast;
4
- const divider_1 = require("@/core/divider");
5
- function dividerLast(input, ...args) {
6
- var _a;
7
- const result = (0, divider_1.divider)(input, ...args, { flatten: true });
8
- return (_a = result.at(-1)) !== null && _a !== void 0 ? _a : '';
9
- }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dividerLoop = dividerLoop;
4
- const is_1 = require("@/utils/is");
5
- const chunk_1 = require("@/utils/chunk");
6
- const divider_1 = require("@/core/divider");
7
- function dividerLoop(input, size, options) {
8
- var _a;
9
- if (!(0, is_1.isPositiveInteger)(size)) {
10
- console.warn('dividerLoop: chunk size must be a positive number');
11
- return [];
12
- }
13
- if ((0, is_1.isString)(input)) {
14
- const indexes = (0, chunk_1.generateIndexes)(input, size);
15
- return (0, divider_1.divider)(input, ...indexes);
16
- }
17
- const result = input.map((item) => {
18
- const indexes = (0, chunk_1.generateIndexes)(item, size);
19
- return (0, divider_1.divider)(item, ...indexes);
20
- });
21
- const flatten = (_a = options === null || options === void 0 ? void 0 : options.flatten) !== null && _a !== void 0 ? _a : false;
22
- return (flatten ? result.flat() : result);
23
- }
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dividerNumberString = dividerNumberString;
4
- const is_1 = require("@/utils/is");
5
- function dividerNumberString(input, options) {
6
- const regex = /\d+|\D+/g;
7
- const divide = (str) => {
8
- return (str.match(regex) || []).filter(Boolean);
9
- };
10
- if ((0, is_1.isString)(input)) {
11
- return divide(input);
12
- }
13
- const result = input.map(divide);
14
- return ((options === null || options === void 0 ? void 0 : options.flatten) ? result.flat() : result);
15
- }
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.divider = divider;
4
- const parser_1 = require("@/core/parser");
5
- const is_1 = require("@/utils/is");
6
- const array_1 = require("@/utils/array");
7
- function divider(input, ...args) {
8
- if (!(0, is_1.isString)(input) && !Array.isArray(input)) {
9
- console.warn("divider: 'input' must be a string or an array of strings. So returning an empty array.");
10
- return [];
11
- }
12
- if ((0, is_1.isEmptyArray)(args)) {
13
- return (0, array_1.ensureArray)(input);
14
- }
15
- // Extract the options from the input
16
- const clonedArgs = [...args];
17
- const lastArg = clonedArgs.at(-1);
18
- const options = (0, is_1.isOptions)(lastArg) ? (clonedArgs.pop(), lastArg) : {};
19
- // Filter out only numbers and strings
20
- const { numSeparators, strSeparators } = clonedArgs.reduce((acc, arg) => {
21
- if ((0, is_1.isNumber)(arg)) {
22
- acc.numSeparators.push(arg);
23
- }
24
- else if ((0, is_1.isString)(arg)) {
25
- acc.strSeparators.push(arg);
26
- }
27
- return acc;
28
- }, { numSeparators: [], strSeparators: [] });
29
- if ((0, is_1.isString)(input)) {
30
- return (0, parser_1.divideString)(input, numSeparators, strSeparators);
31
- }
32
- const result = input.map((item) => (0, parser_1.divideString)(item, numSeparators, strSeparators));
33
- return (options.flatten ? result.flat() : result);
34
- }
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.divideString = divideString;
4
- const is_1 = require("@/utils/is");
5
- const regex_1 = require("@/utils/regex");
6
- const slice_1 = require("@/utils/slice");
7
- const sort_1 = require("@/utils/sort");
8
- function divideString(input, numSeparators, strSeparators) {
9
- if ((0, is_1.isEmptyArray)(numSeparators) && (0, is_1.isEmptyArray)(strSeparators)) {
10
- return [input];
11
- }
12
- // Precompile regex for string separators
13
- const regex = (0, regex_1.getRegex)(strSeparators);
14
- // Divide by number delimiters
15
- const sortedNumSeparators = (0, sort_1.sortAscending)(numSeparators);
16
- const parts = (0, slice_1.sliceByIndexes)(input, sortedNumSeparators);
17
- // Divide by string delimiters
18
- return regex
19
- ? parts.flatMap((part) => part.split(regex)).filter(Boolean)
20
- : parts;
21
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/cjs/index.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dividerLoop = exports.dividerLast = exports.dividerFirst = exports.divider = void 0;
4
- var divider_1 = require("@/core/divider");
5
- Object.defineProperty(exports, "divider", { enumerable: true, get: function () { return divider_1.divider; } });
6
- var divider_first_1 = require("@/core/divider-first");
7
- Object.defineProperty(exports, "dividerFirst", { enumerable: true, get: function () { return divider_first_1.dividerFirst; } });
8
- var divider_last_1 = require("@/core/divider-last");
9
- Object.defineProperty(exports, "dividerLast", { enumerable: true, get: function () { return divider_last_1.dividerLast; } });
10
- var divider_loop_1 = require("@/core/divider-loop");
11
- Object.defineProperty(exports, "dividerLoop", { enumerable: true, get: function () { return divider_loop_1.dividerLoop; } });
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ensureArray = ensureArray;
4
- function ensureArray(input) {
5
- return Array.isArray(input) ? input : [input];
6
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateIndexes = generateIndexes;
4
- function generateIndexes(text, size) {
5
- const indexes = [];
6
- for (let i = size; i < text.length; i += size) {
7
- indexes.push(i);
8
- }
9
- return indexes;
10
- }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNumber = exports.isString = void 0;
4
- exports.isOptions = isOptions;
5
- exports.isEmptyArray = isEmptyArray;
6
- exports.isPositiveInteger = isPositiveInteger;
7
- const isString = (arg) => typeof arg === 'string';
8
- exports.isString = isString;
9
- const isNumber = (arg) => typeof arg === 'number';
10
- exports.isNumber = isNumber;
11
- function isOptions(arg) {
12
- return typeof arg === 'object' && arg !== null && 'flatten' in arg;
13
- }
14
- function isEmptyArray(input) {
15
- return Array.isArray(input) && input.length === 0;
16
- }
17
- function isPositiveInteger(value) {
18
- return Number.isInteger(value) && value > 0;
19
- }
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getRegex = getRegex;
4
- const is_1 = require("@/utils/is");
5
- const regexCache = new Map();
6
- function getRegex(separators) {
7
- if ((0, is_1.isEmptyArray)(separators))
8
- return null;
9
- const key = JSON.stringify(separators);
10
- if (!regexCache.has(key)) {
11
- const pattern = separators.reduce((acc, sep) => acc + escapeRegExp(sep), '');
12
- regexCache.set(key, new RegExp(`[${pattern}]`, 'g'));
13
- }
14
- return regexCache.get(key);
15
- }
16
- function escapeRegExp(str) {
17
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
18
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sliceByIndexes = sliceByIndexes;
4
- const is_1 = require("@/utils/is");
5
- function sliceByIndexes(input, indexes) {
6
- if ((0, is_1.isEmptyArray)(indexes))
7
- return [input];
8
- const parts = [];
9
- let start = 0;
10
- for (const index of indexes) {
11
- if (index <= start || input.length <= index)
12
- continue;
13
- parts.push(input.slice(start, index));
14
- start = index;
15
- }
16
- if (start < input.length) {
17
- parts.push(input.slice(start));
18
- }
19
- return parts;
20
- }
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sortAscending = sortAscending;
4
- function sortAscending(numbers) {
5
- return [...numbers].sort((a, b) => a - b);
6
- }
@@ -1,6 +0,0 @@
1
- import { divider } from '@/core/divider';
2
- export function dividerFirst(input, ...args) {
3
- var _a;
4
- const result = divider(input, ...args, { flatten: true });
5
- return (_a = result[0]) !== null && _a !== void 0 ? _a : '';
6
- }
@@ -1,6 +0,0 @@
1
- import { divider } from '@/core/divider';
2
- export function dividerLast(input, ...args) {
3
- var _a;
4
- const result = divider(input, ...args, { flatten: true });
5
- return (_a = result.at(-1)) !== null && _a !== void 0 ? _a : '';
6
- }
@@ -1,20 +0,0 @@
1
- import { isString, isPositiveInteger } from '@/utils/is';
2
- import { generateIndexes } from '@/utils/chunk';
3
- import { divider } from '@/core/divider';
4
- export function dividerLoop(input, size, options) {
5
- var _a;
6
- if (!isPositiveInteger(size)) {
7
- console.warn('dividerLoop: chunk size must be a positive number');
8
- return [];
9
- }
10
- if (isString(input)) {
11
- const indexes = generateIndexes(input, size);
12
- return divider(input, ...indexes);
13
- }
14
- const result = input.map((item) => {
15
- const indexes = generateIndexes(item, size);
16
- return divider(item, ...indexes);
17
- });
18
- const flatten = (_a = options === null || options === void 0 ? void 0 : options.flatten) !== null && _a !== void 0 ? _a : false;
19
- return (flatten ? result.flat() : result);
20
- }
@@ -1,12 +0,0 @@
1
- import { isString } from '@/utils/is';
2
- export function dividerNumberString(input, options) {
3
- const regex = /\d+|\D+/g;
4
- const divide = (str) => {
5
- return (str.match(regex) || []).filter(Boolean);
6
- };
7
- if (isString(input)) {
8
- return divide(input);
9
- }
10
- const result = input.map(divide);
11
- return ((options === null || options === void 0 ? void 0 : options.flatten) ? result.flat() : result);
12
- }
@@ -1,31 +0,0 @@
1
- import { divideString } from '@/core/parser';
2
- import { isString, isNumber, isOptions, isEmptyArray } from '@/utils/is';
3
- import { ensureArray } from '@/utils/array';
4
- export function divider(input, ...args) {
5
- if (!isString(input) && !Array.isArray(input)) {
6
- console.warn("divider: 'input' must be a string or an array of strings. So returning an empty array.");
7
- return [];
8
- }
9
- if (isEmptyArray(args)) {
10
- return ensureArray(input);
11
- }
12
- // Extract the options from the input
13
- const clonedArgs = [...args];
14
- const lastArg = clonedArgs.at(-1);
15
- const options = isOptions(lastArg) ? (clonedArgs.pop(), lastArg) : {};
16
- // Filter out only numbers and strings
17
- const { numSeparators, strSeparators } = clonedArgs.reduce((acc, arg) => {
18
- if (isNumber(arg)) {
19
- acc.numSeparators.push(arg);
20
- }
21
- else if (isString(arg)) {
22
- acc.strSeparators.push(arg);
23
- }
24
- return acc;
25
- }, { numSeparators: [], strSeparators: [] });
26
- if (isString(input)) {
27
- return divideString(input, numSeparators, strSeparators);
28
- }
29
- const result = input.map((item) => divideString(item, numSeparators, strSeparators));
30
- return (options.flatten ? result.flat() : result);
31
- }
@@ -1,18 +0,0 @@
1
- import { isEmptyArray } from '@/utils/is';
2
- import { getRegex } from '@/utils/regex';
3
- import { sliceByIndexes } from '@/utils/slice';
4
- import { sortAscending } from '@/utils/sort';
5
- export function divideString(input, numSeparators, strSeparators) {
6
- if (isEmptyArray(numSeparators) && isEmptyArray(strSeparators)) {
7
- return [input];
8
- }
9
- // Precompile regex for string separators
10
- const regex = getRegex(strSeparators);
11
- // Divide by number delimiters
12
- const sortedNumSeparators = sortAscending(numSeparators);
13
- const parts = sliceByIndexes(input, sortedNumSeparators);
14
- // Divide by string delimiters
15
- return regex
16
- ? parts.flatMap((part) => part.split(regex)).filter(Boolean)
17
- : parts;
18
- }
@@ -1 +0,0 @@
1
- export {};
package/dist/esm/index.js DELETED
@@ -1,4 +0,0 @@
1
- export { divider } from '@/core/divider';
2
- export { dividerFirst } from '@/core/divider-first';
3
- export { dividerLast } from '@/core/divider-last';
4
- export { dividerLoop } from '@/core/divider-loop';
@@ -1,3 +0,0 @@
1
- export function ensureArray(input) {
2
- return Array.isArray(input) ? input : [input];
3
- }
@@ -1,7 +0,0 @@
1
- export function generateIndexes(text, size) {
2
- const indexes = [];
3
- for (let i = size; i < text.length; i += size) {
4
- indexes.push(i);
5
- }
6
- return indexes;
7
- }
@@ -1,11 +0,0 @@
1
- export const isString = (arg) => typeof arg === 'string';
2
- export const isNumber = (arg) => typeof arg === 'number';
3
- export function isOptions(arg) {
4
- return typeof arg === 'object' && arg !== null && 'flatten' in arg;
5
- }
6
- export function isEmptyArray(input) {
7
- return Array.isArray(input) && input.length === 0;
8
- }
9
- export function isPositiveInteger(value) {
10
- return Number.isInteger(value) && value > 0;
11
- }
@@ -1,15 +0,0 @@
1
- import { isEmptyArray } from '@/utils/is';
2
- const regexCache = new Map();
3
- export function getRegex(separators) {
4
- if (isEmptyArray(separators))
5
- return null;
6
- const key = JSON.stringify(separators);
7
- if (!regexCache.has(key)) {
8
- const pattern = separators.reduce((acc, sep) => acc + escapeRegExp(sep), '');
9
- regexCache.set(key, new RegExp(`[${pattern}]`, 'g'));
10
- }
11
- return regexCache.get(key);
12
- }
13
- function escapeRegExp(str) {
14
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
15
- }
@@ -1,17 +0,0 @@
1
- import { isEmptyArray } from '@/utils/is';
2
- export function sliceByIndexes(input, indexes) {
3
- if (isEmptyArray(indexes))
4
- return [input];
5
- const parts = [];
6
- let start = 0;
7
- for (const index of indexes) {
8
- if (index <= start || input.length <= index)
9
- continue;
10
- parts.push(input.slice(start, index));
11
- start = index;
12
- }
13
- if (start < input.length) {
14
- parts.push(input.slice(start));
15
- }
16
- return parts;
17
- }
@@ -1,3 +0,0 @@
1
- export function sortAscending(numbers) {
2
- return [...numbers].sort((a, b) => a - b);
3
- }
@@ -1,2 +0,0 @@
1
- import type { DividerSeparators } from '@/core/types';
2
- export declare function dividerFirst(input: string | string[], ...args: DividerSeparators): string;
@@ -1,2 +0,0 @@
1
- import type { DividerSeparators } from '@/core/types';
2
- export declare function dividerLast(input: string | string[], ...args: DividerSeparators): string;
@@ -1,2 +0,0 @@
1
- import type { DividerOptions, DividerResult } from '@/core/types';
2
- export declare function dividerLoop<T extends string | string[], F extends boolean>(input: T, size: number, options?: DividerOptions<F>): DividerResult<T, F>;
@@ -1,2 +0,0 @@
1
- import type { DividerOptions, DividerResult } from '@/core/types';
2
- export declare function dividerNumberString<T extends string | string[], F extends boolean = false>(input: T, options?: DividerOptions<F>): DividerResult<T, F>;
@@ -1,2 +0,0 @@
1
- import type { DividerResult, DividerArgs } from '@/core/types';
2
- export declare function divider<T extends string | string[], F extends boolean>(input: T, ...args: DividerArgs<F>): DividerResult<T, F>;
@@ -1 +0,0 @@
1
- export declare function divideString(input: string, numSeparators: number[], strSeparators: string[]): string[];
@@ -1,6 +0,0 @@
1
- export type DividerResult<T extends string | string[], F extends boolean = false> = T extends string ? string[] : F extends true ? string[] : string[][];
2
- export type DividerOptions<F extends boolean> = {
3
- flatten?: F;
4
- };
5
- export type DividerSeparators = (number | string)[];
6
- export type DividerArgs<F extends boolean> = DividerSeparators | [...DividerSeparators, DividerOptions<F>];
@@ -1,5 +0,0 @@
1
- export { divider } from '@/core/divider';
2
- export { dividerFirst } from '@/core/divider-first';
3
- export { dividerLast } from '@/core/divider-last';
4
- export { dividerLoop } from '@/core/divider-loop';
5
- export type { DividerResult } from '@/core/types';
@@ -1 +0,0 @@
1
- export declare function ensureArray<T>(input: T | T[]): T[];
@@ -1 +0,0 @@
1
- export declare function generateIndexes(text: string, size: number): number[];
@@ -1,7 +0,0 @@
1
- export declare const isString: (arg: unknown) => arg is string;
2
- export declare const isNumber: (arg: unknown) => arg is number;
3
- export declare function isOptions(arg: unknown): arg is {
4
- flatten?: boolean;
5
- };
6
- export declare function isEmptyArray<T>(input: T[]): boolean;
7
- export declare function isPositiveInteger(value: unknown): boolean;
@@ -1 +0,0 @@
1
- export declare function getRegex(separators: string[]): RegExp | null;
@@ -1 +0,0 @@
1
- export declare function sliceByIndexes(input: string, indexes: number[]): string[];
@@ -1 +0,0 @@
1
- export declare function sortAscending(numbers: number[]): number[];