You-Dont-Need-Lodash-Underscore
You-Dont-Need-Lodash-Underscore copied to clipboard
Why don't you use `instanceof` for `isDate`?
Here is example code:
// Lodash
console.log(_.isDate(new Date()));
// output: true
// Native
console.log(new Date() instanceof Date);
// output: true
I think you can check that:
https://stackoverflow.com/a/643827
"you can use the instanceof operator, i.e. But it will return true for invalid dates too, e.g. new Date('random_string') is also instance of Date"
Lodash also returns true for invalid dates such as _.isDate("random_string").
Also jest can mess up the instanceof functionality.
Lodash also returns
truefor invalid dates such as_.isDate("random_string").
You should have said
import _ from 'lodash'
console.log(_.isDate("random_string")) // false
console.log(_.isDate(new Date("random_string"))) // true, it's a Date, but invalid
Note: if you want a code that check, you can check as writed on the stackoverflow link with something like
Object.prototype.toString.call(a) === '[object Date]' && !isNaN(a)
import _ from 'lodash'
const test = (a)=>{
console.log(
_.isDate(a),
a instanceof Date,
Object.prototype.toString.call(a) === '[object Date]',
Object.prototype.toString.call(a) === '[object Date]' && !isNaN(a)
)
}
test("random_string")
// false false false false
test(new Date('random_string'))
// false false false false
test(new Date())
// false false false true
playground: https://playcode.io/lodash