Skip to content

Commit c6127cf

Browse files
committed
Define add/remove/move only once for better performance
1 parent d5153d0 commit c6127cf

File tree

1 file changed

+63
-61
lines changed

1 file changed

+63
-61
lines changed

src/form.js

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,6 @@ export default class Form extends React.Component {
120120
coords.shift();
121121

122122
this.setState((state) => {
123-
function addDataUsingCoords(coords, data, value) {
124-
let coord = coords.shift();
125-
if (!isNaN(Number(coord)))
126-
coord = Number(coord);
127-
128-
if (coords.length) {
129-
addDataUsingCoords(coords, data[coord], value);
130-
} else {
131-
if (Array.isArray(data[coord])) {
132-
data[coord].push(value);
133-
}
134-
else {
135-
if (Array.isArray(data)) {
136-
data.push(value);
137-
} else {
138-
data[coord] = value;
139-
}
140-
}
141-
}
142-
}
143-
144123
let _data = JSON.parse(JSON.stringify(state.data));
145124

146125
addDataUsingCoords(coords, _data, blankData);
@@ -154,21 +133,6 @@ export default class Form extends React.Component {
154133
coords.shift();
155134

156135
this.setState((state) => {
157-
function removeDataUsingCoords(coords, data) {
158-
let coord = coords.shift();
159-
if (!isNaN(Number(coord)))
160-
coord = Number(coord);
161-
162-
if (coords.length) {
163-
removeDataUsingCoords(coords, data[coord]);
164-
} else {
165-
if (Array.isArray(data))
166-
data.splice(coord, 1); // in-place mutation
167-
else
168-
delete data[coord];
169-
}
170-
}
171-
172136
let _data = JSON.parse(JSON.stringify(state.data));
173137

174138
removeDataUsingCoords(coords, _data);
@@ -185,31 +149,6 @@ export default class Form extends React.Component {
185149
newCoords.shift();
186150

187151
this.setState((state) => {
188-
function moveDataUsingCoords(oldCoords, newCoords, data) {
189-
let oldCoord = oldCoords.shift();
190-
191-
if (!isNaN(Number(oldCoord)))
192-
oldCoord = Number(oldCoord);
193-
194-
if (oldCoords.length) {
195-
moveDataUsingCoords(oldCoords, newCoords, data[oldCoord]);
196-
} else {
197-
if (Array.isArray(data)) {
198-
/* Using newCoords allows us to move items from
199-
one array to another.
200-
However, for now, we're only moving items in a
201-
single array.
202-
*/
203-
let newCoord = newCoords[newCoords.length - 1];
204-
205-
let item = data[oldCoord];
206-
207-
data.splice(oldCoord, 1);
208-
data.splice(newCoord, 0, item);
209-
}
210-
}
211-
}
212-
213152
let _data = JSON.parse(JSON.stringify(state.data));
214153

215154
moveDataUsingCoords(oldCoords, newCoords, _data);
@@ -236,3 +175,66 @@ export default class Form extends React.Component {
236175
);
237176
}
238177
}
178+
179+
180+
function addDataUsingCoords(coords, data, value) {
181+
let coord = coords.shift();
182+
if (!isNaN(Number(coord)))
183+
coord = Number(coord);
184+
185+
if (coords.length) {
186+
addDataUsingCoords(coords, data[coord], value);
187+
} else {
188+
if (Array.isArray(data[coord])) {
189+
data[coord].push(value);
190+
}
191+
else {
192+
if (Array.isArray(data)) {
193+
data.push(value);
194+
} else {
195+
data[coord] = value;
196+
}
197+
}
198+
}
199+
}
200+
201+
function removeDataUsingCoords(coords, data) {
202+
let coord = coords.shift();
203+
if (!isNaN(Number(coord)))
204+
coord = Number(coord);
205+
206+
if (coords.length) {
207+
removeDataUsingCoords(coords, data[coord]);
208+
} else {
209+
if (Array.isArray(data))
210+
data.splice(coord, 1); // in-place mutation
211+
else
212+
delete data[coord];
213+
}
214+
}
215+
216+
217+
function moveDataUsingCoords(oldCoords, newCoords, data) {
218+
let oldCoord = oldCoords.shift();
219+
220+
if (!isNaN(Number(oldCoord)))
221+
oldCoord = Number(oldCoord);
222+
223+
if (oldCoords.length) {
224+
moveDataUsingCoords(oldCoords, newCoords, data[oldCoord]);
225+
} else {
226+
if (Array.isArray(data)) {
227+
/* Using newCoords allows us to move items from
228+
one array to another.
229+
However, for now, we're only moving items in a
230+
single array.
231+
*/
232+
let newCoord = newCoords[newCoords.length - 1];
233+
234+
let item = data[oldCoord];
235+
236+
data.splice(oldCoord, 1);
237+
data.splice(newCoord, 0, item);
238+
}
239+
}
240+
}

0 commit comments

Comments
 (0)