theprogrammablemind 9.8.0 → 9.9.0-beta.1

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/helpers.js CHANGED
@@ -22,12 +22,30 @@ class OverrideCheck {
22
22
  }
23
23
  }
24
24
 
25
- function pathEquals (p1, p2) {
25
+ function pathEquals (p1, p2, n) {
26
+ if (n !== undefined) {
27
+ if (n < 0) {
28
+ p1 = p1.slice(n)
29
+ p2 = p2.slice(n)
30
+ } else {
31
+ p1 = p1.slice(0, n)
32
+ p2 = p2.slice(0, n)
33
+ }
34
+ }
35
+
26
36
  if (p1.length !== p2.length) {
27
37
  return false
28
38
  }
29
39
  for (let i = 0; i < p1.length; ++i) {
30
- if (typeof p2[i] == 'function') {
40
+ if (p1[i].integer) {
41
+ if (!Number.isInteger(p2[i])) {
42
+ return false
43
+ }
44
+ } else if (p2[i].integer) {
45
+ if (!Number.isInteger(p1[i])) {
46
+ return false
47
+ }
48
+ } else if (typeof p2[i] == 'function') {
31
49
  if (!p2[i](p1[i])) {
32
50
  return false
33
51
  }
@@ -72,7 +90,14 @@ function where (goUp = 2) {
72
90
  let match
73
91
  for (line of lines.slice(1)) {
74
92
  // if (!(line.includes('config.js:') || line.includes('client.js:') || line.includes('<anonymous>'))) {
75
- if (!(line.includes('config.js:') || line.includes('client.js:') || line.includes('helpers.js:'))) {
93
+ const ignore = [
94
+ 'config.js:',
95
+ 'client.js:',
96
+ 'helpers.js:',
97
+ 'configHelpers.js:',
98
+ ]
99
+ // if (!(line.includes('config.js:') || line.includes('client.js:') || line.includes('helpers.js:'))) {
100
+ if(!ignore.some(p => line.includes(p))) {
76
101
  match = regexForm1.exec(line) || regexForm2.exec(line)
77
102
  if (!match) {
78
103
  continue
@@ -477,12 +502,6 @@ const functionsToStrings = (config) => {
477
502
  if (bridge.semantics) {
478
503
  bridge.semantics = bridge.semantics.toString()
479
504
  }
480
- if (bridge.evaluator) {
481
- bridge.evaluator = bridge.evaluator.toString()
482
- }
483
- if (bridge.evaluators) {
484
- bridge.evaluators = bridge.evaluators.toString()
485
- }
486
505
  return bridge
487
506
  })
488
507
  }
@@ -627,7 +646,60 @@ function setByPath(obj, path, value) {
627
646
 
628
647
  const helpDebugHierarchy = 'In order to get a debug break when a specific hierarchy is created set the DEBUG_HIERARCHY environment variable to the JSON of the child-parent pair to break on. For example DEBUG_HIERARCHY=\'["cat", "mammel"]\''
629
648
 
649
+ // removeProp.js
650
+ function removeProp(obj, testFn, { maxDepth = Infinity, seen = new WeakSet() } = {}) {
651
+ if (!obj || typeof obj !== 'object' || maxDepth <= 0) return obj;
652
+ if (seen.has(obj)) return obj;
653
+ seen.add(obj);
654
+
655
+ if (Array.isArray(obj)) {
656
+ // ---- ARRAY: process each element (but don't remove elements unless testFn says so)
657
+ let writeIdx = 0;
658
+ for (let i = 0; i < obj.length; i++) {
659
+ const element = obj[i];
660
+ const shouldRemoveElement = testFn(element, i, obj);
661
+
662
+ if (shouldRemoveElement) {
663
+ // Remove the whole array element
664
+ if (element && typeof element === 'object') {
665
+ // Still walk inside it in case testFn wants side effects
666
+ removeProp(element, testFn, { maxDepth: maxDepth - 1, seen });
667
+ }
668
+ // Skip writing it back
669
+ } else {
670
+ // Keep element, but walk into it to remove inner props
671
+ removeProp(element, testFn, { maxDepth: maxDepth - 1, seen });
672
+ if (writeIdx !== i) {
673
+ obj[writeIdx] = element;
674
+ }
675
+ writeIdx++;
676
+ }
677
+ }
678
+ obj.length = writeIdx;
679
+ return obj;
680
+ }
681
+
682
+ // ---- OBJECT: iterate over own keys
683
+ const keys = Reflect.ownKeys(obj);
684
+ for (const key of keys) {
685
+ const val = obj[key];
686
+ const shouldRemove = testFn(val, key, obj);
687
+
688
+ if (shouldRemove) {
689
+ delete obj[key];
690
+ if (val && typeof val === 'object') {
691
+ removeProp(val, testFn, { maxDepth: maxDepth - 1, seen });
692
+ }
693
+ } else {
694
+ removeProp(val, testFn, { maxDepth: maxDepth - 1, seen });
695
+ }
696
+ }
697
+
698
+ return obj;
699
+ }
700
+
630
701
  module.exports = {
702
+ removeProp,
631
703
  helpDebugHierarchy,
632
704
 
633
705
  stableId,
@@ -662,4 +734,5 @@ module.exports = {
662
734
  watchProperty,
663
735
  pathEquals,
664
736
  OverrideCheck,
737
+ normalizePath,
665
738
  }
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
@@ -78,7 +78,7 @@ class Semantic {
78
78
  return await debug.hitScoped(args.callId, async () => {
79
79
  const matches = await this.matcher(args)
80
80
  if (matches) {
81
- if ((options.debug || {}).match || args.callId === this.callId || process.env.DEBUG_CALLID == args.callId) {
81
+ if ((options.debug || {}).match || args.callId === this.callId || process.env.DEBUG_BREAKAT == args.callId) {
82
82
  debugger // eslint-disable-line no-debugger
83
83
  await this.matcher(args) // DEBUG: This is the line you step into
84
84
  }
@@ -101,7 +101,7 @@ class Semantic {
101
101
  this.fixUpArgs(args, contextPrime)
102
102
 
103
103
  return await debug.hitScoped(args.callId, async () => {
104
- if ((options.debug || {}).apply || args.callId === this.callId || process.env.DEBUG_CALLID == args.callId) {
104
+ if ((options.debug || {}).apply || args.callId === this.callId || process.env.DEBUG_BREAKAT == args.callId) {
105
105
  debugger // eslint-disable-line no-debugger // STEP ABOUT THREE TIMES TO THE DEBUG LINE
106
106
  }
107
107
  if (args.breakOnSemantics) {
@@ -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) {
@@ -255,27 +256,31 @@ class Semantics {
255
256
  const widths = Lines.addRemainder([10, 10])
256
257
  const lines = new Lines(widths)
257
258
  lines.setElement(0, 0, 'Semantic')
259
+ lines.setElement(0, 1, `#${context.context_id || "?"}`)
258
260
  const source = `${semantic.km}/#${semantic.index}`
259
261
  lines.setElement(0, 2, `ERROR while applying (${source}) ${semantic.toLabel()}`)
260
262
  lines.newRow()
261
- lines.setElement(0, 2, semantic.toString())
262
- lines.newRow()
263
263
  lines.setElement(0, 1, 'DEBUG')
264
- lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_CALLID="${args.calls.current()}"`)
264
+ lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_BREAKAT="${args.calls.current()}"`)
265
+ lines.newRow()
266
+ lines.setElement(0, 1, 'STACK')
267
+ lines.setElement(0, 2, stack)
268
+ lines.newRow()
269
+ lines.setElement(0, 2, semantic.toString())
265
270
  lines.newRow()
266
271
  lines.setElement(0, 1, 'TO')
267
272
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
268
273
  lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
269
274
  lines.newRow()
270
- lines.setElement(0, 1, 'STACK')
271
- lines.setElement(0, 2, stack)
272
- lines.newRow()
273
275
  lines.setElement(0, 1, 'ERROR')
274
276
  lines.setElement(0, 2, errorMessage)
275
277
  this.logs.push(lines.toString())
276
278
  const message = `ERROR while applying (${source}) ${semantic.toLabel()}\n to\n ${JSON.stringify(context, null, 2)}.\n${errorMessage}'`
277
279
  // this.logs.push(message)
278
280
  // return [message]
281
+ if (e.reason) {
282
+ throw e
283
+ }
279
284
  args.calls.pop()
280
285
  throw { error: [message], logs: this.logs, reason: e.reason }
281
286
  }
@@ -285,6 +290,7 @@ class Semantics {
285
290
  const widths = Lines.addRemainder([10, 10])
286
291
  const lines = new Lines(widths)
287
292
  lines.setElement(0, 0, 'Semantic')
293
+ lines.setElement(0, 1, `#${context.context_id || "?"}`)
288
294
  if (semantic.index > -1 && semantic.km) {
289
295
  // lines.setElement(0, 2, `KM '${semantic.km}' ordinal: ${semantic.index} ${ semantic.notes ? semantic.nodes : '' }`)
290
296
  lines.setElement(0, 2, semantic.toLabel())
@@ -293,18 +299,18 @@ class Semantics {
293
299
  lines.setElement(0, 1, 'APPLIED')
294
300
  lines.setElement(0, 2, semantic.toLabel())
295
301
  lines.newRow()
296
- lines.setElement(0, 2, semantic.toString())
297
- lines.newRow()
298
302
  lines.setElement(0, 1, 'DEBUG')
299
- lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_CALLID="${args.calls.current()}"`)
303
+ lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_BREAKAT="${args.calls.current()}"`)
304
+ lines.newRow()
305
+ lines.setElement(0, 1, 'STACK')
306
+ lines.setElement(0, 2, stack)
307
+ lines.newRow()
308
+ lines.setElement(0, 2, semantic.toString())
300
309
  lines.newRow()
301
310
  lines.setElement(0, 1, 'TO')
302
311
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
303
312
  lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
304
313
  lines.newRow()
305
- lines.setElement(0, 1, 'STACK')
306
- lines.setElement(0, 2, stack)
307
- lines.newRow()
308
314
  lines.setElement(0, 1, 'RESULT')
309
315
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
310
316
  lines.setElement(1, 2, JSON.stringify(contextPrime, null, 2))
@@ -330,13 +336,16 @@ class Semantics {
330
336
  for (const f of finallyList) {
331
337
  await f()
332
338
  }
333
- args.calls.pop()
334
339
  if (!applied && debug) {
335
340
  const widths = Lines.addRemainder([10, 10])
336
341
  const lines = new Lines(widths)
337
342
  lines.setElement(0, 0, 'Semantic')
343
+ lines.setElement(0, 1, `#${context.context_id || "?"}`)
338
344
  lines.setElement(0, 2, 'No semantic applied')
339
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()
340
349
  lines.setElement(0, 1, 'STACK')
341
350
  lines.setElement(0, 2, stack)
342
351
  lines.newRow()
@@ -345,6 +354,7 @@ class Semantics {
345
354
  lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
346
355
  this.logs.push(lines.toString())
347
356
  }
357
+ args.calls.pop()
348
358
  return contextPrime
349
359
  }
350
360