Repository navigation
Expand file tree
/
Copy pathObjectMap.ts
More file actions
193 lines (157 loc) · 4.65 KB
/
Copy pathObjectMap.ts
File metadata and controls
193 lines (157 loc) · 4.65 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Essentials, ObjectMerge } from '../deps.ts'
import { ObjectMapValue } from './ObjectMapValue.ts'
import { ObjectMapOptions } from './ObjectMapOptions.ts'
export type ObjectMapValueFilter = (objmap: ObjectMapValue) => boolean
const DefaultObjectMapOptions: Essentials.DeepPartial<ObjectMapOptions> = {
include: {
arrays: true,
dates: true,
functions: false,
numbers: true,
objects: true,
strings: true,
},
}
export class ObjectMap {
private readonly objmap: ObjectMapValue
private readonly options: ObjectMapOptions
constructor(instance: any, options: Essentials.DeepPartial<ObjectMapOptions> = {}) {
this.options = ObjectMerge.merge<ObjectMapOptions>({}, DefaultObjectMapOptions, options)
this.objmap = {
name: 'root',
path: [],
properties: [],
type: this.type(instance),
value: instance,
}
Object.keys(instance).forEach((name) => this.map(this.objmap, name))
}
get nodes(): ObjectMapValue[] {
return this.select()
}
get root(): ObjectMapValue {
return this.objmap
}
get(path: string = ''): ObjectMapValue {
if (path === '' || path === '.') {
return this.root
}
return path
.trim()
.split('.')
.reduce<ObjectMapValue>(
(result, property) => result.properties.reduce((_, current) => (current.name === property ? current : _), result),
this.root,
)
}
getValue<T>(path: string): T {
return this.get(path).value
}
paths(): string[] {
return this.select()
.map((objmap) => this.pathstr(objmap))
.sort()
}
select(filter: ObjectMapValueFilter | undefined = () => true): ObjectMapValue[] {
const apply = (objmap: ObjectMapValue, list: ObjectMapValue[]): ObjectMapValue[] => {
return objmap.properties.reduce<ObjectMapValue[]>((results, property) => {
const properties = apply(property, [])
if (filter(property)) {
return results.concat(properties).concat([property])
}
return results.concat([property])
}, list)
}
return apply(this.root, [])
}
set<T>(path: string, value: T): ObjectMap {
this.get(path).value = value
return this
}
toObject(): any {
return this.materialze()
}
private include(type: string): boolean {
switch (type) {
case 'array':
return this.options.include.arrays
case 'date':
return this.options.include.dates
case 'function':
return this.options.include.functions
case 'number':
return this.options.include.numbers
case 'object':
return this.options.include.objects
case 'string':
return this.options.include.strings
default:
return true
}
}
private map(objmap: ObjectMapValue, name: string): ObjectMapValue {
const value = objmap.value[name]
const type = this.type(value)
const included = this.include(type)
const objvalue: ObjectMapValue = {
name,
path: objmap.path.concat([name]),
properties: [],
type,
value,
}
if (included && objvalue.value && (objvalue.type === 'object' || objvalue.type === 'array')) {
objvalue.properties = Object.keys(objvalue.value).reduce<ObjectMapValue[]>((results, property) => {
const map = this.map(objvalue, property)
if (this.include(map.type)) {
return [...results, map]
}
return results
}, [])
}
if (included) {
objmap.properties.push(objvalue)
}
return objvalue
}
private materialze(): any {
const conjure = (objmap: ObjectMapValue, target: any): any => {
if (this.include(objmap.type) === false) {
return target
}
return objmap.properties
.filter((property) => this.include(property.type))
.reduce<any>((result, property) => {
if (property.type === 'array') {
result[property.name] = property.properties.map((propname) => conjure(propname, {}))
} else if (property.type === 'object') {
result[property.name] = conjure(property, {})
} else {
result[property.name] = property.value
}
return result
}, target)
}
const material = {}
conjure(this.root, material)
return material
}
private pathstr(objmap: ObjectMapValue): string {
return objmap.path.join('.')
}
private type(value: any): string {
const type = typeof value
switch (type) {
case 'object':
if (Array.isArray(value)) {
return 'array'
}
// if (types.isDate(value)) {
// return 'date'
// }
return type
default:
return type
}
}
}