ts-object-lite 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/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/object.d.ts +18 -0
- package/dist/object.js +104 -0
- package/package.json +19 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './object';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./object"), exports);
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class ObjectUtils {
|
|
2
|
+
deepGet(obj: unknown, path: string, defaultValue?: unknown): unknown;
|
|
3
|
+
deepSet(obj: Record<string, unknown>, path: string, value: unknown): void;
|
|
4
|
+
deepClone<T>(obj: T): T;
|
|
5
|
+
merge<T extends Record<string, unknown>>(target: T, ...sources: (Partial<T> | undefined | null)[]): T;
|
|
6
|
+
omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Partial<T>;
|
|
7
|
+
pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Partial<T>;
|
|
8
|
+
isEmpty(value: unknown): boolean;
|
|
9
|
+
isEqual(a: unknown, b: unknown): boolean;
|
|
10
|
+
keys(obj: Record<string, unknown>): string[];
|
|
11
|
+
values(obj: Record<string, unknown>): unknown[];
|
|
12
|
+
entries(obj: Record<string, unknown>): [string, unknown][];
|
|
13
|
+
has(obj: Record<string, unknown>, path: string): boolean;
|
|
14
|
+
size(obj: Record<string, unknown>): number;
|
|
15
|
+
mapValues(obj: Record<string, unknown>, fn: (v: unknown, k: string) => unknown): Record<string, unknown>;
|
|
16
|
+
invert(obj: Record<string, string>): Record<string, string>;
|
|
17
|
+
clone<T>(obj: T): T;
|
|
18
|
+
}
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObjectUtils = void 0;
|
|
4
|
+
class ObjectUtils {
|
|
5
|
+
deepGet(obj, path, defaultValue) {
|
|
6
|
+
if (!obj || !path)
|
|
7
|
+
return defaultValue;
|
|
8
|
+
const keys = path.split('.');
|
|
9
|
+
let result = obj;
|
|
10
|
+
for (const key of keys) {
|
|
11
|
+
if (result === null || result === undefined)
|
|
12
|
+
return defaultValue;
|
|
13
|
+
result = result[key];
|
|
14
|
+
}
|
|
15
|
+
return result ?? defaultValue;
|
|
16
|
+
}
|
|
17
|
+
deepSet(obj, path, value) {
|
|
18
|
+
const keys = path.split('.');
|
|
19
|
+
let current = obj;
|
|
20
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
21
|
+
if (!(keys[i] in current) || current[keys[i]] === null)
|
|
22
|
+
current[keys[i]] = {};
|
|
23
|
+
current = current[keys[i]];
|
|
24
|
+
}
|
|
25
|
+
current[keys[keys.length - 1]] = value;
|
|
26
|
+
}
|
|
27
|
+
deepClone(obj) {
|
|
28
|
+
if (obj === null || typeof obj !== 'object')
|
|
29
|
+
return obj;
|
|
30
|
+
if (obj instanceof Date)
|
|
31
|
+
return new Date(obj.getTime());
|
|
32
|
+
if (Array.isArray(obj))
|
|
33
|
+
return obj.map(item => this.deepClone(item));
|
|
34
|
+
const cloned = {};
|
|
35
|
+
for (const key in obj)
|
|
36
|
+
cloned[key] = this.deepClone(obj[key]);
|
|
37
|
+
return cloned;
|
|
38
|
+
}
|
|
39
|
+
merge(target, ...sources) {
|
|
40
|
+
for (const source of sources) {
|
|
41
|
+
if (!source)
|
|
42
|
+
continue;
|
|
43
|
+
for (const key of Object.keys(source)) {
|
|
44
|
+
const tv = target[key], sv = source[key];
|
|
45
|
+
if (tv && typeof tv === 'object' && sv && typeof sv === 'object' && !Array.isArray(tv) && !Array.isArray(sv)) {
|
|
46
|
+
target[key] = this.merge(tv, sv);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
target[key] = sv;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return target;
|
|
54
|
+
}
|
|
55
|
+
omit(obj, keys) {
|
|
56
|
+
const result = { ...obj };
|
|
57
|
+
keys.forEach(k => delete result[k]);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
pick(obj, keys) {
|
|
61
|
+
const result = {};
|
|
62
|
+
keys.forEach(k => { if (k in obj)
|
|
63
|
+
result[k] = obj[k]; });
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
isEmpty(value) {
|
|
67
|
+
if (value === null || value === undefined)
|
|
68
|
+
return true;
|
|
69
|
+
if (typeof value === 'string')
|
|
70
|
+
return value.trim().length === 0;
|
|
71
|
+
if (Array.isArray(value))
|
|
72
|
+
return value.length === 0;
|
|
73
|
+
if (typeof value === 'object')
|
|
74
|
+
return Object.keys(value).length === 0;
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
isEqual(a, b) {
|
|
78
|
+
if (a === b)
|
|
79
|
+
return true;
|
|
80
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object')
|
|
81
|
+
return false;
|
|
82
|
+
const ak = Object.keys(a), bk = Object.keys(b);
|
|
83
|
+
return ak.length === bk.length && ak.every(k => this.isEqual(a[k], b[k]));
|
|
84
|
+
}
|
|
85
|
+
keys(obj) { return Object.keys(obj); }
|
|
86
|
+
values(obj) { return Object.values(obj); }
|
|
87
|
+
entries(obj) { return Object.entries(obj); }
|
|
88
|
+
has(obj, path) { return this.deepGet(obj, path) !== undefined; }
|
|
89
|
+
size(obj) { return Object.keys(obj).length; }
|
|
90
|
+
mapValues(obj, fn) {
|
|
91
|
+
const result = {};
|
|
92
|
+
for (const k of Object.keys(obj))
|
|
93
|
+
result[k] = fn(obj[k], k);
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
invert(obj) {
|
|
97
|
+
const result = {};
|
|
98
|
+
for (const k of Object.keys(obj))
|
|
99
|
+
result[obj[k]] = k;
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
clone(obj) { return this.deepClone(obj); }
|
|
103
|
+
}
|
|
104
|
+
exports.ObjectUtils = ObjectUtils;
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-object-lite",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight, type-safe object utility library for web developers",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist"],
|
|
16
|
+
"keywords": ["utils", "typescript", "object", "helpers", "lite"],
|
|
17
|
+
"author": "Gaurang Mody - G8X",
|
|
18
|
+
"license": "MIT"
|
|
19
|
+
}
|