-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strict comparison? #2060
Comments
Thanks for your question Koos. Mathjs uses type-coercion when comparing values, resulting in expressions like There are no functions implemented to do strict equality checks. If you need this, you can either create and import a function like |
Thanks for the swift response Jos! Is it perchange on the roadmap to allow for type-safe comparision in expressions? I mean that "1===true" is also allowed as an expression? I indeed thought of writing a function myself, but... I do not think it can be done in such a way that
will use my custom 'equal' function right? So it should be done something like this:
|
This is not on the roadmap, but if there is enough need for it we can consider it and make a plan. Side note: I think the official name for this is subject "strict equality comparison" (vs. type-coersion), it does not directly have to do with type safety.
When you create a mathjs instance, you can pass your own factory function for import { create, all, factory } from 'mathjs'
function strictEqual(a, b) {
return a === b
}
const math = create({
...all,
// pass our own implementation for equal
createEqual: factory('equal', [], () => {
return strictEqual
})
})
function testEvaluate (expr) {
console.log(expr, '=', math.evaluate(expr))
}
testEvaluate('"2"==2') // false
// deepEqual uses equal internally
testEvaluate('deepEqual("2", 2)') // false
|
Thanks! Just asking for a friend: suppose one is bound to ES5 (so no 'import' etc) how would that work? This does not work:
|
In the meantime I did find a solution, namely using math.import, with the second argument being {override: true}
|
hm, yeah, that works for overriding |
Mmm... for the time being I think we are fine, but this might indeed pose a problem in the feature. I am also glad that it seems to be a limitation of the bundled version (and not a limitation of my JS skills ;-), spend hours on it because I thought I was overseeing something obvious... P.S. Great testimony on your personal home page! <>< |
Thanks 😄 I'll give this some more thought. Ideally, when overriding an existing function on |
The problem of functions not being recreated is being addressed in #1975. Will close this issue now. |
How to do type-safe comparison in this (excellent) library?
I expect this one to return 'false', as 1 !== true.
The text was updated successfully, but these errors were encountered: