@nyaomaru/divider 1.5.1 → 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.
- package/dist/index.cjs +32 -15
- package/dist/index.js +32 -15
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -39,6 +39,9 @@ function isEmptyArray(input) {
|
|
|
39
39
|
function isPositiveInteger(value) {
|
|
40
40
|
return Number.isInteger(value) && value > 0;
|
|
41
41
|
}
|
|
42
|
+
function isValidInput(input) {
|
|
43
|
+
return isString(input) || Array.isArray(input) && input.every(isString);
|
|
44
|
+
}
|
|
42
45
|
|
|
43
46
|
// src/utils/regex.ts
|
|
44
47
|
var regexCache = /* @__PURE__ */ new Map();
|
|
@@ -95,9 +98,35 @@ function ensureArray(input) {
|
|
|
95
98
|
return Array.isArray(input) ? input : [input];
|
|
96
99
|
}
|
|
97
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
|
+
|
|
98
127
|
// src/core/divider.ts
|
|
99
128
|
function divider(input, ...args) {
|
|
100
|
-
if (!
|
|
129
|
+
if (!isValidInput(input)) {
|
|
101
130
|
console.warn(
|
|
102
131
|
"divider: 'input' must be a string or an array of strings. So returning an empty array."
|
|
103
132
|
);
|
|
@@ -106,20 +135,8 @@ function divider(input, ...args) {
|
|
|
106
135
|
if (isEmptyArray(args)) {
|
|
107
136
|
return ensureArray(input);
|
|
108
137
|
}
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
const options = isOptions(lastArg) ? (clonedArgs.pop(), lastArg) : {};
|
|
112
|
-
const { numSeparators, strSeparators } = clonedArgs.reduce(
|
|
113
|
-
(acc, arg) => {
|
|
114
|
-
if (isNumber(arg)) {
|
|
115
|
-
acc.numSeparators.push(arg);
|
|
116
|
-
} else if (isString(arg)) {
|
|
117
|
-
acc.strSeparators.push(arg);
|
|
118
|
-
}
|
|
119
|
-
return acc;
|
|
120
|
-
},
|
|
121
|
-
{ numSeparators: [], strSeparators: [] }
|
|
122
|
-
);
|
|
138
|
+
const { cleanedArgs, options } = extractOptions(args);
|
|
139
|
+
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|
|
123
140
|
if (isString(input)) {
|
|
124
141
|
return divideString(input, numSeparators, strSeparators);
|
|
125
142
|
}
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,9 @@ function isEmptyArray(input) {
|
|
|
10
10
|
function isPositiveInteger(value) {
|
|
11
11
|
return Number.isInteger(value) && value > 0;
|
|
12
12
|
}
|
|
13
|
+
function isValidInput(input) {
|
|
14
|
+
return isString(input) || Array.isArray(input) && input.every(isString);
|
|
15
|
+
}
|
|
13
16
|
|
|
14
17
|
// src/utils/regex.ts
|
|
15
18
|
var regexCache = /* @__PURE__ */ new Map();
|
|
@@ -66,9 +69,35 @@ function ensureArray(input) {
|
|
|
66
69
|
return Array.isArray(input) ? input : [input];
|
|
67
70
|
}
|
|
68
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
|
+
|
|
69
98
|
// src/core/divider.ts
|
|
70
99
|
function divider(input, ...args) {
|
|
71
|
-
if (!
|
|
100
|
+
if (!isValidInput(input)) {
|
|
72
101
|
console.warn(
|
|
73
102
|
"divider: 'input' must be a string or an array of strings. So returning an empty array."
|
|
74
103
|
);
|
|
@@ -77,20 +106,8 @@ function divider(input, ...args) {
|
|
|
77
106
|
if (isEmptyArray(args)) {
|
|
78
107
|
return ensureArray(input);
|
|
79
108
|
}
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const options = isOptions(lastArg) ? (clonedArgs.pop(), lastArg) : {};
|
|
83
|
-
const { numSeparators, strSeparators } = clonedArgs.reduce(
|
|
84
|
-
(acc, arg) => {
|
|
85
|
-
if (isNumber(arg)) {
|
|
86
|
-
acc.numSeparators.push(arg);
|
|
87
|
-
} else if (isString(arg)) {
|
|
88
|
-
acc.strSeparators.push(arg);
|
|
89
|
-
}
|
|
90
|
-
return acc;
|
|
91
|
-
},
|
|
92
|
-
{ numSeparators: [], strSeparators: [] }
|
|
93
|
-
);
|
|
109
|
+
const { cleanedArgs, options } = extractOptions(args);
|
|
110
|
+
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|
|
94
111
|
if (isString(input)) {
|
|
95
112
|
return divideString(input, numSeparators, strSeparators);
|
|
96
113
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nyaomaru/divider",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.5.
|
|
4
|
+
"version": "1.5.2",
|
|
5
5
|
"description": "To divide string or string[] with a given separator",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"ts-jest": "^29.2.5",
|
|
47
47
|
"ts-node": "^10.9.2",
|
|
48
48
|
"tsup": "^8.4.0",
|
|
49
|
+
"tsx": "^4.19.3",
|
|
49
50
|
"typescript": "^5.7.3"
|
|
50
51
|
},
|
|
51
52
|
"engines": {
|