theprogrammablemind 9.8.0-beta.9 → 9.9.0-beta.0

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/src/project2.js CHANGED
@@ -1,18 +1,6 @@
1
1
  const debug = require('./debug')
2
2
  const helpers = require('./helpers')
3
3
 
4
- function areFirstNEqual(arr1, arr2, n) {
5
- if (n <= 0) return true;
6
- if (arr1.length < n || arr2.length < n) return false;
7
-
8
- for (let i = 0; i < n; i++) {
9
- if (arr1[i] !== arr2[i]) {
10
- return false;
11
- }
12
- }
13
- return true;
14
- }
15
-
16
4
  function project(source, filters, path=[]) {
17
5
  if (['boolean', 'string', 'number'].includes(typeof source)) {
18
6
  return source
@@ -49,12 +37,16 @@ function project(source, filters, path=[]) {
49
37
  let properties = []
50
38
  for (const filter of selectedFilters) {
51
39
  for (const property of filter.apply(source)) {
52
- properties.push(property)
40
+ if (!properties.includes(property)) {
41
+ properties.push(property)
42
+ }
53
43
  }
54
44
  }
55
45
  if (source?.checks) {
56
46
  for (const check of source.checks) {
57
- properties.push(check)
47
+ if (!properties.includes(check)) {
48
+ properties.push(check)
49
+ }
58
50
  }
59
51
  }
60
52
 
@@ -64,10 +56,14 @@ function project(source, filters, path=[]) {
64
56
  // property that contains a list of properties to be checked
65
57
  if (property.properties) {
66
58
  for (const moreProperty of source[property.properties] || []) {
67
- updatedProperties.push(moreProperty)
59
+ if (!updatedProperties.includes(property)) {
60
+ updatedProperties.push(moreProperty)
61
+ }
68
62
  }
69
63
  } else {
70
- updatedProperties.push(property)
64
+ if (!updatedProperties.includes(property)) {
65
+ updatedProperties.push(property)
66
+ }
71
67
  }
72
68
  }
73
69
  properties = updatedProperties
@@ -75,7 +71,7 @@ function project(source, filters, path=[]) {
75
71
  // Build the result object
76
72
  const result = {};
77
73
  properties.forEach(prop => {
78
- if (prop.path && (prop.path.length === path.length + 1) && areFirstNEqual(path, prop.path, path.length) && source.hasOwnProperty(prop.path[path.length])) {
74
+ if (prop.path && (prop.path.length === path.length + 1) && helpers.pathEquals(path, prop.path, path.length) && source.hasOwnProperty(prop.path[path.length])) {
79
75
  const endProp = prop.path[path.length]
80
76
  if (['boolean', 'string', 'number'].includes(typeof source[endProp])) {
81
77
  result[endProp] = source[endProp]
@@ -109,8 +105,18 @@ function project(source, filters, path=[]) {
109
105
  const instantiatedCheck = []
110
106
  for (const check of prop.check) {
111
107
  if (typeof check.property == 'function') {
112
- for (const sourceKey of source[prop.property].keys()) {
108
+ const keys = (value) => {
109
+ if (Array.isArray(value)) {
110
+ return value.keys()
111
+ } else {
112
+ return Object.keys(value)
113
+ }
114
+ }
115
+ for (const sourceKey of keys(source[prop.property])) {
113
116
  if (check.property(sourceKey)) {
117
+ if (!check.check) {
118
+ throw new Error(`For the property check of the property '${check.property.toString()}', the 'check' (singular) property is missing.`)
119
+ }
114
120
  instantiatedCheck.push({ property: sourceKey, check: check.check })
115
121
  }
116
122
  }
package/src/semantics.js CHANGED
@@ -226,6 +226,7 @@ class Semantics {
226
226
  }
227
227
  contextPrime = await semantic.apply(args, context, s, options)
228
228
  if (continueWasCalled) {
229
+ context = contextPrime
229
230
  continue
230
231
  }
231
232
  if (deferWasCalled) {
@@ -259,14 +260,14 @@ class Semantics {
259
260
  const source = `${semantic.km}/#${semantic.index}`
260
261
  lines.setElement(0, 2, `ERROR while applying (${source}) ${semantic.toLabel()}`)
261
262
  lines.newRow()
262
- lines.setElement(0, 2, semantic.toString())
263
- lines.newRow()
264
263
  lines.setElement(0, 1, 'DEBUG')
265
264
  lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_BREAKAT="${args.calls.current()}"`)
266
265
  lines.newRow()
267
266
  lines.setElement(0, 1, 'STACK')
268
267
  lines.setElement(0, 2, stack)
269
268
  lines.newRow()
269
+ lines.setElement(0, 2, semantic.toString())
270
+ lines.newRow()
270
271
  lines.setElement(0, 1, 'TO')
271
272
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
272
273
  lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
@@ -277,6 +278,9 @@ class Semantics {
277
278
  const message = `ERROR while applying (${source}) ${semantic.toLabel()}\n to\n ${JSON.stringify(context, null, 2)}.\n${errorMessage}'`
278
279
  // this.logs.push(message)
279
280
  // return [message]
281
+ if (e.reason) {
282
+ throw e
283
+ }
280
284
  args.calls.pop()
281
285
  throw { error: [message], logs: this.logs, reason: e.reason }
282
286
  }
@@ -295,14 +299,14 @@ class Semantics {
295
299
  lines.setElement(0, 1, 'APPLIED')
296
300
  lines.setElement(0, 2, semantic.toLabel())
297
301
  lines.newRow()
298
- lines.setElement(0, 2, semantic.toString())
299
- lines.newRow()
300
302
  lines.setElement(0, 1, 'DEBUG')
301
303
  lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_BREAKAT="${args.calls.current()}"`)
302
304
  lines.newRow()
303
305
  lines.setElement(0, 1, 'STACK')
304
306
  lines.setElement(0, 2, stack)
305
307
  lines.newRow()
308
+ lines.setElement(0, 2, semantic.toString())
309
+ lines.newRow()
306
310
  lines.setElement(0, 1, 'TO')
307
311
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
308
312
  lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
@@ -332,7 +336,6 @@ class Semantics {
332
336
  for (const f of finallyList) {
333
337
  await f()
334
338
  }
335
- args.calls.pop()
336
339
  if (!applied && debug) {
337
340
  const widths = Lines.addRemainder([10, 10])
338
341
  const lines = new Lines(widths)
@@ -340,6 +343,9 @@ class Semantics {
340
343
  lines.setElement(0, 1, `#${context.context_id || "?"}`)
341
344
  lines.setElement(0, 2, 'No semantic applied')
342
345
  lines.newRow()
346
+ lines.setElement(0, 1, 'DEBUG')
347
+ lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_BREAKAT="${args.calls.current()}"`)
348
+ lines.newRow()
343
349
  lines.setElement(0, 1, 'STACK')
344
350
  lines.setElement(0, 2, stack)
345
351
  lines.newRow()
@@ -348,6 +354,7 @@ class Semantics {
348
354
  lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
349
355
  this.logs.push(lines.toString())
350
356
  }
357
+ args.calls.pop()
351
358
  return contextPrime
352
359
  }
353
360