Git Product home page Git Product logo

Comments (4)

gilly3 avatar gilly3 commented on May 26, 2024 1

Lodash also returns true for invalid dates such as _.isDate("random_string").

from you-dont-need-lodash-underscore.

Its-Just-Nans avatar Its-Just-Nans commented on May 26, 2024

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"

from you-dont-need-lodash-underscore.

Uzlopak avatar Uzlopak commented on May 26, 2024

Also jest can mess up the instanceof functionality.

from you-dont-need-lodash-underscore.

Its-Just-Nans avatar Its-Just-Nans commented on May 26, 2024

Lodash also returns true for 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

from you-dont-need-lodash-underscore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.