Skip to content

Commit 6b688b1

Browse files
committed
Rework comparison strictness NaN check, and separate out tests
1 parent e9ce948 commit 6b688b1

12 files changed

+1152
-1088
lines changed

defaultMethods.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,14 @@ const defaultMethods = {
790790

791791
function createComparator (name, func) {
792792
const opStr = { [Compiled]: name }
793+
const strict = name.length === 3
793794
return {
794795
method: (args, context, above, engine) => {
795796
if (!Array.isArray(args) || args.length <= 1) throw INVALID_ARGUMENTS
796797
if (args.length === 2) {
797798
const a = runOptimizedOrFallback(args[0], engine, context, above)
798799
const b = runOptimizedOrFallback(args[1], engine, context, above)
799-
if (typeof a !== typeof b) {
800+
if (!strict && typeof a !== typeof b) {
800801
if (typeof a === 'string' && Number.isNaN(+a)) throw NaN
801802
if (typeof b === 'string' && Number.isNaN(+b)) throw NaN
802803
}
@@ -805,7 +806,7 @@ function createComparator (name, func) {
805806
let prev = runOptimizedOrFallback(args[0], engine, context, above)
806807
for (let i = 1; i < args.length; i++) {
807808
const current = runOptimizedOrFallback(args[i], engine, context, above)
808-
if (typeof current !== typeof prev) {
809+
if (!strict && typeof current !== typeof prev) {
809810
if (typeof current === 'string' && Number.isNaN(+current)) throw NaN
810811
if (i === 1 && typeof prev === 'string' && Number.isNaN(+prev)) throw NaN
811812
}
@@ -819,7 +820,7 @@ function createComparator (name, func) {
819820
if (args.length === 2) {
820821
const a = await runOptimizedOrFallback(args[0], engine, context, above)
821822
const b = await runOptimizedOrFallback(args[1], engine, context, above)
822-
if (typeof a !== typeof b) {
823+
if (!strict && typeof a !== typeof b) {
823824
if (typeof a === 'string' && Number.isNaN(+a)) throw NaN
824825
if (typeof b === 'string' && Number.isNaN(+b)) throw NaN
825826
}
@@ -828,7 +829,7 @@ function createComparator (name, func) {
828829
let prev = await runOptimizedOrFallback(args[0], engine, context, above)
829830
for (let i = 1; i < args.length; i++) {
830831
const current = await runOptimizedOrFallback(args[i], engine, context, above)
831-
if (typeof current !== typeof prev) {
832+
if (!strict && typeof current !== typeof prev) {
832833
if (typeof current === 'string' && Number.isNaN(+current)) throw NaN
833834
if (i === 1 && typeof prev === 'string' && Number.isNaN(+prev)) throw NaN
834835
}
@@ -840,9 +841,9 @@ function createComparator (name, func) {
840841
compile: (data, buildState) => {
841842
if (!Array.isArray(data)) return false
842843
if (data.length < 2) return false
843-
if (data.length === 2) return buildState.compile`((prev = ${data[0]}) ${opStr} compareCheck(${data[1]}, prev))`
844-
let res = buildState.compile`((prev = ${data[0]}) ${opStr} (prev = compareCheck(${data[1]}, prev)))`
845-
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev ${opStr} (prev = compareCheck(${data[i]}, prev)))`
844+
if (data.length === 2) return buildState.compile`((prev = ${data[0]}) ${opStr} compareCheck(${data[1]}, prev, ${strict}))`
845+
let res = buildState.compile`((prev = ${data[0]}) ${opStr} (prev = compareCheck(${data[1]}, prev, ${strict})))`
846+
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev ${opStr} (prev = compareCheck(${data[i]}, prev, ${strict})))`
846847
return res
847848
},
848849
[OriginalImpl]: true,

optimizer.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ function checkIdioms (logic, engine, above) {
119119
if (logic[comparison] && Array.isArray(logic[comparison]) && engine.methods[comparison][OriginalImpl]) {
120120
const _comparisonFunc = comparisons[comparison]
121121

122-
function comparisonFunc (a, b) {
123-
if (typeof a !== typeof b) {
124-
if (typeof a === 'string' && Number.isNaN(+a)) throw NaN
125-
if (typeof b === 'string' && Number.isNaN(+b)) throw NaN
122+
const comparisonFunc = comparison.length === 3
123+
? _comparisonFunc
124+
: function comparisonFunc (a, b) {
125+
if (typeof a !== typeof b) {
126+
if (typeof a === 'string' && Number.isNaN(+a)) throw NaN
127+
if (typeof b === 'string' && Number.isNaN(+b)) throw NaN
128+
}
129+
return _comparisonFunc(a, b)
126130
}
127-
return _comparisonFunc(a, b)
128-
}
129131

130132
if (logic[comparison].length === 2) {
131133
const [a, b] = logic[comparison]

0 commit comments

Comments
 (0)