Simplify only assignment

This commit is contained in:
toasted-nutbread 2020-02-15 17:31:08 -05:00
parent 19fb7dacb4
commit f143632f28
2 changed files with 33 additions and 36 deletions

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/*global utilSetEqual, utilSetIntersection, apiTemplateRender*/ /*global apiTemplateRender*/
function dictEnabledSet(options) { function dictEnabledSet(options) {
const enabledDictionaryMap = new Map(); const enabledDictionaryMap = new Map();
@ -145,6 +145,30 @@ function dictTermsGroup(definitions, dictionaries) {
return dictTermsSort(results); return dictTermsSort(results);
} }
function dictAreSetsEqual(set1, set2) {
if (set1.size !== set2.size) {
return false;
}
for (const value of set1) {
if (!set2.has(value)) {
return false;
}
}
return true;
}
function dictGetSetIntersection(set1, set2) {
const result = [];
for (const value of set1) {
if (set2.has(value)) {
result.push(value);
}
}
return result;
}
function dictTermsMergeBySequence(definitions, mainDictionary) { function dictTermsMergeBySequence(definitions, mainDictionary) {
const sequencedDefinitions = new Map(); const sequencedDefinitions = new Map();
const nonSequencedDefinitions = []; const nonSequencedDefinitions = [];
@ -262,16 +286,15 @@ function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices
} }
for (const definition of definitionsByGloss.values()) { for (const definition of definitionsByGloss.values()) {
definition.only = []; const only = [];
if (!utilSetEqual(definition.expression, resultExpressionSet)) { const expressionSet = definition.expression;
for (const expression of utilSetIntersection(definition.expression, resultExpressionSet)) { const readingSet = definition.reading;
definition.only.push(expression); definition.only = only;
} if (!dictAreSetsEqual(expressionSet, resultExpressionSet)) {
only.push(...dictGetSetIntersection(expressionSet, resultExpressionSet));
} }
if (!utilSetEqual(definition.reading, resultReadingSet)) { if (!dictAreSetsEqual(readingSet, resultReadingSet)) {
for (const reading of utilSetIntersection(definition.reading, resultReadingSet)) { only.push(...dictGetSetIntersection(readingSet, resultReadingSet));
definition.only.push(reading);
}
} }
} }

View File

@ -59,32 +59,6 @@ function utilBackgroundFunctionIsolate(func) {
return backgroundPage.utilFunctionIsolate(func); return backgroundPage.utilFunctionIsolate(func);
} }
function utilSetEqual(setA, setB) {
if (setA.size !== setB.size) {
return false;
}
for (const value of setA) {
if (!setB.has(value)) {
return false;
}
}
return true;
}
function utilSetIntersection(setA, setB) {
return new Set(
[...setA].filter((value) => setB.has(value))
);
}
function utilSetDifference(setA, setB) {
return new Set(
[...setA].filter((value) => !setB.has(value))
);
}
function utilStringHashCode(string) { function utilStringHashCode(string) {
let hashCode = 0; let hashCode = 0;