From 982d8ad0f2a4b209f94ca2690233b7c4186afe72 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 19 Nov 2021 01:08:21 -0500 Subject: [PATCH] Changeset: Refactor `makeAttribsString` for readability --- src/static/js/Changeset.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index a335f7ccb..71d7fe115 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -1905,25 +1905,24 @@ exports.builder = (oldLen) => { return self; }; +/** + * Constructs an attribute string from a sequence of attributes. + * + * @param {string} opcode - The opcode for the Op that will get the resulting attribute string. + * @param {?(Attribute[]|AttributeString)} attribs - The attributes to insert into the pool + * (if necessary) and encode. If an attribute string, no checking is performed to ensure that + * the attributes exist in the pool, are in the canonical order, and contain no duplicate keys. + * If this is an iterable of attributes, `pool` must be non-null. + * @param {AttributePool} pool - Attribute pool. Required if `attribs` is an iterable of attributes, + * ignored if `attribs` is an attribute string. + * @returns {AttributeString} + */ exports.makeAttribsString = (opcode, attribs, pool) => { - // makeAttribsString(opcode, '*3') or makeAttribsString(opcode, [['foo','bar']], myPool) work - if (!attribs) { - return ''; - } else if ((typeof attribs) === 'string') { - return attribs; - } else if (pool && attribs.length) { - if (attribs.length > 1) { - attribs = attribs.slice(); - sortAttribs(attribs); - } - const result = []; - for (const pair of attribs) { - if (opcode === '=' || (opcode === '+' && pair[1])) { - result.push(`*${exports.numToString(pool.putAttrib(pair))}`); - } - } - return result.join(''); - } + if (!attribs || !['=', '+'].includes(opcode)) return ''; + if (typeof attribs === 'string') return attribs; + return sortAttribs(attribs.filter(([k, v]) => v || opcode === '=')) + .map((a) => `*${exports.numToString(pool.putAttrib(a))}`) + .join(''); }; /**