@nxtedition/lib 14.0.8 → 14.0.10
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/package.json +1 -1
- package/util/template/index.js +143 -106
package/package.json
CHANGED
package/util/template/index.js
CHANGED
|
@@ -42,102 +42,172 @@ module.exports = (options) => {
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
const hashTemplate = (template, prefix) => {
|
|
46
|
+
if (fp.isPlainObject(template)) {
|
|
47
|
+
let hashes
|
|
48
|
+
for (const key of Object.keys(template)) {
|
|
49
|
+
const hash = hashTemplate(template[key], key)
|
|
50
|
+
if (hash) {
|
|
51
|
+
hashes ??= []
|
|
52
|
+
hashes.push(hash)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return hashes ? objectHash([prefix, hashes]) : ''
|
|
56
|
+
} else if (fp.isArray(template)) {
|
|
57
|
+
let hashes
|
|
58
|
+
for (let idx = 0; idx < template.length; idx++) {
|
|
59
|
+
const hash = hashTemplate(template[idx], idx)
|
|
60
|
+
if (hash) {
|
|
61
|
+
hashes ??= []
|
|
62
|
+
hashes.push(hash)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return hashes ? objectHash([prefix, hashes]) : ''
|
|
66
|
+
} else if (isTemplate(template)) {
|
|
67
|
+
return objectHash([prefix, template])
|
|
68
|
+
} else {
|
|
69
|
+
return ''
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const _compileArrayTemplate = weakCache(
|
|
74
|
+
(arr, hash) => {
|
|
75
|
+
if (!fp.isArray(arr)) {
|
|
76
|
+
throw new Error('invalid argument')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!hash) {
|
|
80
|
+
return null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let resolvers
|
|
84
|
+
let indices
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i < arr.length; i++) {
|
|
87
|
+
const resolver = compileTemplate(arr[i])
|
|
88
|
+
if (resolver) {
|
|
89
|
+
resolvers ??= []
|
|
90
|
+
resolvers.push(resolver)
|
|
91
|
+
indices ??= []
|
|
92
|
+
indices.push(i)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return resolvers
|
|
97
|
+
? (args$) =>
|
|
98
|
+
rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
|
|
99
|
+
rx.map((values) => {
|
|
100
|
+
const ret = [...arr]
|
|
101
|
+
for (let n = 0; n < values.length; n++) {
|
|
102
|
+
ret[indices[n]] = values[n]
|
|
103
|
+
}
|
|
104
|
+
return ret
|
|
105
|
+
})
|
|
106
|
+
)
|
|
107
|
+
: null
|
|
108
|
+
},
|
|
109
|
+
(arr, hash) => hash ?? hashTemplate(arr)
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
const compileArrayTemplate = (arr) => {
|
|
46
113
|
if (!fp.isArray(arr)) {
|
|
47
114
|
throw new Error('invalid argument')
|
|
48
115
|
}
|
|
116
|
+
const hash = hashTemplate(arr)
|
|
117
|
+
return hash ? _compileArrayTemplate(arr, hash) : null
|
|
118
|
+
}
|
|
49
119
|
|
|
50
|
-
|
|
51
|
-
|
|
120
|
+
const _compileObjectTemplate = weakCache(
|
|
121
|
+
(obj, hash) => {
|
|
122
|
+
if (!fp.isPlainObject(obj)) {
|
|
123
|
+
throw new Error('invalid argument')
|
|
124
|
+
}
|
|
52
125
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (resolver) {
|
|
56
|
-
resolvers ??= []
|
|
57
|
-
resolvers.push(resolver)
|
|
58
|
-
indices ??= []
|
|
59
|
-
indices.push(i)
|
|
126
|
+
if (!hash) {
|
|
127
|
+
return null
|
|
60
128
|
}
|
|
61
|
-
}
|
|
62
129
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
130
|
+
let resolvers
|
|
131
|
+
let indices
|
|
132
|
+
|
|
133
|
+
const keys = Object.keys(obj)
|
|
134
|
+
|
|
135
|
+
for (let i = 0; i < keys.length; i++) {
|
|
136
|
+
const resolver = compileTemplate(obj[keys[i]])
|
|
137
|
+
if (resolver) {
|
|
138
|
+
resolvers ??= []
|
|
139
|
+
resolvers.push(resolver)
|
|
140
|
+
indices ??= []
|
|
141
|
+
indices.push(keys[i])
|
|
142
|
+
}
|
|
143
|
+
}
|
|
76
144
|
|
|
77
|
-
|
|
145
|
+
return resolvers
|
|
146
|
+
? (args$) =>
|
|
147
|
+
rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
|
|
148
|
+
rx.map((values) => {
|
|
149
|
+
const ret = { ...obj }
|
|
150
|
+
for (let n = 0; n < values.length; n++) {
|
|
151
|
+
ret[indices[n]] = values[n]
|
|
152
|
+
}
|
|
153
|
+
return ret
|
|
154
|
+
})
|
|
155
|
+
)
|
|
156
|
+
: null
|
|
157
|
+
},
|
|
158
|
+
(obj, hash) => hash ?? hashTemplate(obj)
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
const compileObjectTemplate = (obj) => {
|
|
78
162
|
if (!fp.isPlainObject(obj)) {
|
|
79
163
|
throw new Error('invalid argument')
|
|
80
164
|
}
|
|
81
165
|
|
|
82
|
-
|
|
83
|
-
|
|
166
|
+
const hash = hashTemplate(obj)
|
|
167
|
+
return hash ? _compileObjectTemplate(obj, hash) : null
|
|
168
|
+
}
|
|
84
169
|
|
|
85
|
-
|
|
170
|
+
const _compileStringTemplate = weakCache(
|
|
171
|
+
(str, hash) => {
|
|
172
|
+
if (!fp.isString(str)) {
|
|
173
|
+
throw new Error('invalid argument')
|
|
174
|
+
}
|
|
86
175
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (resolver) {
|
|
90
|
-
resolvers ??= []
|
|
91
|
-
resolvers.push(resolver)
|
|
92
|
-
indices ??= []
|
|
93
|
-
indices.push(keys[i])
|
|
176
|
+
if (!hash) {
|
|
177
|
+
return null
|
|
94
178
|
}
|
|
95
|
-
}
|
|
96
179
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const ret = { ...obj }
|
|
102
|
-
for (let n = 0; n < values.length; n++) {
|
|
103
|
-
ret[indices[n]] = values[n]
|
|
104
|
-
}
|
|
105
|
-
return ret
|
|
106
|
-
})
|
|
107
|
-
)
|
|
108
|
-
: null
|
|
109
|
-
}
|
|
180
|
+
const match = inner(str)
|
|
181
|
+
if (!match) {
|
|
182
|
+
return null
|
|
183
|
+
}
|
|
110
184
|
|
|
111
|
-
|
|
112
|
-
if (!fp.isString(str)) {
|
|
113
|
-
throw new Error('invalid argument')
|
|
114
|
-
}
|
|
185
|
+
const { pre, type, body, post } = match
|
|
115
186
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
187
|
+
const compileExpression = compilers[type]
|
|
188
|
+
if (!compileExpression) {
|
|
189
|
+
throw new Error('unknown expression type: ' + type)
|
|
190
|
+
}
|
|
120
191
|
|
|
121
|
-
|
|
192
|
+
const expr = compileExpression(body)
|
|
122
193
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
194
|
+
if (!pre && !post) {
|
|
195
|
+
return (args$) => expr(args$)
|
|
196
|
+
}
|
|
127
197
|
|
|
128
|
-
|
|
198
|
+
return (args$) =>
|
|
199
|
+
expr(args$).pipe(rx.map((body) => `${pre}${stringify(body, type !== 'js')}${post}`))
|
|
200
|
+
},
|
|
201
|
+
(str, hash) => hash ?? hashTemplate(str)
|
|
202
|
+
)
|
|
129
203
|
|
|
130
|
-
|
|
131
|
-
|
|
204
|
+
const compileStringTemplate = (obj) => {
|
|
205
|
+
if (!fp.isString(obj)) {
|
|
206
|
+
throw new Error('invalid argument')
|
|
132
207
|
}
|
|
133
208
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
rx.switchMap((body) => {
|
|
137
|
-
const str = `${pre}${stringify(body, type !== 'js')}${post}`
|
|
138
|
-
return compileStringTemplate(str)?.(args$) ?? rxjs.of(str)
|
|
139
|
-
})
|
|
140
|
-
)
|
|
209
|
+
const hash = hashTemplate(obj)
|
|
210
|
+
return hash ? _compileStringTemplate(obj, hash) : null
|
|
141
211
|
}
|
|
142
212
|
|
|
143
213
|
function stringify(value, escape) {
|
|
@@ -155,35 +225,7 @@ module.exports = (options) => {
|
|
|
155
225
|
return typeof val === 'string' && val.indexOf('{{') !== -1
|
|
156
226
|
}
|
|
157
227
|
|
|
158
|
-
function
|
|
159
|
-
if (fp.isPlainObject(template)) {
|
|
160
|
-
let hashes
|
|
161
|
-
for (const key of Object.keys(template)) {
|
|
162
|
-
const hash = hashTemplate(template[key], key)
|
|
163
|
-
if (hash) {
|
|
164
|
-
hashes ??= []
|
|
165
|
-
hashes.push(hash)
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return hashes ? objectHash([prefix, hashes]) : ''
|
|
169
|
-
} else if (fp.isArray(template)) {
|
|
170
|
-
let hashes
|
|
171
|
-
for (let idx = 0; idx < template.length; idx++) {
|
|
172
|
-
const hash = hashTemplate(template[idx], idx)
|
|
173
|
-
if (hash) {
|
|
174
|
-
hashes ??= []
|
|
175
|
-
hashes.push(hash)
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return hashes ? objectHash([prefix, hashes]) : ''
|
|
179
|
-
} else if (isTemplate(template)) {
|
|
180
|
-
return objectHash([prefix, template])
|
|
181
|
-
} else {
|
|
182
|
-
return ''
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function _compileTemplate(template) {
|
|
228
|
+
function compileTemplate(template) {
|
|
187
229
|
if (fp.isPlainObject(template)) {
|
|
188
230
|
return compileObjectTemplate(template)
|
|
189
231
|
} else if (fp.isArray(template)) {
|
|
@@ -195,11 +237,6 @@ module.exports = (options) => {
|
|
|
195
237
|
}
|
|
196
238
|
}
|
|
197
239
|
|
|
198
|
-
const compileTemplate = weakCache(
|
|
199
|
-
(template) => _compileTemplate(template),
|
|
200
|
-
(template) => hashTemplate(template)
|
|
201
|
-
)
|
|
202
|
-
|
|
203
240
|
async function resolveTemplate(template, args$) {
|
|
204
241
|
return rxjs.firstValueFrom(onResolveTemplate(template, args$))
|
|
205
242
|
}
|