@codady/utils 0.0.4 → 0.0.5
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/utils.cjs.js +75 -3
- package/dist/utils.esm.js +75 -3
- package/dist/utils.umd.js +75 -3
- package/package.json +1 -1
- package/src/mutateArray.js +3 -5
- package/src/mutateMethods.js +5 -0
package/dist/utils.cjs.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* @since Last modified: 2025-12-18 21:
|
|
3
|
+
* @since Last modified: 2025-12-18 21:39:9
|
|
4
4
|
* @name Utils for web front-end.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.4
|
|
6
6
|
* @author AXUI development team <3217728223@qq.com>
|
|
7
7
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
8
8
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -102,6 +102,76 @@ const deepCloneToJSON = (data) => {
|
|
|
102
102
|
}
|
|
103
103
|
};
|
|
104
104
|
|
|
105
|
+
const mutateMethods = [
|
|
106
|
+
'push', 'pop', 'shift', 'unshift', 'splice',
|
|
107
|
+
'sort', 'reverse', 'copyWithin', 'fill'
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
const mutateArray = ({ target, method, args = [], onBeforeMutate = () => { }, onAfterMutate = () => { }, allowList }) => {
|
|
111
|
+
// Validation: Ensure target is an array and method is allowed
|
|
112
|
+
if (!Array.isArray(target)) {
|
|
113
|
+
throw new TypeError("The 'target' parameter must be an array.");
|
|
114
|
+
}
|
|
115
|
+
//使用默认
|
|
116
|
+
if (!allowList || allowList?.length) {
|
|
117
|
+
allowList = mutateMethods;
|
|
118
|
+
}
|
|
119
|
+
if (!allowList?.includes(method)) {
|
|
120
|
+
throw new Error(`Method "${method}" is not in the allowList or is not a mutation method.`);
|
|
121
|
+
}
|
|
122
|
+
const context = {}, len = target.length;
|
|
123
|
+
// Capture "Pre-mutation" context for Undo/Redo/Tracking purposes
|
|
124
|
+
switch (method) {
|
|
125
|
+
// 'push' and 'unshift' are add operations, in undo/redo,
|
|
126
|
+
// we can determine the original data structure from result.length and args.length
|
|
127
|
+
// but methods that involve deletion need to record the deleted items to ensure the patch can restore the data structure during undo
|
|
128
|
+
case 'push':
|
|
129
|
+
case 'unshift':
|
|
130
|
+
context.addedItems = [...args];
|
|
131
|
+
break;
|
|
132
|
+
case 'pop':
|
|
133
|
+
context.poppedValue = target[len - 1];
|
|
134
|
+
break;
|
|
135
|
+
case 'shift':
|
|
136
|
+
context.shiftedValue = target[0];
|
|
137
|
+
break;
|
|
138
|
+
case 'splice':
|
|
139
|
+
const [s, d] = args,
|
|
140
|
+
// Calculate actual start index (handling negative values)
|
|
141
|
+
start = s < 0 ? Math.max(len + s, 0) : Math.min(s, len),
|
|
142
|
+
// Handle deleteCount defaults
|
|
143
|
+
deleteCount = d === undefined ? len - start : d;
|
|
144
|
+
context.deletedItems = target.slice(start, start + deleteCount);
|
|
145
|
+
break;
|
|
146
|
+
case 'sort':
|
|
147
|
+
case 'reverse':
|
|
148
|
+
// These methods reorder the whole array; requires a full shallow copy
|
|
149
|
+
context.oldSnapshot = [...target];
|
|
150
|
+
break;
|
|
151
|
+
case 'fill':
|
|
152
|
+
case 'copyWithin':
|
|
153
|
+
const startIdx = args[1] || 0, endIdx = args[2] === undefined ? len : args[2];
|
|
154
|
+
// Overwritten values
|
|
155
|
+
context.oldItems = target.slice(startIdx, endIdx);
|
|
156
|
+
context.start = startIdx;
|
|
157
|
+
context.end = endIdx;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
// Execute the "onBeforeMutate" callback before mutation
|
|
161
|
+
onBeforeMutate && onBeforeMutate(context);
|
|
162
|
+
// Execute the native array mutation
|
|
163
|
+
const result = Array.prototype[method].apply(target, args);
|
|
164
|
+
// Construct and trigger the patch callback
|
|
165
|
+
onAfterMutate && onAfterMutate({
|
|
166
|
+
method,
|
|
167
|
+
args,
|
|
168
|
+
context,
|
|
169
|
+
result,
|
|
170
|
+
timestamp: Date.now()
|
|
171
|
+
});
|
|
172
|
+
return result;
|
|
173
|
+
};
|
|
174
|
+
|
|
105
175
|
const requireTypes = (data, require, cb) => {
|
|
106
176
|
// Normalize the input types (convert to array if it's a single type)
|
|
107
177
|
let requiredTypes = Array.isArray(require) ? require : [require], dataType = getDataType(data), typeLower = dataType.toLowerCase(),
|
|
@@ -136,7 +206,9 @@ const utils = {
|
|
|
136
206
|
//renderTpl,
|
|
137
207
|
//parseStr,
|
|
138
208
|
deepClone,
|
|
139
|
-
deepCloneToJSON
|
|
209
|
+
deepCloneToJSON,
|
|
210
|
+
mutateArray,
|
|
211
|
+
mutateMethods
|
|
140
212
|
};
|
|
141
213
|
|
|
142
214
|
module.exports = utils;
|
package/dist/utils.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* @since Last modified: 2025-12-18 21:
|
|
3
|
+
* @since Last modified: 2025-12-18 21:39:9
|
|
4
4
|
* @name Utils for web front-end.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.4
|
|
6
6
|
* @author AXUI development team <3217728223@qq.com>
|
|
7
7
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
8
8
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -100,6 +100,76 @@ const deepCloneToJSON = (data) => {
|
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
102
|
|
|
103
|
+
const mutateMethods = [
|
|
104
|
+
'push', 'pop', 'shift', 'unshift', 'splice',
|
|
105
|
+
'sort', 'reverse', 'copyWithin', 'fill'
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
const mutateArray = ({ target, method, args = [], onBeforeMutate = () => { }, onAfterMutate = () => { }, allowList }) => {
|
|
109
|
+
// Validation: Ensure target is an array and method is allowed
|
|
110
|
+
if (!Array.isArray(target)) {
|
|
111
|
+
throw new TypeError("The 'target' parameter must be an array.");
|
|
112
|
+
}
|
|
113
|
+
//使用默认
|
|
114
|
+
if (!allowList || allowList?.length) {
|
|
115
|
+
allowList = mutateMethods;
|
|
116
|
+
}
|
|
117
|
+
if (!allowList?.includes(method)) {
|
|
118
|
+
throw new Error(`Method "${method}" is not in the allowList or is not a mutation method.`);
|
|
119
|
+
}
|
|
120
|
+
const context = {}, len = target.length;
|
|
121
|
+
// Capture "Pre-mutation" context for Undo/Redo/Tracking purposes
|
|
122
|
+
switch (method) {
|
|
123
|
+
// 'push' and 'unshift' are add operations, in undo/redo,
|
|
124
|
+
// we can determine the original data structure from result.length and args.length
|
|
125
|
+
// but methods that involve deletion need to record the deleted items to ensure the patch can restore the data structure during undo
|
|
126
|
+
case 'push':
|
|
127
|
+
case 'unshift':
|
|
128
|
+
context.addedItems = [...args];
|
|
129
|
+
break;
|
|
130
|
+
case 'pop':
|
|
131
|
+
context.poppedValue = target[len - 1];
|
|
132
|
+
break;
|
|
133
|
+
case 'shift':
|
|
134
|
+
context.shiftedValue = target[0];
|
|
135
|
+
break;
|
|
136
|
+
case 'splice':
|
|
137
|
+
const [s, d] = args,
|
|
138
|
+
// Calculate actual start index (handling negative values)
|
|
139
|
+
start = s < 0 ? Math.max(len + s, 0) : Math.min(s, len),
|
|
140
|
+
// Handle deleteCount defaults
|
|
141
|
+
deleteCount = d === undefined ? len - start : d;
|
|
142
|
+
context.deletedItems = target.slice(start, start + deleteCount);
|
|
143
|
+
break;
|
|
144
|
+
case 'sort':
|
|
145
|
+
case 'reverse':
|
|
146
|
+
// These methods reorder the whole array; requires a full shallow copy
|
|
147
|
+
context.oldSnapshot = [...target];
|
|
148
|
+
break;
|
|
149
|
+
case 'fill':
|
|
150
|
+
case 'copyWithin':
|
|
151
|
+
const startIdx = args[1] || 0, endIdx = args[2] === undefined ? len : args[2];
|
|
152
|
+
// Overwritten values
|
|
153
|
+
context.oldItems = target.slice(startIdx, endIdx);
|
|
154
|
+
context.start = startIdx;
|
|
155
|
+
context.end = endIdx;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
// Execute the "onBeforeMutate" callback before mutation
|
|
159
|
+
onBeforeMutate && onBeforeMutate(context);
|
|
160
|
+
// Execute the native array mutation
|
|
161
|
+
const result = Array.prototype[method].apply(target, args);
|
|
162
|
+
// Construct and trigger the patch callback
|
|
163
|
+
onAfterMutate && onAfterMutate({
|
|
164
|
+
method,
|
|
165
|
+
args,
|
|
166
|
+
context,
|
|
167
|
+
result,
|
|
168
|
+
timestamp: Date.now()
|
|
169
|
+
});
|
|
170
|
+
return result;
|
|
171
|
+
};
|
|
172
|
+
|
|
103
173
|
const requireTypes = (data, require, cb) => {
|
|
104
174
|
// Normalize the input types (convert to array if it's a single type)
|
|
105
175
|
let requiredTypes = Array.isArray(require) ? require : [require], dataType = getDataType(data), typeLower = dataType.toLowerCase(),
|
|
@@ -134,7 +204,9 @@ const utils = {
|
|
|
134
204
|
//renderTpl,
|
|
135
205
|
//parseStr,
|
|
136
206
|
deepClone,
|
|
137
|
-
deepCloneToJSON
|
|
207
|
+
deepCloneToJSON,
|
|
208
|
+
mutateArray,
|
|
209
|
+
mutateMethods
|
|
138
210
|
};
|
|
139
211
|
|
|
140
212
|
export { utils as default };
|
package/dist/utils.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* @since Last modified: 2025-12-18 21:
|
|
3
|
+
* @since Last modified: 2025-12-18 21:39:9
|
|
4
4
|
* @name Utils for web front-end.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.4
|
|
6
6
|
* @author AXUI development team <3217728223@qq.com>
|
|
7
7
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
8
8
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -106,6 +106,76 @@
|
|
|
106
106
|
}
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
+
const mutateMethods = [
|
|
110
|
+
'push', 'pop', 'shift', 'unshift', 'splice',
|
|
111
|
+
'sort', 'reverse', 'copyWithin', 'fill'
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
const mutateArray = ({ target, method, args = [], onBeforeMutate = () => { }, onAfterMutate = () => { }, allowList }) => {
|
|
115
|
+
// Validation: Ensure target is an array and method is allowed
|
|
116
|
+
if (!Array.isArray(target)) {
|
|
117
|
+
throw new TypeError("The 'target' parameter must be an array.");
|
|
118
|
+
}
|
|
119
|
+
//使用默认
|
|
120
|
+
if (!allowList || allowList?.length) {
|
|
121
|
+
allowList = mutateMethods;
|
|
122
|
+
}
|
|
123
|
+
if (!allowList?.includes(method)) {
|
|
124
|
+
throw new Error(`Method "${method}" is not in the allowList or is not a mutation method.`);
|
|
125
|
+
}
|
|
126
|
+
const context = {}, len = target.length;
|
|
127
|
+
// Capture "Pre-mutation" context for Undo/Redo/Tracking purposes
|
|
128
|
+
switch (method) {
|
|
129
|
+
// 'push' and 'unshift' are add operations, in undo/redo,
|
|
130
|
+
// we can determine the original data structure from result.length and args.length
|
|
131
|
+
// but methods that involve deletion need to record the deleted items to ensure the patch can restore the data structure during undo
|
|
132
|
+
case 'push':
|
|
133
|
+
case 'unshift':
|
|
134
|
+
context.addedItems = [...args];
|
|
135
|
+
break;
|
|
136
|
+
case 'pop':
|
|
137
|
+
context.poppedValue = target[len - 1];
|
|
138
|
+
break;
|
|
139
|
+
case 'shift':
|
|
140
|
+
context.shiftedValue = target[0];
|
|
141
|
+
break;
|
|
142
|
+
case 'splice':
|
|
143
|
+
const [s, d] = args,
|
|
144
|
+
// Calculate actual start index (handling negative values)
|
|
145
|
+
start = s < 0 ? Math.max(len + s, 0) : Math.min(s, len),
|
|
146
|
+
// Handle deleteCount defaults
|
|
147
|
+
deleteCount = d === undefined ? len - start : d;
|
|
148
|
+
context.deletedItems = target.slice(start, start + deleteCount);
|
|
149
|
+
break;
|
|
150
|
+
case 'sort':
|
|
151
|
+
case 'reverse':
|
|
152
|
+
// These methods reorder the whole array; requires a full shallow copy
|
|
153
|
+
context.oldSnapshot = [...target];
|
|
154
|
+
break;
|
|
155
|
+
case 'fill':
|
|
156
|
+
case 'copyWithin':
|
|
157
|
+
const startIdx = args[1] || 0, endIdx = args[2] === undefined ? len : args[2];
|
|
158
|
+
// Overwritten values
|
|
159
|
+
context.oldItems = target.slice(startIdx, endIdx);
|
|
160
|
+
context.start = startIdx;
|
|
161
|
+
context.end = endIdx;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
// Execute the "onBeforeMutate" callback before mutation
|
|
165
|
+
onBeforeMutate && onBeforeMutate(context);
|
|
166
|
+
// Execute the native array mutation
|
|
167
|
+
const result = Array.prototype[method].apply(target, args);
|
|
168
|
+
// Construct and trigger the patch callback
|
|
169
|
+
onAfterMutate && onAfterMutate({
|
|
170
|
+
method,
|
|
171
|
+
args,
|
|
172
|
+
context,
|
|
173
|
+
result,
|
|
174
|
+
timestamp: Date.now()
|
|
175
|
+
});
|
|
176
|
+
return result;
|
|
177
|
+
};
|
|
178
|
+
|
|
109
179
|
const requireTypes = (data, require, cb) => {
|
|
110
180
|
// Normalize the input types (convert to array if it's a single type)
|
|
111
181
|
let requiredTypes = Array.isArray(require) ? require : [require], dataType = getDataType(data), typeLower = dataType.toLowerCase(),
|
|
@@ -140,7 +210,9 @@
|
|
|
140
210
|
//renderTpl,
|
|
141
211
|
//parseStr,
|
|
142
212
|
deepClone,
|
|
143
|
-
deepCloneToJSON
|
|
213
|
+
deepCloneToJSON,
|
|
214
|
+
mutateArray,
|
|
215
|
+
mutateMethods
|
|
144
216
|
};
|
|
145
217
|
|
|
146
218
|
return utils;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codady/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"author": "AXUI Development Team",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.",
|
package/src/mutateArray.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since Last modified: 2025/12/18 20:
|
|
2
|
+
* @since Last modified: 2025/12/18 21:20:26
|
|
3
3
|
* Mutates an array by applying one of the allowed mutation methods such as push, pop, shift, unshift, splice, etc.
|
|
4
4
|
* Supports tracking of added, removed, and updated items for undo/redo or tracking purposes.
|
|
5
5
|
* The mutation methods that can be tracked include push, pop, shift, unshift, splice, sort, reverse, fill, and copyWithin.
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
* // The patch callback logs the mutation details including deleted items and added items.
|
|
31
31
|
*/
|
|
32
32
|
'use strict';
|
|
33
|
+
import mutateMethods from "./mutateMethods";
|
|
33
34
|
const mutateArray = ({ target, method, args = [], onBeforeMutate = () => { }, onAfterMutate = () => { }, allowList }) => {
|
|
34
35
|
// Validation: Ensure target is an array and method is allowed
|
|
35
36
|
if (!Array.isArray(target)) {
|
|
@@ -37,10 +38,7 @@ const mutateArray = ({ target, method, args = [], onBeforeMutate = () => { }, on
|
|
|
37
38
|
}
|
|
38
39
|
//使用默认
|
|
39
40
|
if (!allowList || allowList?.length) {
|
|
40
|
-
allowList =
|
|
41
|
-
'push', 'pop', 'shift', 'unshift', 'splice',
|
|
42
|
-
'sort', 'reverse', 'copyWithin', 'fill'
|
|
43
|
-
];
|
|
41
|
+
allowList = mutateMethods;
|
|
44
42
|
}
|
|
45
43
|
if (!allowList?.includes(method)) {
|
|
46
44
|
throw new Error(`Method "${method}" is not in the allowList or is not a mutation method.`);
|