Skip to content

Commit

Permalink
Refactor type comparison logic in addTypes function
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesodland committed Oct 12, 2024
1 parent 326fc3b commit 0586aaa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/numeric-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ export function addTypes(input1, input2) {
// 3a If all the entries of type1 with non-zero values are contained in type2 with the same value, and vice-versa
// Copy all of type1’s entries to finalType, and then copy all of type2’s entries to finalType that
// finalType doesn’t already contain. Set finalType’s percent hint to type1’s percent hint. Return finalType.
if (!baseTypes.some(baseType => (type1[baseType] || type2[baseType]) && type1[baseType] !== type2[baseType])) {
if (baseTypes
.filter(baseType=> type1[baseType] || type2[baseType])
.every(baseType => type1[baseType] === type2[baseType])) {
return {
...type2,
...type1,
Expand All @@ -517,7 +519,9 @@ export function addTypes(input1, input2) {
// If, afterwards, all the entries of type1 with non-zero values are contained in type2 with the same value,
// and vice versa, then copy all of type1’s entries to finalType, and then copy all of type2’s entries to
// finalType that finalType doesn’t already contain. Set finalType’s percent hint to hint. Return finalType.
if (!baseTypes.some(baseType => (tempType1[baseType] || tempType2[baseType]) && tempType1[baseType] !== tempType2[baseType])) {
if (baseTypes
.filter(baseType => tempType1[baseType] || tempType2[baseType])
.every(baseType => tempType1[baseType] === tempType2[baseType])) {
return {
...tempType2,
...tempType1,
Expand Down
4 changes: 2 additions & 2 deletions test/unit/cssom/css-numeric-value-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('CSSNumericValue.type()', () => {
];

for (const [unitA, unitB, type] of compatibleTuples) {
test(`Type of CSSMathSum of '${unitA}' and '${unitB}' is '${type}'`, () => {
test(`Type of CSSMathSum of '${unitA}' and '${unitB}' is '${JSON.stringify(type)}'`, () => {
expect(new CSSMathSum(new CSSUnitValue(10, unitA), new CSSUnitValue(10, unitB)).type()).toEqual(type);
});
}
Expand All @@ -100,7 +100,7 @@ describe('CSSNumericValue.type()', () => {
['percent', 'fr', {flex: 1, percentHint: 'flex'}], //
];
for (const [unitA, unitB, type] of percentHintTuples) {
test(`Type of CSSMathSum of '${unitA}' and '${unitB}' is '${type}'`, () => {
test(`Type of CSSMathSum of '${unitA}' and '${unitB}' is '${JSON.stringify(type)}'`, () => {
expect(new CSSMathSum(new CSSUnitValue(10, unitA), new CSSUnitValue(10, unitB)).type()).toEqual(type);
});
}
Expand Down

0 comments on commit 0586aaa

Please sign in to comment.