-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyjson.ts
132 lines (111 loc) · 3.16 KB
/
easyjson.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// easyjson.ts
type Callback = (error: Error | null, data?: any) => void;
class EasyJson {
private data: any;
private callback?: Callback;
constructor(initialData: any = {}, callback?: Callback) {
this.data = initialData;
this.callback = callback;
}
// Initialize new instance with optional callback
static new(initialData: any = {}, callback?: Callback): EasyJson {
return new EasyJson(initialData, callback);
}
// Search for a path in JSON and return data
search(path: string, callback?: Callback): any {
const keys = path.split('.');
let result = this.data;
try {
for (let key of keys) {
result = result[key];
if (result === undefined) {
throw new Error('Path not found');
}
}
if (callback) callback(null, result);
return result;
} catch (error) {
if (callback) callback(error);
return undefined;
}
}
// Delete a node at the specified path
del(path: string, callback?: Callback): boolean {
const keys = path.split('.');
let result = this.data;
let parent: any = this.data;
const lastKey = keys.pop();
try {
for (let key of keys) {
parent = result[key];
if (parent === undefined) throw new Error('Path not found');
result = parent;
}
if (lastKey && result[lastKey] !== undefined) {
delete result[lastKey];
if (callback) callback(null, true);
return true;
} else {
throw new Error('Path not found');
}
} catch (error) {
if (callback) callback(error);
return false;
}
}
// Add a new node at the specified path with optional data
add(path: string, value: any = null, callback?: Callback): boolean {
const keys = path.split('.');
let result = this.data;
try {
for (let i = 0; i < keys.length - 1; i++) {
result = result[keys[i]] = result[keys[i]] || {};
}
const lastKey = keys[keys.length - 1];
result[lastKey] = value;
if (callback) callback(null, true);
return true;
} catch (error) {
if (callback) callback(error);
return false;
}
}
// Edit a node at the specified path
edit(path: string, value: any, callback?: Callback): boolean {
const keys = path.split('.');
let result = this.data;
try {
for (let i = 0; i < keys.length - 1; i++) {
result = result[keys[i]] = result[keys[i]] || {};
}
const lastKey = keys[keys.length - 1];
if (result[lastKey] === undefined) {
throw new Error('Path not found');
}
result[lastKey] = value;
if (callback) callback(null, true);
return true;
} catch (error) {
if (callback) callback(error);
return false;
}
}
// Check if the node exists at the specified path
exists(path: string): boolean {
const keys = path.split('.');
let result = this.data;
try {
for (let key of keys) {
result = result[key];
if (result === undefined) {
return false;
}
}
return true;
} catch (error) {
return false;
}
}
}
// Export all methods for external use
export const ej = EasyJson;