@nxtedition/lib 14.0.10 → 14.0.12

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "14.0.10",
3
+ "version": "14.0.12",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "files": [
@@ -70,144 +70,99 @@ module.exports = (options) => {
70
70
  }
71
71
  }
72
72
 
73
- const _compileArrayTemplate = weakCache(
74
- (arr, hash) => {
75
- if (!fp.isArray(arr)) {
76
- throw new Error('invalid argument')
73
+ function compileArrayTemplate(template) {
74
+ let resolvers
75
+ let indices
76
+
77
+ for (let i = 0; i < template.length; i++) {
78
+ const resolver = compileTemplate(template[i])
79
+ if (resolver) {
80
+ resolvers ??= []
81
+ resolvers.push(resolver)
82
+ indices ??= []
83
+ indices.push(i)
77
84
  }
85
+ }
78
86
 
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)
87
+ return resolvers
88
+ ? (arr, args$) => {
89
+ const len = resolvers.length
90
+ const values = new Array(len)
91
+ for (let n = 0; n < len; n++) {
92
+ values[n] = resolvers[n](arr[indices[n]], args$)
93
+ }
94
+
95
+ return rxjs.combineLatest(values).pipe(
96
+ rx.map((values) => {
97
+ const ret = [...arr]
98
+ for (let n = 0; n < values.length; n++) {
99
+ ret[indices[n]] = values[n]
100
+ }
101
+ return ret
102
+ })
103
+ )
93
104
  }
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) => {
113
- if (!fp.isArray(arr)) {
114
- throw new Error('invalid argument')
115
- }
116
- const hash = hashTemplate(arr)
117
- return hash ? _compileArrayTemplate(arr, hash) : null
105
+ : null
118
106
  }
119
107
 
120
- const _compileObjectTemplate = weakCache(
121
- (obj, hash) => {
122
- if (!fp.isPlainObject(obj)) {
123
- throw new Error('invalid argument')
124
- }
108
+ function compileObjectTemplate(template) {
109
+ let resolvers
110
+ let indices
125
111
 
126
- if (!hash) {
127
- return null
128
- }
129
-
130
- let resolvers
131
- let indices
132
-
133
- const keys = Object.keys(obj)
112
+ const keys = Object.keys(template)
134
113
 
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
- }
114
+ for (let i = 0; i < keys.length; i++) {
115
+ const resolver = compileTemplate(template[keys[i]])
116
+ if (resolver) {
117
+ resolvers ??= []
118
+ resolvers.push(resolver)
119
+ indices ??= []
120
+ indices.push(keys[i])
143
121
  }
144
-
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) => {
162
- if (!fp.isPlainObject(obj)) {
163
- throw new Error('invalid argument')
164
122
  }
165
123
 
166
- const hash = hashTemplate(obj)
167
- return hash ? _compileObjectTemplate(obj, hash) : null
124
+ return resolvers
125
+ ? (obj, args$) => {
126
+ const len = resolvers.length
127
+ const values = new Array(len)
128
+ for (let n = 0; n < len; n++) {
129
+ values[n] = resolvers[n](obj[indices[n]], args$)
130
+ }
131
+
132
+ return rxjs.combineLatest(values).pipe(
133
+ rx.map((values) => {
134
+ const ret = { ...obj }
135
+ for (let n = 0; n < values.length; n++) {
136
+ ret[indices[n]] = values[n]
137
+ }
138
+ return ret
139
+ })
140
+ )
141
+ }
142
+ : null
168
143
  }
169
144
 
170
- const _compileStringTemplate = weakCache(
171
- (str, hash) => {
172
- if (!fp.isString(str)) {
173
- throw new Error('invalid argument')
174
- }
175
-
176
- if (!hash) {
177
- return null
178
- }
179
-
180
- const match = inner(str)
181
- if (!match) {
182
- return null
183
- }
184
-
185
- const { pre, type, body, post } = match
186
-
187
- const compileExpression = compilers[type]
188
- if (!compileExpression) {
189
- throw new Error('unknown expression type: ' + type)
190
- }
145
+ function compileStringTemplate(template) {
146
+ const match = inner(template)
147
+ if (!match) {
148
+ return null
149
+ }
191
150
 
192
- const expr = compileExpression(body)
151
+ const { pre, type, body, post } = match
193
152
 
194
- if (!pre && !post) {
195
- return (args$) => expr(args$)
196
- }
153
+ const compileExpression = compilers[type]
154
+ if (!compileExpression) {
155
+ throw new Error('unknown expression type: ' + type)
156
+ }
197
157
 
198
- return (args$) =>
199
- expr(args$).pipe(rx.map((body) => `${pre}${stringify(body, type !== 'js')}${post}`))
200
- },
201
- (str, hash) => hash ?? hashTemplate(str)
202
- )
158
+ const expr = compileExpression(body)
203
159
 
204
- const compileStringTemplate = (obj) => {
205
- if (!fp.isString(obj)) {
206
- throw new Error('invalid argument')
160
+ if (!pre && !post) {
161
+ return (str, args$) => expr(args$)
207
162
  }
208
163
 
209
- const hash = hashTemplate(obj)
210
- return hash ? _compileStringTemplate(obj, hash) : null
164
+ return (str, args$) =>
165
+ expr(args$).pipe(rx.map((body) => `${pre}${stringify(body, type !== 'js')}${post}`))
211
166
  }
212
167
 
213
168
  function stringify(value, escape) {
@@ -225,16 +180,24 @@ module.exports = (options) => {
225
180
  return typeof val === 'string' && val.indexOf('{{') !== -1
226
181
  }
227
182
 
183
+ const compileTemplateCache = weakCache(
184
+ (template) => {
185
+ if (fp.isPlainObject(template)) {
186
+ return compileObjectTemplate(template)
187
+ } else if (fp.isArray(template)) {
188
+ return compileArrayTemplate(template)
189
+ } else if (isTemplate(template)) {
190
+ return compileStringTemplate(template)
191
+ } else {
192
+ return null
193
+ }
194
+ },
195
+ (template, hash) => hash ?? hashTemplate(template)
196
+ )
197
+
228
198
  function compileTemplate(template) {
229
- if (fp.isPlainObject(template)) {
230
- return compileObjectTemplate(template)
231
- } else if (fp.isArray(template)) {
232
- return compileArrayTemplate(template)
233
- } else if (isTemplate(template)) {
234
- return compileStringTemplate(template)
235
- } else {
236
- return null
237
- }
199
+ const hash = hashTemplate(template)
200
+ return hash ? compileTemplateCache(template, hash) : null
238
201
  }
239
202
 
240
203
  async function resolveTemplate(template, args$) {
@@ -243,7 +206,7 @@ module.exports = (options) => {
243
206
 
244
207
  function onResolveTemplate(template, args$) {
245
208
  try {
246
- return compileTemplate(template)?.(args$) ?? rxjs.of(template)
209
+ return compileTemplate(template)?.(template, args$) ?? rxjs.of(template)
247
210
  } catch (err) {
248
211
  return rxjs.throwError(() => err)
249
212
  }