esoftplay 0.0.140-s → 0.0.140-u
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/modules/lib/object.ts +91 -28
- package/package.json +1 -1
package/modules/lib/object.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import { update } from "immhelper";
|
|
4
4
|
|
|
5
|
+
type FilterByItem = (item: any, index: number) => boolean
|
|
6
|
+
|
|
5
7
|
export default class m {
|
|
6
8
|
#value = undefined
|
|
7
9
|
|
|
@@ -18,52 +20,81 @@ export default class m {
|
|
|
18
20
|
this.cursorBuilder = this.cursorBuilder.bind(this)
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
cursorBuilder(command: string, array: any, value: any, ...values: any[]): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
22
|
-
return (cursor?: string | number, ...cursors: (string | number)[]) => {
|
|
23
|
-
let
|
|
24
|
-
let
|
|
23
|
+
cursorBuilder(command: string, array: any, value: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
24
|
+
return (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => {
|
|
25
|
+
let _value = array
|
|
26
|
+
let cCursor = [cursor, ...cursors]
|
|
25
27
|
let spec = {}
|
|
28
|
+
let hasError = false
|
|
29
|
+
|
|
30
|
+
cCursor = [cursor, ...cursors].map((item) => {
|
|
31
|
+
if (typeof item == 'string') {
|
|
32
|
+
_value = _value[item]
|
|
33
|
+
return item
|
|
34
|
+
} else if (typeof item == 'function') {
|
|
35
|
+
if (Array.isArray(_value)) {
|
|
36
|
+
const idx = _value.findIndex(item)
|
|
37
|
+
if (idx > -1) {
|
|
38
|
+
_value = _value[idx]
|
|
39
|
+
return idx
|
|
40
|
+
} else {
|
|
41
|
+
hasError = true
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
hasError = true
|
|
46
|
+
return undefined
|
|
47
|
+
// new Error("LibObject : CursorByFilter must be executed at array")
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
let pathToUpdate = cCursor.filter(x => x != undefined).join('.')
|
|
53
|
+
let allValues = [value, ...values].filter(x => x != undefined)
|
|
26
54
|
if (pathToUpdate != '')
|
|
27
55
|
spec = { [pathToUpdate]: [command, ...allValues] }
|
|
28
56
|
else
|
|
29
57
|
spec = [command, ...allValues]
|
|
30
|
-
|
|
58
|
+
if (hasError)
|
|
59
|
+
this.#value = array
|
|
60
|
+
else
|
|
61
|
+
this.#value = update(array, spec)
|
|
31
62
|
return this
|
|
32
63
|
}
|
|
33
64
|
}
|
|
34
65
|
|
|
35
|
-
push(value?: any, ...values: any[]): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
66
|
+
push(value?: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
36
67
|
return this.cursorBuilder("push", this.#value, value, ...values)
|
|
37
68
|
}
|
|
38
69
|
|
|
39
|
-
unshift(value?: any, ...values: any[]): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
70
|
+
unshift(value?: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
40
71
|
return this.cursorBuilder("unshift", this.#value, value, ...values)
|
|
41
72
|
}
|
|
42
73
|
|
|
43
|
-
splice(index: number, deleteCount: number, value?: any, ...values: any[]): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
74
|
+
splice(index: number, deleteCount: number, value?: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
44
75
|
return this.cursorBuilder("splice", this.#value, index, deleteCount, value, ...values)
|
|
45
76
|
}
|
|
46
|
-
unset(index: number | string, ...indexs: (string | number)[]): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
77
|
+
unset(index: number | string, ...indexs: (string | number | FilterByItem)[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
47
78
|
return this.cursorBuilder("unset", this.#value, index, ...indexs)
|
|
48
79
|
}
|
|
49
80
|
|
|
50
|
-
set(value: any): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
81
|
+
set(value: any): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
51
82
|
return this.cursorBuilder("set", this.#value, value)
|
|
52
83
|
}
|
|
53
84
|
|
|
54
|
-
update(callback: (lastValue: any) => any): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
85
|
+
update(callback: (lastValue: any) => any): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
55
86
|
return this.cursorBuilder("batch", this.#value, callback)
|
|
56
87
|
}
|
|
57
88
|
|
|
58
|
-
assign(obj1: any): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
89
|
+
assign(obj1: any): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
59
90
|
return this.cursorBuilder("assign", this.#value, deepCopy(obj1))
|
|
60
91
|
}
|
|
61
92
|
|
|
62
|
-
removeKeys(deletedItemKeys: string[]): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
93
|
+
removeKeys(deletedItemKeys: string[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
63
94
|
return this.cursorBuilder("batch", this.#value, (arr: any) => _removeKeys(arr, deletedItemKeys))
|
|
64
95
|
}
|
|
65
96
|
|
|
66
|
-
replaceItem<T>(filter: (item: T, index: number) => boolean, newItem: T): (cursor?: string | number, ...cursors: (string | number)[]) => this {
|
|
97
|
+
replaceItem<T>(filter: (item: T, index: number) => boolean, newItem: T): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => this {
|
|
67
98
|
return this.cursorBuilder("batch", this.#value, (arr: any) => _replaceItem(arr, filter, newItem))
|
|
68
99
|
}
|
|
69
100
|
|
|
@@ -71,45 +102,77 @@ export default class m {
|
|
|
71
102
|
return this.#value
|
|
72
103
|
}
|
|
73
104
|
|
|
74
|
-
static push(array: any, value?: any, ...values: any[]): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
105
|
+
static push(array: any, value?: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
75
106
|
return cursorBuilder("push", array, value, ...values)
|
|
76
107
|
}
|
|
77
|
-
static unshift(array: any, value?: any, ...values: any[]): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
108
|
+
static unshift(array: any, value?: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
78
109
|
return cursorBuilder("unshift", array, value, ...values)
|
|
79
110
|
}
|
|
80
|
-
static removeKeys(arrayOrObj: any, deletedItemKeys: string[]): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
111
|
+
static removeKeys(arrayOrObj: any, deletedItemKeys: string[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
81
112
|
return cursorBuilder("batch", arrayOrObj, (arrOrObj: any) => _removeKeys(arrOrObj, deletedItemKeys))
|
|
82
113
|
}
|
|
83
|
-
static replaceItem<T>(arrayOrObj: any, filter: (item: T, index: number) => boolean, newItem: T): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
114
|
+
static replaceItem<T>(arrayOrObj: any, filter: (item: T, index: number) => boolean, newItem: T): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
84
115
|
return cursorBuilder("batch", arrayOrObj, (arrOrObj: any) => _replaceItem(arrOrObj, filter, newItem))
|
|
85
116
|
}
|
|
86
|
-
static splice(array: any, index: number, deleteCount: number, value?: any, ...values: any[]): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
117
|
+
static splice(array: any, index: number, deleteCount: number, value?: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
87
118
|
return cursorBuilder("splice", array, index, deleteCount, value, ...values)
|
|
88
119
|
}
|
|
89
|
-
static unset(obj: any, index: number | string, ...indexs: (string | number)[]): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
120
|
+
static unset(obj: any, index: number | string, ...indexs: (string | number | FilterByItem)[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
90
121
|
return cursorBuilder("unset", obj, index, ...indexs)
|
|
91
122
|
}
|
|
92
|
-
static set(obj: any, value: any): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
123
|
+
static set(obj: any, value: any): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
93
124
|
return cursorBuilder("set", obj, value)
|
|
94
125
|
}
|
|
95
|
-
static update(obj: any, callback: (lastValue: any) => any): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
126
|
+
static update(obj: any, callback: (lastValue: any) => any): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
96
127
|
return cursorBuilder("batch", obj, callback)
|
|
97
128
|
}
|
|
98
|
-
static assign(obj: any, obj1: any): (cursor?: string | number, ...cursors: (string | number)[]) => any {
|
|
129
|
+
static assign(obj: any, obj1: any): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
99
130
|
return cursorBuilder("assign", obj, deepCopy(obj1))
|
|
100
131
|
}
|
|
101
132
|
}
|
|
102
133
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
function cursorBuilder(command: string, array: any, value: any, ...values: any[]): (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) => any {
|
|
137
|
+
return function (cursor?: string | number | FilterByItem, ...cursors: (string | number | FilterByItem)[]) {
|
|
138
|
+
let _value = array
|
|
139
|
+
let cCursor = [cursor, ...cursors]
|
|
107
140
|
let spec = {}
|
|
141
|
+
let hasError = false
|
|
142
|
+
|
|
143
|
+
cCursor = [cursor, ...cursors].map((item) => {
|
|
144
|
+
if (typeof item == 'string') {
|
|
145
|
+
_value = _value[item]
|
|
146
|
+
return item
|
|
147
|
+
} else if (typeof item == 'function') {
|
|
148
|
+
if (Array.isArray(_value)) {
|
|
149
|
+
const idx = _value.findIndex(item)
|
|
150
|
+
if (idx > -1) {
|
|
151
|
+
_value = _value[idx]
|
|
152
|
+
return idx
|
|
153
|
+
} else {
|
|
154
|
+
hasError = true
|
|
155
|
+
return undefined
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
hasError = true
|
|
159
|
+
return undefined
|
|
160
|
+
// new Error("LibObject : CursorByFilter must be executed at array")
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
let pathToUpdate = cCursor.filter(x => x != undefined).join('.')
|
|
166
|
+
let allValues = [value, ...values].filter(x => x != undefined)
|
|
108
167
|
if (pathToUpdate != '')
|
|
109
168
|
spec = { [pathToUpdate]: [command, ...allValues] }
|
|
110
169
|
else
|
|
111
170
|
spec = [command, ...allValues]
|
|
112
|
-
|
|
171
|
+
|
|
172
|
+
if (hasError)
|
|
173
|
+
return array
|
|
174
|
+
else
|
|
175
|
+
return update(array, spec)
|
|
113
176
|
}
|
|
114
177
|
}
|
|
115
178
|
|