refactor: utils.type() #4306
Labels
status: accepting prs
Mocha can use your help with this one!
type: chore
generally involving deps, tooling, configuration, etc.
utils.type(value)
is a function which returns a human-readable "type" of whatever value you pass it.About
type()
:undefined
,null
, etc); if it does not find these, it then looks at the result ofObject.prototype.toString.call(value)
(e.g.,[Object object]
) and extracting the second token from within the bracketsdate
fortype(new Date())
anderror
fortype(new Error())
; more granularity thantypeof
offersArray.isArray(value)
for arrays (where available)value === null
fornull
typeof value
for most other stuffWith additions of async functions and ESM, the
Object.prototype.toString
strategy adds new potential return values oftype()
:type(async function() {})
returnsasyncfunction
n
imported vialet n = await import(name)
,type(n)
returnsmodule
While the new values may be good for displaying diffs, they are not helpful in the general case. For example, a return value of
asyncfunction
will tell us thatvalue
is a function which returns aPromise
, but a function needn't beasync
to return aPromise
, so it adds little value. We have no known use case for a return value ofmodule
outside of diffing (and maybe not even then--we should probably play with that one in our test suite).This is what I'd like to propose:
type()
should be renamed to e.g.,canonicalType()
utils.canoncalize()
, which is part of the comparison/diff codeutils
is just a code dump, as you'd expect.type()
?) should be created to be used elsewhere in the codebasetypeof
,Array.isArray()
, around the codebase should be replaced with this new function, for consistencyHappy to hear alternatives, as long as those alternatives do not involve "rewrite Mocha in TypeScript"
The text was updated successfully, but these errors were encountered: