ks-fwork 2.0.1 → 3.0.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/dist/ks-fwork.js +725 -0
- package/package.json +16 -4
- package/src/app.js +12 -27
- package/src/component.js +149 -0
- package/src/destroy-dom.js +5 -0
- package/src/events.js +13 -8
- package/src/h.js +6 -2
- package/src/index.js +1 -0
- package/src/mount-dom.js +32 -14
- package/src/nodes-equal.js +13 -1
- package/src/patch-dom.js +48 -15
- package/src/utils/arrays.js +51 -50
- package/src/utils/objects.js +4 -0
- package/src/utils/props.js +6 -0
package/src/utils/arrays.js
CHANGED
|
@@ -40,81 +40,51 @@ class ArrayWithOriginalIndices {
|
|
|
40
40
|
return this.#originalIndices[index];
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (index >= this.length) { return false; }
|
|
47
|
-
|
|
48
|
-
const item = this.#oldArray[index];
|
|
49
|
-
const indexInNewArray = newArray.findIndex((newArrayItem) => this.#equalsFn(item, newArrayItem))
|
|
50
|
-
|
|
51
|
-
return indexInNewArray === -1;
|
|
43
|
+
// ADD
|
|
44
|
+
isAddition(item, startIndex) {
|
|
45
|
+
return this.findIndexFrom(item, startIndex) === -1;
|
|
52
46
|
}
|
|
53
47
|
|
|
54
|
-
|
|
48
|
+
addItem(item, index) {
|
|
55
49
|
const operation = {
|
|
56
|
-
op: ARRAY_DIFF_OP.
|
|
50
|
+
op: ARRAY_DIFF_OP.ADD,
|
|
57
51
|
index,
|
|
58
|
-
item
|
|
52
|
+
item
|
|
59
53
|
}
|
|
60
54
|
|
|
61
|
-
this.#oldArray.splice(index,
|
|
62
|
-
this.#originalIndices.splice(index, 1);
|
|
55
|
+
this.#oldArray.splice(index, 0, item);
|
|
56
|
+
this.#originalIndices.splice(index, 0, -1);
|
|
63
57
|
|
|
64
58
|
return operation;
|
|
65
59
|
}
|
|
66
60
|
|
|
67
|
-
//
|
|
68
|
-
|
|
61
|
+
// REMOVE
|
|
62
|
+
isRemoval(index, newArray) {
|
|
69
63
|
// Bail out if index is past the end of the old array.
|
|
70
|
-
if (index >= this.length) {return false; }
|
|
64
|
+
if (index >= this.length) { return false; }
|
|
71
65
|
|
|
72
66
|
const item = this.#oldArray[index];
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
return this.#equalsFn(item, newArrayItem);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
noopItem(index) {
|
|
79
|
-
return {
|
|
80
|
-
op: ARRAY_DIFF_OP.NOOP,
|
|
81
|
-
originalIndex: this.originalIndexAt(index),
|
|
82
|
-
index,
|
|
83
|
-
item: this.#oldArray[index],
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// ADD
|
|
88
|
-
isAddition(item, fromIndex) {
|
|
89
|
-
return this.findFromIndex(item, fromIndex) === -1;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
findFromIndex(item, fromIndex) {
|
|
93
|
-
for (let i = fromIndex; i < this.length; i++) {
|
|
94
|
-
if (this.#equalsFn(item, this.#oldArray[i])) {
|
|
95
|
-
return i;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
67
|
+
const indexInNewArray = newArray.findIndex((newArrayItem) => this.#equalsFn(item, newArrayItem))
|
|
98
68
|
|
|
99
|
-
return -1;
|
|
69
|
+
return indexInNewArray === -1;
|
|
100
70
|
}
|
|
101
71
|
|
|
102
|
-
|
|
72
|
+
removeItem(index) {
|
|
103
73
|
const operation = {
|
|
104
|
-
op: ARRAY_DIFF_OP.
|
|
74
|
+
op: ARRAY_DIFF_OP.REMOVE,
|
|
105
75
|
index,
|
|
106
|
-
item
|
|
76
|
+
item: this.#oldArray[index],
|
|
107
77
|
}
|
|
108
78
|
|
|
109
|
-
this.#oldArray.splice(index,
|
|
110
|
-
this.#originalIndices.splice(index,
|
|
79
|
+
this.#oldArray.splice(index, 1);
|
|
80
|
+
this.#originalIndices.splice(index, 1);
|
|
111
81
|
|
|
112
82
|
return operation;
|
|
113
83
|
}
|
|
114
84
|
|
|
115
85
|
// MOVE
|
|
116
86
|
moveItem(item, toIndex) {
|
|
117
|
-
const fromIndex = this.
|
|
87
|
+
const fromIndex = this.findIndexFrom(item, toIndex);
|
|
118
88
|
|
|
119
89
|
const operation = {
|
|
120
90
|
op: ARRAY_DIFF_OP.MOVE,
|
|
@@ -133,7 +103,27 @@ class ArrayWithOriginalIndices {
|
|
|
133
103
|
return operation;
|
|
134
104
|
}
|
|
135
105
|
|
|
136
|
-
//
|
|
106
|
+
// NOOP
|
|
107
|
+
isNoop(index, newArray) {
|
|
108
|
+
// Bail out if index is past the end of the old array.
|
|
109
|
+
if (index >= this.length) {return false; }
|
|
110
|
+
|
|
111
|
+
const item = this.#oldArray[index];
|
|
112
|
+
const newArrayItem = newArray[index];
|
|
113
|
+
|
|
114
|
+
return this.#equalsFn(item, newArrayItem);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
noopItem(index) {
|
|
118
|
+
return {
|
|
119
|
+
op: ARRAY_DIFF_OP.NOOP,
|
|
120
|
+
originalIndex: this.originalIndexAt(index),
|
|
121
|
+
index,
|
|
122
|
+
item: this.#oldArray[index],
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Remove all items after specified index
|
|
137
127
|
removeItemsAfter(index) {
|
|
138
128
|
const operations = [];
|
|
139
129
|
|
|
@@ -143,6 +133,17 @@ class ArrayWithOriginalIndices {
|
|
|
143
133
|
|
|
144
134
|
return operations;
|
|
145
135
|
}
|
|
136
|
+
|
|
137
|
+
// Return the first index at or after startIndex where the item matches, or -1 if item is not found.
|
|
138
|
+
findIndexFrom(item, startIndex) {
|
|
139
|
+
for (let i = startIndex; i < this.length; i++) {
|
|
140
|
+
if (this.#equalsFn(item, this.#oldArray[i])) {
|
|
141
|
+
return i;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return -1;
|
|
146
|
+
}
|
|
146
147
|
}
|
|
147
148
|
|
|
148
149
|
export function arraysDiffSequence(oldArray, newArray, equalsFn = (a, b) => a === b) {
|
package/src/utils/objects.js
CHANGED