mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
Changeset: Refactor makeAttribsString
for readability
This commit is contained in:
parent
9e7b142bb7
commit
982d8ad0f2
1 changed files with 17 additions and 18 deletions
|
@ -1905,25 +1905,24 @@ exports.builder = (oldLen) => {
|
||||||
return self;
|
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) => {
|
exports.makeAttribsString = (opcode, attribs, pool) => {
|
||||||
// makeAttribsString(opcode, '*3') or makeAttribsString(opcode, [['foo','bar']], myPool) work
|
if (!attribs || !['=', '+'].includes(opcode)) return '';
|
||||||
if (!attribs) {
|
if (typeof attribs === 'string') return attribs;
|
||||||
return '';
|
return sortAttribs(attribs.filter(([k, v]) => v || opcode === '='))
|
||||||
} else if ((typeof attribs) === 'string') {
|
.map((a) => `*${exports.numToString(pool.putAttrib(a))}`)
|
||||||
return attribs;
|
.join('');
|
||||||
} 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('');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue