benchmark: Use same equivalency checker as spec compliance check

This commit is contained in:
Rebecca Turner
2019-02-20 14:09:55 -08:00
parent 75f613be61
commit ad6c9e09c4
3 changed files with 55 additions and 76 deletions

View File

@@ -1,27 +1,4 @@
'use strict'
const assert = require('assert')
module.exports = (a, b) => assert(isObjectDeeply(a, b))
function isObjectDeeply (aa, bb) {
if (typeof aa !== typeof bb) return false
if (aa == null || bb == null || typeof aa !== 'object') return Object.is(aa, bb)
if (Array.isArray(aa) && Array.isArray(bb)) return isArrayDeeply(aa, bb)
if (Array.isArray(aa) || Array.isArray(bb)) return false
if (aa instanceof Date && bb instanceof Date) return aa.toISOString() === bb.toISOString()
let aaKeys = Object.keys(aa)
let bbKeys = Object.keys(bb)
if (aaKeys.length !== bbKeys.length) return false
for (let key of aaKeys) {
if (bbKeys.indexOf(key) === -1) return false
if (!isObjectDeeply(aa[key], bb[key])) return false
}
return true
}
function isArrayDeeply (aa, bb) {
if (aa.length !== bb.length) return false
for (let ii = 0; ii < aa.length; ++ii) {
if (!isObjectDeeply(aa[ii], bb[ii])) return false
}
return true
}
const isDeeply = require('./test/lib/is-deeply.js')
module.exports = (a, b) => assert(isDeeply(a, b))

48
test/lib/is-deeply.js Normal file
View File

@@ -0,0 +1,48 @@
'use strict'
module.exports = isDeeply
function kindOf (aa) {
const aaType = typeof aa
if (aaType === 'number' || aaType === 'bigint') return 'numberish'
if (aa instanceof Date) return 'date'
if (Array.isArray(aa)) return 'array'
if (Buffer.isBuffer(aa)) return 'buffer'
return aaType
}
function isDeeply (aa, bb) {
if (kindOf(aa) !== kindOf(bb)) return false
if (Object.is(aa, bb)) return true
const kind = kindOf(aa)
if (kind === 'numberish') {
/* NOTE: This allows numbers to be compared to bigints. They only count
as == if they represent the exact same value, so if a bigint was
needed to represent this number the check will fail, but if it can be
safely either, then either is allowed. */
/* eslint-disable eqeqeq */
return aa == bb
}
if (kind === 'date') {
if (aa.toISOString().replace(/[.]0+$/, '') === bb.toISOString().replace(/[.]0+$/, '')) return true
if (Number(aa) === Number(bb)) return true
if (Number(new Date(aa.toISOString())) === Number(new Date(bb.toISOString()))) return true
return false
}
if (kind === 'array') {
if (aa.length !== bb.length) return false
for (let ii = 0; ii < aa.length; ++ii) {
if (!isDeeply(aa[ii], bb[ii])) return false
}
return true
}
if (kind !== 'object') return false
let aaKeys = Object.keys(aa)
let bbKeys = Object.keys(bb)
if (aaKeys.length !== bbKeys.length) return false
for (let key of aaKeys) {
if (bbKeys.indexOf(key) === -1) return false
if (!isDeeply(aa[key], bb[key])) return false
}
return true
}

View File

@@ -2,58 +2,12 @@
const tap = require('tap')
module.exports = tap
const isDeeply = require('./is-deeply.js')
if (!tap.deeplyObjectIs) {
tap.Test.prototype.addAssert('deeplyObjectIs', 2, function (found, wanted, message, extra) {
const isDeeply = isObjectDeeply(found, wanted)
if (!isDeeply) tap.comment(found, wanted)
return this.ok(isDeeply, message, extra)
const equiv = isDeeply(found, wanted)
if (!equiv) tap.comment(found, wanted)
return this.ok(equiv, message, extra)
})
}
function kindOf (aa) {
const aaType = typeof aa
if (aaType === 'number' || aaType === 'bigint') return 'numberish'
if (aa instanceof Date) return 'date'
if (Array.isArray(aa)) return 'array'
if (Buffer.isBuffer(aa)) return 'buffer'
return aaType
}
function isObjectDeeply (aa, bb) {
if (kindOf(aa) !== kindOf(bb)) return false
if (Object.is(aa, bb)) return true
const kind = kindOf(aa)
if (kind === 'numberish') {
/* NOTE: This allows numbers to be compared to bigints. They only count
as == if they represent the exact same value, so if a bigint was
needed to represent this number the check will fail, but if it can be
safely either, then either is allowed. */
/* eslint-disable eqeqeq */
return aa == bb
}
if (kind === 'date') {
if (aa.toISOString().replace(/[.]0+$/, '') === bb.toISOString().replace(/[.]0+$/, '')) return true
if (Number(aa) === Number(bb)) return true
if (Number(new Date(aa.toISOString())) === Number(new Date(bb.toISOString()))) return true
return false
}
if (kind === 'array') return isArrayDeeply(aa, bb)
if (kind !== 'object') return false
let aaKeys = Object.keys(aa)
let bbKeys = Object.keys(bb)
if (aaKeys.length !== bbKeys.length) return false
for (let key of aaKeys) {
if (bbKeys.indexOf(key) === -1) return false
if (!isObjectDeeply(aa[key], bb[key])) return false
}
return true
}
function isArrayDeeply (aa, bb) {
if (aa.length !== bb.length) return false
for (let ii = 0; ii < aa.length; ++ii) {
if (!isObjectDeeply(aa[ii], bb[ii])) return false
}
return true
}