solarite 0.2.4 → 0.3.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.
Files changed (38) hide show
  1. package/dist/Solarite-debug.js +1457 -1402
  2. package/dist/Solarite.js +1425 -1272
  3. package/dist/Solarite.min.js +2 -2
  4. package/package.json +5 -6
  5. package/readme.md +2 -4
  6. package/src/{solarite/ExprPath.js → ExprPath.js} +421 -231
  7. package/src/Globals.js +79 -0
  8. package/src/HtmlParser.js +91 -0
  9. package/src/{util/MultiValueMap.js → MultiValueMap.js} +22 -26
  10. package/src/{solarite/NodeGroup.js → NodeGroup.js} +286 -226
  11. package/src/{solarite/Shell.js → Shell.js} +119 -92
  12. package/src/Solarite.d.ts +62 -0
  13. package/src/{solarite/Solarite.js → Solarite.js} +15 -13
  14. package/src/{solarite/Template.js → Template.js} +22 -19
  15. package/src/Util.js +330 -0
  16. package/src/{util/Errors.js → assert.js} +1 -0
  17. package/src/createSolarite.js +154 -0
  18. package/src/{util/delve.js → delve.js} +5 -4
  19. package/src/{solarite/getArg.js → getArg.js} +41 -15
  20. package/src/{solarite/r.js → h.js} +59 -29
  21. package/src/{solarite/hash.js → hash.js} +12 -9
  22. package/src/unused/FastLookupArray.js +54 -0
  23. package/src/unused/Hashes.js +339 -0
  24. package/src/unused/InUse.test.js +92 -0
  25. package/src/unused/InUseMap.js +98 -0
  26. package/src/unused/LinkedList.js +117 -0
  27. package/src/unused/LinkedList.test.js +115 -0
  28. package/src/unused/Misc.js +13 -0
  29. package/src/unused/Perf.js +47 -0
  30. package/src/unused/TrackedArray.js +54 -0
  31. package/src/watch.js +546 -0
  32. package/src/solarite/Globals.js +0 -54
  33. package/src/solarite/Util.js +0 -388
  34. package/src/solarite/createSolarite.js +0 -274
  35. package/src/solarite/watch3.js +0 -189
  36. package/src/util/Util.js +0 -113
  37. /package/src/{solarite/udomdiff.js → udomdiff.js} +0 -0
  38. /package/src/{util → unused}/WeakArray.js +0 -0
@@ -0,0 +1,98 @@
1
+
2
+
3
+
4
+ /**
5
+ * An array that keeps track of how many elements are in-use.
6
+ * In-use elements are always at the beginning of the array. */
7
+ export class InUseArray extends Array {
8
+
9
+ /**
10
+ * @type {int} Everything before this number is in-use. */
11
+ count = 0;
12
+
13
+
14
+ /**
15
+ * Get a value and mark it as in-use.
16
+ * @param val {undefined|*} Optional. If set, find this specific value. Otherwise find any value.
17
+ * @return {undefined|*} The value that is now marked as in-use. */
18
+ use(val=undefined) {
19
+ if (val) {
20
+ // If val doesn't exist or is already in-use, return undefined.
21
+ let index = this.indexOf(val);
22
+ if (index === -1 || index < this.count)
23
+ return undefined;
24
+
25
+ // Swap the first available with val.
26
+ this[index] = this[this.count];
27
+ this[this.count] = val;
28
+ this.count++;
29
+ return val;
30
+ }
31
+
32
+ // Try to get last available.
33
+ else {
34
+ if (this.count >= this.length)
35
+ return undefined;
36
+ let result = this[this.count];
37
+ this.count++;
38
+ return result;
39
+ }
40
+ }
41
+
42
+ free() {
43
+ this.count = 0;
44
+ }
45
+
46
+ reset() {
47
+ this.length = 0;
48
+ }
49
+ }
50
+
51
+ /**
52
+ *
53
+ */
54
+ export class InUseMap {
55
+
56
+ /** @type {Object<string, InUseArray>} */
57
+ data = {};
58
+
59
+ // Set a new value for a key
60
+ add(key, value) {
61
+ let data = this.data;
62
+ let array = data[key]
63
+ if (!array) {
64
+ array = new InUseArray();
65
+ data[key] = array;
66
+ }
67
+ array.push(value);
68
+ }
69
+
70
+ // Get all values for a key
71
+ getAll(key) {
72
+ return this.data[key] || [];
73
+ }
74
+
75
+ // Find a value matching the given key, mark it as inUse, and return it.
76
+ use(key, val=undefined) {
77
+ return this.data[key]?.use(val) || undefined
78
+ }
79
+
80
+ // temporary
81
+ delete(key, val=undefined) {
82
+ return this.data[key]?.use(val) || undefined
83
+ }
84
+
85
+ freeAll() {
86
+ for (let key in this.data)
87
+ this.data[key].free();
88
+ }
89
+
90
+ hasValue(val) {
91
+ let data = this.data;
92
+ let names = [];
93
+ for (let name in data)
94
+ if (data[name].includes(val))
95
+ names.push(name)
96
+ return names;
97
+ }
98
+ }
@@ -0,0 +1,117 @@
1
+ export default class LinkedList {
2
+
3
+ constructor(input) {
4
+ this.head = null;
5
+ this.tail = null;
6
+ this.map = new Map();
7
+
8
+ if (Array.isArray(input)) {
9
+ input.forEach(val => this.push(val));
10
+ } else if (input instanceof LinkedList) {
11
+ for (let val of input) {
12
+ this.push(val);
13
+ }
14
+ }
15
+ }
16
+
17
+ push(value) {
18
+ if (!this.head) {
19
+ this.head = value;
20
+ this.tail = value;
21
+ return;
22
+ }
23
+
24
+ this.map.set(value, { prev: this.tail, next: null });
25
+ this.map.set(this.tail, { ...this.map.get(this.tail), next: value });
26
+ this.tail = value;
27
+ }
28
+
29
+ unshift(value) {
30
+ if (!this.head) {
31
+ this.push(value);
32
+ return;
33
+ }
34
+
35
+ this.map.set(value, { prev: null, next: this.head });
36
+ this.map.set(this.head, { ...this.map.get(this.head), prev: value });
37
+ this.head = value;
38
+ }
39
+
40
+ shift() {
41
+ if (!this.head) return;
42
+
43
+ const secondNode = this.map.get(this.head).next;
44
+
45
+ if (secondNode) {
46
+ this.map.set(secondNode, { ...this.map.get(secondNode), prev: null });
47
+ } else {
48
+ this.tail = null;
49
+ }
50
+
51
+ this.head = secondNode;
52
+ }
53
+
54
+ pop() {
55
+ if (!this.tail) return;
56
+
57
+ const penultimate = this.map.get(this.tail).prev;
58
+
59
+ if (penultimate) {
60
+ this.map.set(penultimate, { ...this.map.get(penultimate), next: null });
61
+ } else {
62
+ this.head = null;
63
+ }
64
+
65
+ this.tail = penultimate;
66
+ }
67
+
68
+ *[Symbol.iterator]() {
69
+ let currentNode = this.head;
70
+
71
+ while (currentNode) {
72
+ yield currentNode;
73
+ const mapData = this.map.get(currentNode);
74
+ currentNode = mapData ? mapData.next : null;
75
+ }
76
+ }
77
+
78
+ slice(start, end) {
79
+ const newList = new LinkedList();
80
+ let currentNode = start;
81
+
82
+ while (currentNode && currentNode !== end) {
83
+ newList.push(currentNode);
84
+ currentNode = this.map.get(currentNode).next;
85
+ }
86
+ if (end) {
87
+ newList.push(end);
88
+ }
89
+
90
+ return newList;
91
+ }
92
+
93
+
94
+ splice(start, end, newList) {
95
+ // TODO: assert items in newList aren't already in our list.
96
+
97
+ const firstNewNode = newList instanceof LinkedList ? newList.head : newList[0];
98
+ const lastNewNode = newList instanceof LinkedList ? newList.tail : newList[newList.length - 1];
99
+
100
+ if (firstNewNode) {
101
+ this.map.set(start, { ...this.map.get(start), next: firstNewNode });
102
+ this.map.set(firstNewNode, { ...this.map.get(firstNewNode), prev: start });
103
+ }
104
+
105
+ const afterEnd = end ? this.map.get(end).next : null;
106
+ if (lastNewNode) {
107
+ this.map.set(lastNewNode, { ...this.map.get(lastNewNode), next: afterEnd });
108
+ if (afterEnd) {
109
+ this.map.set(afterEnd, { ...this.map.get(afterEnd), prev: lastNewNode });
110
+ } else {
111
+ this.tail = lastNewNode;
112
+ }
113
+ } else if (afterEnd) {
114
+ this.map.set(afterEnd, { ...this.map.get(afterEnd), prev: start });
115
+ }
116
+ }
117
+ }
@@ -0,0 +1,115 @@
1
+ import Testimony, {assert} from "../../tests/Testimony.js";
2
+ import LinkedList from "./LinkedList.js";
3
+
4
+ Testimony.test('LinkedList.constructor with Array input', () => {
5
+ const linkedList = new LinkedList(['A', 'B', 'C']);
6
+ assert.eq([...linkedList], ['A', 'B', 'C']);
7
+ });
8
+
9
+ Testimony.test('LinkedList.constructor with LinkedList input', () => {
10
+ const originalList = new LinkedList(['X', 'Y', 'Z']);
11
+ const copiedList = new LinkedList(originalList);
12
+ assert.eq([...copiedList], ['X', 'Y', 'Z']);
13
+ });
14
+
15
+ Testimony.test('LinkedList.constructor with mixed input', () => {
16
+ const originalList = new LinkedList(['D', 'E']);
17
+ const extendedList = new LinkedList([...originalList, 'F', 'G']);
18
+ assert.eq([...extendedList], ['D', 'E', 'F', 'G']);
19
+ });
20
+
21
+ Testimony.test('LinkedList.constructor with no input', () => {
22
+ const emptyList = new LinkedList();
23
+ assert.eq([...emptyList], []);
24
+ });
25
+
26
+
27
+ Testimony.test('LinkedList.push', () => {
28
+ const linkedList = new LinkedList();
29
+ linkedList.push("A");
30
+ linkedList.push("B");
31
+ assert.eq([...linkedList], ['A', 'B']);
32
+ });
33
+
34
+ Testimony.test('LinkedList.unshift', () => {
35
+ const linkedList = new LinkedList();
36
+ linkedList.unshift("B");
37
+ linkedList.unshift("A");
38
+ assert.eq([...linkedList], ['A', 'B']);
39
+ });
40
+
41
+ Testimony.test('LinkedList.shift', () => {
42
+ const linkedList = new LinkedList();
43
+ linkedList.push("A");
44
+ linkedList.push("B");
45
+ linkedList.shift();
46
+ assert.eq([...linkedList], ['B']);
47
+ linkedList.shift();
48
+ assert.eq([...linkedList], []);
49
+ });
50
+
51
+ Testimony.test('LinkedList.pop', () => {
52
+ const linkedList = new LinkedList();
53
+ linkedList.push("A");
54
+ linkedList.push("B");
55
+ linkedList.pop();
56
+ assert.eq([...linkedList], ['A']);
57
+ linkedList.pop();
58
+ assert.eq([...linkedList], []);
59
+ });
60
+
61
+ Testimony.test('LinkedList.slice', () => {
62
+ const linkedList = new LinkedList();
63
+ linkedList.push("A");
64
+ linkedList.push("B");
65
+ linkedList.push("C");
66
+ const subList = linkedList.slice("A", "B");
67
+ assert.eq([...subList], ['A', 'B']);
68
+ });
69
+
70
+ Testimony.test('LinkedList._splice', () => {
71
+ const linkedList = new LinkedList();
72
+ linkedList.push("A");
73
+ linkedList.push("B");
74
+ linkedList.splice("A", null, ["X", "Y"]);
75
+ assert.eq([...linkedList], ['A', 'X', 'Y', 'B']);
76
+ linkedList.splice("X", "Y");
77
+ assert.eq([...linkedList], ['B']);
78
+ });
79
+
80
+
81
+
82
+
83
+ function buildData(count = rowCount) {
84
+ function _random(max) {
85
+ return Math.round(Math.random()*1000)%max;
86
+ }
87
+
88
+ var adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"];
89
+ var colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
90
+ var nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
91
+ var data = [];
92
+ for (let i=0; i<count; i++)
93
+ data.push({id: i, label: adjectives[_random(adjectives.length)] + " " + colours[_random(colours.length)] + " " + nouns[_random(nouns.length)] });
94
+ return data;
95
+ }
96
+
97
+
98
+ Testimony.test('LinkedList.benchmark', () => {
99
+
100
+ let data = buildData(10_000)
101
+ let start = performance.now()
102
+ let list = new LinkedList(data);
103
+ console.log(performance.now() - start)
104
+
105
+ start = performance.now()
106
+ let i = 0;
107
+ for (let item in list) {
108
+ i++;
109
+ }
110
+ console.log(i)
111
+ console.log(performance.now() - start)
112
+
113
+
114
+
115
+ });
@@ -0,0 +1,13 @@
1
+ var Misc = {
2
+ weakMemoize(obj, callback) {
3
+ let result = weakMemoizeInputs.get(obj);
4
+ if (!result) {
5
+ result = callback(obj);
6
+ weakMemoizeInputs.set(obj, result);
7
+ }
8
+ return result;
9
+ }
10
+ };
11
+
12
+
13
+ let weakMemoizeInputs = new WeakMap();
@@ -0,0 +1,47 @@
1
+ var Perf = {
2
+ _timers: {},
3
+
4
+ /**
5
+ * Start a timer with a given name.
6
+ * @param timerName {string}
7
+ */
8
+ start(timerName) {
9
+ if (!Perf._timers[timerName]) {
10
+ Perf._timers[timerName] = {
11
+ startTime: null,
12
+ elapsedTime: 0
13
+ };
14
+ }
15
+ Perf._timers[timerName].startTime = performance.now();
16
+ },
17
+
18
+ /**
19
+ * Stop a timer with a given name and calculate the elapsed time.
20
+ * @param timerName {string}
21
+ */
22
+ stop(timerName) {
23
+ if (Perf._timers[timerName] && Perf._timers[timerName].startTime !== null) {
24
+ let stopTime = performance.now();
25
+ Perf._timers[timerName].elapsedTime += stopTime - Perf._timers[timerName].startTime;
26
+ Perf._timers[timerName].startTime = null; // Reset start time
27
+ }
28
+ },
29
+
30
+ /**
31
+ * Get the total time elapsed for each timer.
32
+ * @return {string[]}
33
+ */
34
+ getReport() {
35
+ let report = [];
36
+ for (let timerName in Perf._timers) {
37
+ report.push(`${timerName}: ${Perf._timers[timerName].elapsedTime.toFixed(2)} ms`);
38
+ }
39
+ return report;
40
+ },
41
+
42
+ clear() {
43
+ this._timers = {};
44
+ }
45
+ }
46
+
47
+ export default Perf;
@@ -0,0 +1,54 @@
1
+
2
+ export default class TrackedArray extends Array {
3
+ constructor(...args) {
4
+ super(...args);
5
+ this.ops = [];
6
+ }
7
+
8
+ // Intercepting 'push' as 'insert'
9
+ push(...items) {
10
+ const startIdx = this.length;
11
+ super.push(...items);
12
+ this.ops.push({ op: 'insert', index: startIdx, values: items });
13
+ return this.length;
14
+ }
15
+
16
+ // Intercepting 'pop' as 'remove'
17
+ pop() {
18
+ const removedIndex = this.length - 1;
19
+ const removedItem = super.pop();
20
+ this.ops.push({ op: 'remove', index: removedIndex, length: 1 });
21
+ return removedItem;
22
+ }
23
+
24
+ // Intercepting 'shift' as 'remove'
25
+ shift() {
26
+ const removedItem = super.shift();
27
+ this.ops.push({ op: 'remove', index: 0, length: 1 });
28
+ return removedItem;
29
+ }
30
+
31
+ // Intercepting 'unshift' as 'insert'
32
+ unshift(...items) {
33
+ super.unshift(...items);
34
+ this.ops.push({ op: 'insert', index: 0, values: items });
35
+ return this.length;
36
+ }
37
+
38
+ // Intercepting 'splice' for insert, update, or remove
39
+ splice(start, deleteCount, ...items) {
40
+ const removedItems = super.splice(start, deleteCount, ...items);
41
+
42
+ if (deleteCount > 0) {
43
+ this.ops.push({ op: 'remove', index: start, length: deleteCount });
44
+ }
45
+ if (items.length > 0) {
46
+ const operation = deleteCount > 0 ? 'update' : 'insert';
47
+ this.ops.push({ op, index: start, values: items });
48
+ }
49
+
50
+ return removedItems;
51
+ }
52
+
53
+ // TODO: reverse, sorty, copyWithin, fill
54
+ }