1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 23:27:42 +00:00

LibJS: Implement unary plus / minus

This commit is contained in:
Linus Groh 2020-04-02 17:58:39 +01:00 committed by Andreas Kling
parent 5e6e1fd482
commit a62230770b
9 changed files with 73 additions and 25 deletions

View file

@ -1,31 +1,43 @@
function assert(x) { if (!x) throw 1; }
// FIXME: Just "+x" doesn't parse currently,
// so we use "x - 0", which is effectively the same.
// "0 + x" would _not_ work in all cases.
function n(x) { return x - 0; }
try {
assert(n(false) === 0);
assert(n(true) === 1);
assert(n(null) === 0);
assert(n([]) === 0);
assert(n([[[[[]]]]]) === 0);
assert(n([[[[[42]]]]]) === 42);
assert(n("") === 0);
assert(n("42") === 42);
assert(n(42) === 42);
assert(+false === 0);
assert(-false === 0);
assert(+true === 1);
assert(-true === -1);
assert(+null === 0);
assert(-null === 0);
assert(+[] === 0);
assert(-[] === 0);
assert(+[[[[[]]]]] === 0);
assert(-[[[[[]]]]] === 0);
assert(+[[[[[42]]]]] === 42);
assert(-[[[[[42]]]]] === -42);
assert(+"" === 0);
assert(-"" === 0);
assert(+"42" === 42);
assert(-"42" === -42);
assert(+42 === 42);
assert(-42 === -42);
// FIXME: returns NaN
// assert(n("1.23") === 1.23)
// assert(+"1.23" === 1.23)
// assert(-"1.23" === -1.23)
// FIXME: chokes on ASSERT
// assert(n(1.23) === 1.23);
// assert(+1.23 === 1.23);
// assert(-1.23 === -1.23);
assert(isNaN(n(undefined)));
assert(isNaN(n({})));
assert(isNaN(n({a: 1})));
assert(isNaN(n([1, 2, 3])));
assert(isNaN(n([[[["foo"]]]])));
assert(isNaN(n("foo")));
assert(isNaN(+undefined));
assert(isNaN(-undefined));
assert(isNaN(+{}));
assert(isNaN(-{}));
assert(isNaN(+{a: 1}));
assert(isNaN(-{a: 1}));
assert(isNaN(+[1, 2, 3]));
assert(isNaN(-[1, 2, 3]));
assert(isNaN(+[[[["foo"]]]]));
assert(isNaN(-[[[["foo"]]]]));
assert(isNaN(+"foo"));
assert(isNaN(-"foo"));
console.log("PASS");
} catch (e) {