Skip to content

Commit 22127b8

Browse files
committed
Changeset: Use Maps to simplify attribute processing
1 parent ba7bfa8 commit 22127b8

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

src/static/js/Changeset.js

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,32 +1127,23 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
11271127
return att2;
11281128
}
11291129
if (!att2) return att1;
1130-
const atts = [];
1130+
const atts = new Map();
11311131
att1.replace(/\*([0-9a-z]+)/g, (_, a) => {
1132-
atts.push(pool.getAttrib(exports.parseNum(a)));
1132+
const [key, val] = pool.getAttrib(exports.parseNum(a));
1133+
atts.set(key, val);
11331134
return '';
11341135
});
11351136
att2.replace(/\*([0-9a-z]+)/g, (_, a) => {
1136-
const pair = pool.getAttrib(exports.parseNum(a));
1137-
let found = false;
1138-
for (let i = 0; i < atts.length; i++) {
1139-
const oldPair = atts[i];
1140-
if (oldPair[0] !== pair[0]) continue;
1141-
if (pair[1] || resultIsMutation) {
1142-
oldPair[1] = pair[1];
1143-
} else {
1144-
atts.splice(i, 1);
1145-
}
1146-
found = true;
1147-
break;
1148-
}
1149-
if ((!found) && (pair[1] || resultIsMutation)) {
1150-
atts.push(pair);
1137+
const [key, val] = pool.getAttrib(exports.parseNum(a));
1138+
if (val || resultIsMutation) {
1139+
atts.set(key, val);
1140+
} else {
1141+
atts.delete(key);
11511142
}
11521143
return '';
11531144
});
11541145
const buf = exports.stringAssembler();
1155-
for (const att of sortAttribs(atts)) {
1146+
for (const att of sortAttribs([...atts])) {
11561147
buf.append('*');
11571148
buf.append(exports.numToString(pool.putAttrib(att)));
11581149
}
@@ -2263,22 +2254,15 @@ const followAttributes = (att1, att2, pool) => {
22632254
// to produce the merged set.
22642255
if ((!att2) || (!pool)) return '';
22652256
if (!att1) return att2;
2266-
const atts = [];
2257+
const atts = new Map();
22672258
att2.replace(/\*([0-9a-z]+)/g, (_, a) => {
2268-
atts.push(pool.getAttrib(exports.parseNum(a)));
2259+
const [key, val] = pool.getAttrib(exports.parseNum(a));
2260+
atts.set(key, val);
22692261
return '';
22702262
});
22712263
att1.replace(/\*([0-9a-z]+)/g, (_, a) => {
2272-
const pair1 = pool.getAttrib(exports.parseNum(a));
2273-
for (let i = 0; i < atts.length; i++) {
2274-
const pair2 = atts[i];
2275-
if (pair1[0] !== pair2[0]) continue;
2276-
if (pair1[1] <= pair2[1]) {
2277-
// winner of merge is pair1, delete this attribute
2278-
atts.splice(i, 1);
2279-
}
2280-
break;
2281-
}
2264+
const [key, val] = pool.getAttrib(exports.parseNum(a));
2265+
if (atts.has(key) && val <= atts.get(key)) atts.delete(key);
22822266
return '';
22832267
});
22842268
// we've only removed attributes, so they're already sorted

0 commit comments

Comments
 (0)