Skip to content

Commit 7c1e2fc

Browse files
committed
Changeset: Use Maps to simplify attribute processing
1 parent fe07527 commit 7c1e2fc

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
@@ -1129,32 +1129,23 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
11291129
return att2;
11301130
}
11311131
if (!att2) return att1;
1132-
const atts = [];
1132+
const atts = new Map();
11331133
att1.replace(/\*([0-9a-z]+)/g, (_, a) => {
1134-
atts.push(pool.getAttrib(exports.parseNum(a)));
1134+
const [key, val] = pool.getAttrib(exports.parseNum(a));
1135+
atts.set(key, val);
11351136
return '';
11361137
});
11371138
att2.replace(/\*([0-9a-z]+)/g, (_, a) => {
1138-
const pair = pool.getAttrib(exports.parseNum(a));
1139-
let found = false;
1140-
for (let i = 0; i < atts.length; i++) {
1141-
const oldPair = atts[i];
1142-
if (oldPair[0] !== pair[0]) continue;
1143-
if (pair[1] || resultIsMutation) {
1144-
oldPair[1] = pair[1];
1145-
} else {
1146-
atts.splice(i, 1);
1147-
}
1148-
found = true;
1149-
break;
1150-
}
1151-
if ((!found) && (pair[1] || resultIsMutation)) {
1152-
atts.push(pair);
1139+
const [key, val] = pool.getAttrib(exports.parseNum(a));
1140+
if (val || resultIsMutation) {
1141+
atts.set(key, val);
1142+
} else {
1143+
atts.delete(key);
11531144
}
11541145
return '';
11551146
});
11561147
const buf = exports.stringAssembler();
1157-
for (const att of sortAttribs(atts)) {
1148+
for (const att of sortAttribs([...atts])) {
11581149
buf.append('*');
11591150
buf.append(exports.numToString(pool.putAttrib(att)));
11601151
}
@@ -2265,22 +2256,15 @@ const followAttributes = (att1, att2, pool) => {
22652256
// to produce the merged set.
22662257
if ((!att2) || (!pool)) return '';
22672258
if (!att1) return att2;
2268-
const atts = [];
2259+
const atts = new Map();
22692260
att2.replace(/\*([0-9a-z]+)/g, (_, a) => {
2270-
atts.push(pool.getAttrib(exports.parseNum(a)));
2261+
const [key, val] = pool.getAttrib(exports.parseNum(a));
2262+
atts.set(key, val);
22712263
return '';
22722264
});
22732265
att1.replace(/\*([0-9a-z]+)/g, (_, a) => {
2274-
const pair1 = pool.getAttrib(exports.parseNum(a));
2275-
for (let i = 0; i < atts.length; i++) {
2276-
const pair2 = atts[i];
2277-
if (pair1[0] !== pair2[0]) continue;
2278-
if (pair1[1] <= pair2[1]) {
2279-
// winner of merge is pair1, delete this attribute
2280-
atts.splice(i, 1);
2281-
}
2282-
break;
2283-
}
2266+
const [key, val] = pool.getAttrib(exports.parseNum(a));
2267+
if (atts.has(key) && val <= atts.get(key)) atts.delete(key);
22842268
return '';
22852269
});
22862270
// we've only removed attributes, so they're already sorted

0 commit comments

Comments
 (0)