1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:27:45 +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

@ -2,7 +2,7 @@ function assert(x) { if (!x) throw 1; }
try {
assert(Math.abs('-1') === 1);
assert(Math.abs(0 - 2) === 2);
assert(Math.abs(-2) === 2);
assert(Math.abs(null) === 0);
assert(Math.abs('') === 0);
assert(Math.abs([]) === 0);

View file

@ -22,7 +22,7 @@ try {
assert(s.startsWith("foo", false) === true);
assert(s.startsWith("foo", true) === false);
assert(s.startsWith("foo", "foo") === true);
assert(s.startsWith("foo", 0 - 1) === true);
assert(s.startsWith("foo", -1) === true);
assert(s.startsWith("foo", 42) === false);
assert(s.startsWith("bar", 3) === true);
assert(s.startsWith("bar", "3") === true);
@ -31,7 +31,7 @@ try {
assert(s.startsWith("") === true);
assert(s.startsWith("", 0) === true);
assert(s.startsWith("", 1) === true);
assert(s.startsWith("", 0 - 1) === true);
assert(s.startsWith("", -1) === true);
assert(s.startsWith("", 42) === true);
console.log("PASS");

View file

@ -8,6 +8,8 @@ try {
assert(!o.a === false);
assert(!o.a === !(o.a));
assert(~o.a === ~(o.a));
assert(+o.a === +(o.a));
assert(-o.a === -(o.a));
assert((typeof "x" === "string") === true);
assert(!(typeof "x" === "string") === false);

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) {