@zairakai/js-utils 1.0.0
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/LICENSE +21 -0
- package/README.md +270 -0
- package/dist/arrays.cjs +210 -0
- package/dist/arrays.d.cts +119 -0
- package/dist/arrays.d.ts +119 -0
- package/dist/arrays.js +32 -0
- package/dist/chunk-27YHP2CK.js +407 -0
- package/dist/chunk-3WNRYKPG.js +37 -0
- package/dist/chunk-42CHLXT7.js +214 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-7SXRFZBB.js +173 -0
- package/dist/chunk-F6RSTW65.js +156 -0
- package/dist/chunk-G7ZJ23DW.js +253 -0
- package/dist/chunk-IPP7PA6H.js +136 -0
- package/dist/chunk-LDSWHSRX.js +96 -0
- package/dist/chunk-TY75OOIQ.js +700 -0
- package/dist/chunk-W6JEMFAF.js +54 -0
- package/dist/chunk-XEJLBAXE.js +164 -0
- package/dist/chunk-Z7G3SIQH.js +270 -0
- package/dist/chunk-ZJPKS2MQ.js +101 -0
- package/dist/collections.cjs +797 -0
- package/dist/collections.d.cts +353 -0
- package/dist/collections.d.ts +353 -0
- package/dist/collections.js +17 -0
- package/dist/datetime.cjs +80 -0
- package/dist/datetime.d.cts +75 -0
- package/dist/datetime.d.ts +75 -0
- package/dist/datetime.js +24 -0
- package/dist/equals.cjs +121 -0
- package/dist/equals.d.cts +24 -0
- package/dist/equals.d.ts +24 -0
- package/dist/equals.js +8 -0
- package/dist/formatters.cjs +201 -0
- package/dist/formatters.d.cts +180 -0
- package/dist/formatters.d.ts +180 -0
- package/dist/formatters.js +48 -0
- package/dist/index.cjs +2906 -0
- package/dist/index.d.cts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +348 -0
- package/dist/number.cjs +279 -0
- package/dist/number.d.cts +177 -0
- package/dist/number.d.ts +177 -0
- package/dist/number.js +10 -0
- package/dist/obj.cjs +427 -0
- package/dist/obj.d.cts +177 -0
- package/dist/obj.d.ts +177 -0
- package/dist/obj.js +12 -0
- package/dist/php-arrays.cjs +954 -0
- package/dist/php-arrays.d.cts +256 -0
- package/dist/php-arrays.d.ts +256 -0
- package/dist/php-arrays.js +70 -0
- package/dist/runtime.cjs +134 -0
- package/dist/runtime.d.cts +90 -0
- package/dist/runtime.d.ts +90 -0
- package/dist/runtime.js +24 -0
- package/dist/schemas.cjs +86 -0
- package/dist/schemas.d.cts +108 -0
- package/dist/schemas.d.ts +108 -0
- package/dist/schemas.js +22 -0
- package/dist/str.cjs +499 -0
- package/dist/str.d.cts +282 -0
- package/dist/str.d.ts +282 -0
- package/dist/str.js +11 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/validator.cjs +251 -0
- package/dist/validator.d.cts +99 -0
- package/dist/validator.d.ts +99 -0
- package/dist/validator.js +11 -0
- package/dist/validators.cjs +217 -0
- package/dist/validators.d.cts +216 -0
- package/dist/validators.d.ts +216 -0
- package/dist/validators.js +64 -0
- package/package.json +180 -0
- package/src/arrays.ts +316 -0
- package/src/collections.ts +866 -0
- package/src/datetime.ts +103 -0
- package/src/equals.ts +134 -0
- package/src/formatters.ts +342 -0
- package/src/index.ts +36 -0
- package/src/number.ts +281 -0
- package/src/obj.ts +303 -0
- package/src/php-arrays.ts +445 -0
- package/src/pipe.ts +29 -0
- package/src/runtime.ts +194 -0
- package/src/schemas.ts +136 -0
- package/src/str.ts +438 -0
- package/src/types.ts +13 -0
- package/src/validator.ts +157 -0
- package/src/validators.ts +359 -0
package/dist/obj.cjs
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
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/obj.ts
|
|
21
|
+
var obj_exports = {};
|
|
22
|
+
__export(obj_exports, {
|
|
23
|
+
Obj: () => Obj,
|
|
24
|
+
Objectable: () => Objectable,
|
|
25
|
+
obj: () => obj
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(obj_exports);
|
|
28
|
+
|
|
29
|
+
// src/equals.ts
|
|
30
|
+
var isEqual = (a, b) => {
|
|
31
|
+
if (a === b) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (a && b && "object" === typeof a && "object" === typeof b) {
|
|
35
|
+
if (a.constructor !== b.constructor) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(a)) {
|
|
39
|
+
if (a.length !== b.length) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
for (let i = 0; i < a.length; i++) {
|
|
43
|
+
if (!isEqual(a[i], b[i])) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
if (a instanceof Map && b instanceof Map) {
|
|
50
|
+
if (a.size !== b.size) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
for (const [key, val] of a) {
|
|
54
|
+
if (!b.has(key) || !isEqual(val, b.get(key))) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (a instanceof Set && b instanceof Set) {
|
|
61
|
+
if (a.size !== b.size) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
for (const val of a) {
|
|
65
|
+
if (!b.has(val)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
if (a.constructor === RegExp) {
|
|
72
|
+
return a.source === b.source && a.flags === b.flags;
|
|
73
|
+
}
|
|
74
|
+
if (a.valueOf !== Object.prototype.valueOf) {
|
|
75
|
+
return a.valueOf() === b.valueOf();
|
|
76
|
+
}
|
|
77
|
+
if (a.toString !== Object.prototype.toString) {
|
|
78
|
+
return a.toString() === b.toString();
|
|
79
|
+
}
|
|
80
|
+
const keys = Object.keys(a);
|
|
81
|
+
if (keys.length !== Object.keys(b).length) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const bObj = b;
|
|
85
|
+
const aObj = a;
|
|
86
|
+
for (const key of keys) {
|
|
87
|
+
if (!Object.prototype.hasOwnProperty.call(bObj, key)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (!isEqual(aObj[key], bObj[key])) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
return a !== a && b !== b;
|
|
97
|
+
};
|
|
98
|
+
var diff = (original, current) => {
|
|
99
|
+
const result = {};
|
|
100
|
+
if (isEqual(original, current)) {
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
if (!original || !current || "object" !== typeof original || "object" !== typeof current) {
|
|
104
|
+
return current || {};
|
|
105
|
+
}
|
|
106
|
+
const originalObj = original;
|
|
107
|
+
const currentObj = current;
|
|
108
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(originalObj), ...Object.keys(currentObj)]);
|
|
109
|
+
for (const key of keys) {
|
|
110
|
+
if (!isEqual(originalObj[key], currentObj[key])) {
|
|
111
|
+
if (originalObj[key] && currentObj[key] && "object" === typeof originalObj[key] && "object" === typeof currentObj[key] && !Array.isArray(originalObj[key])) {
|
|
112
|
+
result[key] = diff(originalObj[key], currentObj[key]);
|
|
113
|
+
} else {
|
|
114
|
+
result[key] = currentObj[key];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// src/runtime.ts
|
|
122
|
+
function isRecord(value) {
|
|
123
|
+
return null !== value && "object" === typeof value && !Array.isArray(value);
|
|
124
|
+
}
|
|
125
|
+
var data_get = (target, key, defaultValue = null) => {
|
|
126
|
+
if (!isRecord(target) && !Array.isArray(target)) {
|
|
127
|
+
return defaultValue;
|
|
128
|
+
}
|
|
129
|
+
const parts = Array.isArray(key) ? key : key.split(".");
|
|
130
|
+
let result = target;
|
|
131
|
+
for (const part of parts) {
|
|
132
|
+
if (!isRecord(result) && !Array.isArray(result)) {
|
|
133
|
+
return defaultValue;
|
|
134
|
+
}
|
|
135
|
+
if (part in result) {
|
|
136
|
+
result = result[part];
|
|
137
|
+
} else {
|
|
138
|
+
return defaultValue;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
};
|
|
143
|
+
var data_set = (target, key, value) => {
|
|
144
|
+
if (!isRecord(target) && !Array.isArray(target)) {
|
|
145
|
+
return target;
|
|
146
|
+
}
|
|
147
|
+
const parts = Array.isArray(key) ? key : key.split(".");
|
|
148
|
+
let current = target;
|
|
149
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
150
|
+
const part = parts[i];
|
|
151
|
+
if (!isRecord(current)) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
const currentRecord = current;
|
|
155
|
+
if (!(part in currentRecord) || !isRecord(currentRecord[part])) {
|
|
156
|
+
currentRecord[part] = {};
|
|
157
|
+
}
|
|
158
|
+
current = currentRecord[part];
|
|
159
|
+
}
|
|
160
|
+
if (isRecord(current)) {
|
|
161
|
+
;
|
|
162
|
+
current[parts[parts.length - 1]] = value;
|
|
163
|
+
}
|
|
164
|
+
return target;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// src/obj.ts
|
|
168
|
+
var Objectable = class _Objectable {
|
|
169
|
+
/**
|
|
170
|
+
* Create a new Objectable instance.
|
|
171
|
+
*
|
|
172
|
+
* @param {T} value The initial object value
|
|
173
|
+
*/
|
|
174
|
+
constructor(value) {
|
|
175
|
+
this.value = structuredClone(value);
|
|
176
|
+
this.originalValue = structuredClone(value);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get the current object state.
|
|
180
|
+
*
|
|
181
|
+
* @returns {T} The current object
|
|
182
|
+
*/
|
|
183
|
+
toObject() {
|
|
184
|
+
return this.value;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Alias for toObject().
|
|
188
|
+
*
|
|
189
|
+
* @returns {T} The current object
|
|
190
|
+
*/
|
|
191
|
+
get() {
|
|
192
|
+
return this.value;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Get the original state from construction.
|
|
196
|
+
*
|
|
197
|
+
* @returns {T} The original object
|
|
198
|
+
*/
|
|
199
|
+
getOriginal() {
|
|
200
|
+
return this.originalValue;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Sync original state with current state (marking it as clean).
|
|
204
|
+
*
|
|
205
|
+
* @returns {this} The Objectable instance
|
|
206
|
+
*/
|
|
207
|
+
syncOriginal() {
|
|
208
|
+
this.originalValue = structuredClone(this.value);
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Determine if any attribute has been modified.
|
|
213
|
+
*
|
|
214
|
+
* @param {string} [key] Optional key to check specifically
|
|
215
|
+
* @returns {boolean} True if dirty, false otherwise
|
|
216
|
+
*/
|
|
217
|
+
isDirty(key) {
|
|
218
|
+
if (key) {
|
|
219
|
+
return !isEqual(data_get(this.originalValue, key), data_get(this.value, key));
|
|
220
|
+
}
|
|
221
|
+
return !isEqual(this.originalValue, this.value);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Determine if the object is equivalent to its original state.
|
|
225
|
+
*
|
|
226
|
+
* @param {string} [key] Optional key to check specifically
|
|
227
|
+
* @returns {boolean} True if clean, false otherwise
|
|
228
|
+
*/
|
|
229
|
+
isClean(key) {
|
|
230
|
+
return !this.isDirty(key);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Get the attributes that have been modified.
|
|
234
|
+
*
|
|
235
|
+
* @returns {Partial<T>} The dirty attributes
|
|
236
|
+
*/
|
|
237
|
+
getDirty() {
|
|
238
|
+
return diff(this.originalValue, this.value);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Get a value by dot notation.
|
|
242
|
+
*
|
|
243
|
+
* @param {string} key The key to retrieve
|
|
244
|
+
* @param {unknown} [defaultValue=null] The default value if not found
|
|
245
|
+
* @returns {unknown} The retrieved value
|
|
246
|
+
*/
|
|
247
|
+
dataGet(key, defaultValue = null) {
|
|
248
|
+
return data_get(this.value, key, defaultValue);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Set a value by dot notation.
|
|
252
|
+
*
|
|
253
|
+
* @param {string} key The key to set
|
|
254
|
+
* @param {unknown} value The value to set
|
|
255
|
+
* @returns {this} The Objectable instance
|
|
256
|
+
*/
|
|
257
|
+
dataSet(key, value) {
|
|
258
|
+
data_set(this.value, key, value);
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Get only specified keys.
|
|
263
|
+
*
|
|
264
|
+
* @param {K[]} keys The keys to include
|
|
265
|
+
* @returns {Objectable<Pick<T, K>>} A new Objectable instance
|
|
266
|
+
*/
|
|
267
|
+
only(keys) {
|
|
268
|
+
const result = {};
|
|
269
|
+
keys.forEach((key) => {
|
|
270
|
+
const k = key;
|
|
271
|
+
if (k in this.value) {
|
|
272
|
+
result[k] = this.value[k];
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
return new _Objectable(result);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Get all keys except specified ones.
|
|
279
|
+
*
|
|
280
|
+
* @param {K[]} keys The keys to exclude
|
|
281
|
+
* @returns {Objectable<Omit<T, K>>} A new Objectable instance
|
|
282
|
+
*/
|
|
283
|
+
except(keys) {
|
|
284
|
+
const result = { ...this.value };
|
|
285
|
+
keys.forEach((key) => {
|
|
286
|
+
const k = key;
|
|
287
|
+
delete result[k];
|
|
288
|
+
});
|
|
289
|
+
return new _Objectable(result);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Map over object entries.
|
|
293
|
+
*
|
|
294
|
+
* @param {Function} callback The callback to execute
|
|
295
|
+
* @returns {Record<string, U>} The resulting object
|
|
296
|
+
*/
|
|
297
|
+
map(callback) {
|
|
298
|
+
const result = {};
|
|
299
|
+
for (const [key, val] of Object.entries(this.value)) {
|
|
300
|
+
result[key] = callback(val, key);
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Filter object entries.
|
|
306
|
+
*
|
|
307
|
+
* @param {Function} callback The callback to execute
|
|
308
|
+
* @returns {this} The Objectable instance
|
|
309
|
+
*/
|
|
310
|
+
filter(callback) {
|
|
311
|
+
const result = {};
|
|
312
|
+
for (const [key, val] of Object.entries(this.value)) {
|
|
313
|
+
if (callback(val, key)) {
|
|
314
|
+
result[key] = val;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
this.value = result;
|
|
318
|
+
return this;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Merge with another object.
|
|
322
|
+
*
|
|
323
|
+
* @param {U} other The object to merge with
|
|
324
|
+
* @returns {Objectable<T & U>} A new Objectable instance
|
|
325
|
+
*/
|
|
326
|
+
merge(other) {
|
|
327
|
+
return new _Objectable({ ...this.value, ...other });
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Deep merge with another object (non-mutating on source).
|
|
331
|
+
*
|
|
332
|
+
* @param {Record<string, unknown>} other The object to deep merge with
|
|
333
|
+
* @returns {this} The Objectable instance
|
|
334
|
+
*/
|
|
335
|
+
mergeDeep(other) {
|
|
336
|
+
const merge = (target, source) => {
|
|
337
|
+
const result = { ...target };
|
|
338
|
+
for (const key of Object.keys(source)) {
|
|
339
|
+
const srcVal = source[key];
|
|
340
|
+
const tgtVal = target[key];
|
|
341
|
+
if (srcVal !== null && "object" === typeof srcVal && !Array.isArray(srcVal) && tgtVal !== null && "object" === typeof tgtVal && !Array.isArray(tgtVal)) {
|
|
342
|
+
result[key] = merge(tgtVal, srcVal);
|
|
343
|
+
} else {
|
|
344
|
+
result[key] = srcVal;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return result;
|
|
348
|
+
};
|
|
349
|
+
this.value = merge(this.value, other);
|
|
350
|
+
return this;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Check if object has key.
|
|
354
|
+
*
|
|
355
|
+
* @param {string} key The key to check
|
|
356
|
+
* @returns {boolean} True if key exists
|
|
357
|
+
*/
|
|
358
|
+
has(key) {
|
|
359
|
+
return key in this.value;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Get keys.
|
|
363
|
+
*
|
|
364
|
+
* @returns {string[]} The object keys
|
|
365
|
+
*/
|
|
366
|
+
keys() {
|
|
367
|
+
return Object.keys(this.value);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Get values.
|
|
371
|
+
*
|
|
372
|
+
* @returns {unknown[]} The object values
|
|
373
|
+
*/
|
|
374
|
+
values() {
|
|
375
|
+
return Object.values(this.value);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Clone the object.
|
|
379
|
+
*
|
|
380
|
+
* @returns {Objectable<T>} A new cloned Objectable instance
|
|
381
|
+
*/
|
|
382
|
+
clone() {
|
|
383
|
+
return new _Objectable(structuredClone(this.value));
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Pipe to a callback.
|
|
387
|
+
*
|
|
388
|
+
* @param {Function} callback The callback to execute
|
|
389
|
+
* @returns {U} The result of the callback
|
|
390
|
+
*/
|
|
391
|
+
pipe(callback) {
|
|
392
|
+
return callback(this);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Execute callback if condition is met.
|
|
396
|
+
*
|
|
397
|
+
* @param {boolean | Function} condition The condition to check
|
|
398
|
+
* @param {Function} callback The callback to execute
|
|
399
|
+
* @returns {this} The Objectable instance
|
|
400
|
+
*/
|
|
401
|
+
when(condition, callback) {
|
|
402
|
+
const shouldExecute = "function" === typeof condition ? condition() : condition;
|
|
403
|
+
if (shouldExecute) {
|
|
404
|
+
callback(this);
|
|
405
|
+
}
|
|
406
|
+
return this;
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
var obj = (value) => {
|
|
410
|
+
return new Objectable(value);
|
|
411
|
+
};
|
|
412
|
+
var Obj = {
|
|
413
|
+
/** Create a new Objectable instance */
|
|
414
|
+
of: (value) => new Objectable(value),
|
|
415
|
+
/** Get object keys */
|
|
416
|
+
keys: (value) => Object.keys(value),
|
|
417
|
+
/** Get object values */
|
|
418
|
+
values: (value) => Object.values(value),
|
|
419
|
+
/** Merge objects */
|
|
420
|
+
merge: (target, ...sources) => Object.assign({}, target, ...sources)
|
|
421
|
+
};
|
|
422
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
423
|
+
0 && (module.exports = {
|
|
424
|
+
Obj,
|
|
425
|
+
Objectable,
|
|
426
|
+
obj
|
|
427
|
+
});
|
package/dist/obj.d.cts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fluent object manipulation class with state tracking.
|
|
3
|
+
*/
|
|
4
|
+
declare class Objectable<T extends Record<string, unknown>> {
|
|
5
|
+
protected value: T;
|
|
6
|
+
protected originalValue: T;
|
|
7
|
+
/**
|
|
8
|
+
* Create a new Objectable instance.
|
|
9
|
+
*
|
|
10
|
+
* @param {T} value The initial object value
|
|
11
|
+
*/
|
|
12
|
+
constructor(value: T);
|
|
13
|
+
/**
|
|
14
|
+
* Get the current object state.
|
|
15
|
+
*
|
|
16
|
+
* @returns {T} The current object
|
|
17
|
+
*/
|
|
18
|
+
toObject(): T;
|
|
19
|
+
/**
|
|
20
|
+
* Alias for toObject().
|
|
21
|
+
*
|
|
22
|
+
* @returns {T} The current object
|
|
23
|
+
*/
|
|
24
|
+
get(): T;
|
|
25
|
+
/**
|
|
26
|
+
* Get the original state from construction.
|
|
27
|
+
*
|
|
28
|
+
* @returns {T} The original object
|
|
29
|
+
*/
|
|
30
|
+
getOriginal(): T;
|
|
31
|
+
/**
|
|
32
|
+
* Sync original state with current state (marking it as clean).
|
|
33
|
+
*
|
|
34
|
+
* @returns {this} The Objectable instance
|
|
35
|
+
*/
|
|
36
|
+
syncOriginal(): this;
|
|
37
|
+
/**
|
|
38
|
+
* Determine if any attribute has been modified.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} [key] Optional key to check specifically
|
|
41
|
+
* @returns {boolean} True if dirty, false otherwise
|
|
42
|
+
*/
|
|
43
|
+
isDirty(key?: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Determine if the object is equivalent to its original state.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} [key] Optional key to check specifically
|
|
48
|
+
* @returns {boolean} True if clean, false otherwise
|
|
49
|
+
*/
|
|
50
|
+
isClean(key?: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Get the attributes that have been modified.
|
|
53
|
+
*
|
|
54
|
+
* @returns {Partial<T>} The dirty attributes
|
|
55
|
+
*/
|
|
56
|
+
getDirty(): Partial<T>;
|
|
57
|
+
/**
|
|
58
|
+
* Get a value by dot notation.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} key The key to retrieve
|
|
61
|
+
* @param {unknown} [defaultValue=null] The default value if not found
|
|
62
|
+
* @returns {unknown} The retrieved value
|
|
63
|
+
*/
|
|
64
|
+
dataGet(key: string, defaultValue?: unknown): unknown;
|
|
65
|
+
/**
|
|
66
|
+
* Set a value by dot notation.
|
|
67
|
+
*
|
|
68
|
+
* @param {string} key The key to set
|
|
69
|
+
* @param {unknown} value The value to set
|
|
70
|
+
* @returns {this} The Objectable instance
|
|
71
|
+
*/
|
|
72
|
+
dataSet(key: string, value: unknown): this;
|
|
73
|
+
/**
|
|
74
|
+
* Get only specified keys.
|
|
75
|
+
*
|
|
76
|
+
* @param {K[]} keys The keys to include
|
|
77
|
+
* @returns {Objectable<Pick<T, K>>} A new Objectable instance
|
|
78
|
+
*/
|
|
79
|
+
only<K extends keyof T>(keys: K[]): Objectable<Pick<T, K>>;
|
|
80
|
+
/**
|
|
81
|
+
* Get all keys except specified ones.
|
|
82
|
+
*
|
|
83
|
+
* @param {K[]} keys The keys to exclude
|
|
84
|
+
* @returns {Objectable<Omit<T, K>>} A new Objectable instance
|
|
85
|
+
*/
|
|
86
|
+
except<K extends keyof T>(keys: K[]): Objectable<Omit<T, K>>;
|
|
87
|
+
/**
|
|
88
|
+
* Map over object entries.
|
|
89
|
+
*
|
|
90
|
+
* @param {Function} callback The callback to execute
|
|
91
|
+
* @returns {Record<string, U>} The resulting object
|
|
92
|
+
*/
|
|
93
|
+
map<U>(callback: (value: T[keyof T], key: keyof T) => U): Record<string, U>;
|
|
94
|
+
/**
|
|
95
|
+
* Filter object entries.
|
|
96
|
+
*
|
|
97
|
+
* @param {Function} callback The callback to execute
|
|
98
|
+
* @returns {this} The Objectable instance
|
|
99
|
+
*/
|
|
100
|
+
filter(callback: (value: T[keyof T], key: keyof T) => boolean): this;
|
|
101
|
+
/**
|
|
102
|
+
* Merge with another object.
|
|
103
|
+
*
|
|
104
|
+
* @param {U} other The object to merge with
|
|
105
|
+
* @returns {Objectable<T & U>} A new Objectable instance
|
|
106
|
+
*/
|
|
107
|
+
merge<U extends Record<string, unknown>>(other: U): Objectable<T & U>;
|
|
108
|
+
/**
|
|
109
|
+
* Deep merge with another object (non-mutating on source).
|
|
110
|
+
*
|
|
111
|
+
* @param {Record<string, unknown>} other The object to deep merge with
|
|
112
|
+
* @returns {this} The Objectable instance
|
|
113
|
+
*/
|
|
114
|
+
mergeDeep(other: Record<string, unknown>): this;
|
|
115
|
+
/**
|
|
116
|
+
* Check if object has key.
|
|
117
|
+
*
|
|
118
|
+
* @param {string} key The key to check
|
|
119
|
+
* @returns {boolean} True if key exists
|
|
120
|
+
*/
|
|
121
|
+
has(key: string): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Get keys.
|
|
124
|
+
*
|
|
125
|
+
* @returns {string[]} The object keys
|
|
126
|
+
*/
|
|
127
|
+
keys(): string[];
|
|
128
|
+
/**
|
|
129
|
+
* Get values.
|
|
130
|
+
*
|
|
131
|
+
* @returns {unknown[]} The object values
|
|
132
|
+
*/
|
|
133
|
+
values(): unknown[];
|
|
134
|
+
/**
|
|
135
|
+
* Clone the object.
|
|
136
|
+
*
|
|
137
|
+
* @returns {Objectable<T>} A new cloned Objectable instance
|
|
138
|
+
*/
|
|
139
|
+
clone(): Objectable<T>;
|
|
140
|
+
/**
|
|
141
|
+
* Pipe to a callback.
|
|
142
|
+
*
|
|
143
|
+
* @param {Function} callback The callback to execute
|
|
144
|
+
* @returns {U} The result of the callback
|
|
145
|
+
*/
|
|
146
|
+
pipe<U>(callback: (obj: this) => U): U;
|
|
147
|
+
/**
|
|
148
|
+
* Execute callback if condition is met.
|
|
149
|
+
*
|
|
150
|
+
* @param {boolean | Function} condition The condition to check
|
|
151
|
+
* @param {Function} callback The callback to execute
|
|
152
|
+
* @returns {this} The Objectable instance
|
|
153
|
+
*/
|
|
154
|
+
when(condition: boolean | (() => boolean), callback: (obj: this) => void): this;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Create a new fluent Objectable instance.
|
|
158
|
+
*
|
|
159
|
+
* @param {T} value The initial value
|
|
160
|
+
* @returns {Objectable<T>} A new Objectable instance
|
|
161
|
+
*/
|
|
162
|
+
declare const obj: <T extends Record<string, unknown>>(value: T) => Objectable<T>;
|
|
163
|
+
/**
|
|
164
|
+
* Static methods for object manipulation.
|
|
165
|
+
*/
|
|
166
|
+
declare const Obj: {
|
|
167
|
+
/** Create a new Objectable instance */
|
|
168
|
+
of: <T extends Record<string, unknown>>(value: T) => Objectable<T>;
|
|
169
|
+
/** Get object keys */
|
|
170
|
+
keys: (value: Record<string, unknown>) => string[];
|
|
171
|
+
/** Get object values */
|
|
172
|
+
values: (value: Record<string, unknown>) => unknown[];
|
|
173
|
+
/** Merge objects */
|
|
174
|
+
merge: (target: Record<string, unknown>, ...sources: Record<string, unknown>[]) => any;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export { Obj, Objectable, obj };
|