mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:07:35 +00:00
LibJS: Add parseFloat()
This commit is contained in:
parent
cc42d75209
commit
6f6b089aa0
3 changed files with 87 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -82,6 +83,7 @@ void GlobalObject::initialize()
|
|||
put_native_function("gc", gc, 0, attr);
|
||||
put_native_function("isNaN", is_nan, 1, attr);
|
||||
put_native_function("isFinite", is_finite, 1, attr);
|
||||
put_native_function("parseFloat", parse_float, 1, attr);
|
||||
|
||||
put("NaN", js_nan(), 0);
|
||||
put("Infinity", js_infinity(), 0);
|
||||
|
@ -140,4 +142,17 @@ Value GlobalObject::is_finite(Interpreter& interpreter)
|
|||
return Value(interpreter.argument(0).to_number().is_finite_number());
|
||||
}
|
||||
|
||||
Value GlobalObject::parse_float(Interpreter& interpreter)
|
||||
{
|
||||
auto string = interpreter.argument(0).to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
for (size_t length = string.length(); length > 0; --length) {
|
||||
auto number = Value(js_string(interpreter, string.substring(0, length))).to_number();
|
||||
if (!number.is_nan())
|
||||
return number;
|
||||
}
|
||||
return js_nan();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ private:
|
|||
static Value gc(Interpreter&);
|
||||
static Value is_nan(Interpreter&);
|
||||
static Value is_finite(Interpreter&);
|
||||
static Value parse_float(Interpreter&);
|
||||
|
||||
Shape* m_empty_object_shape { nullptr };
|
||||
|
||||
|
|
71
Libraries/LibJS/Tests/parseFloat.js
Normal file
71
Libraries/LibJS/Tests/parseFloat.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
const NUMBER_TEST_CASES = [
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
[.23, 0.23],
|
||||
[1.23, 1.23],
|
||||
[0.0123E+2, 1.23],
|
||||
[1.23e4, 12300],
|
||||
[Infinity, Infinity]
|
||||
];
|
||||
|
||||
NUMBER_TEST_CASES.forEach(test => {
|
||||
const value = test[0];
|
||||
const result = test[1];
|
||||
assert(parseFloat(value) === result);
|
||||
assert(parseFloat(+value) === result);
|
||||
assert(parseFloat(-value) === -result);
|
||||
});
|
||||
|
||||
const STRING_TEST_CASES = [
|
||||
["0", 0],
|
||||
["1", 1],
|
||||
[".23", 0.23],
|
||||
["1.23", 1.23],
|
||||
["0.0123E+2", 1.23],
|
||||
["1.23e4", 12300],
|
||||
["Infinity", Infinity]
|
||||
];
|
||||
|
||||
STRING_TEST_CASES.forEach(test => {
|
||||
const value = test[0];
|
||||
const result = test[1];
|
||||
assert(parseFloat(value) === result);
|
||||
assert(parseFloat(`+${value}`) === result);
|
||||
assert(parseFloat(`-${value}`) === -result);
|
||||
assert(parseFloat(`${value}foo`) === result);
|
||||
assert(parseFloat(`+${value}foo`) === result);
|
||||
assert(parseFloat(`-${value}foo`) === -result);
|
||||
assert(parseFloat(` \n \t ${value} \v foo `) === result);
|
||||
assert(parseFloat(` \r -${value} \f \n\n foo `) === -result);
|
||||
assert(parseFloat({ toString: () => value }) === result);
|
||||
});
|
||||
|
||||
const NAN_TEST_CASES = [
|
||||
"",
|
||||
[],
|
||||
[],
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
undefined,
|
||||
NaN,
|
||||
"foo123",
|
||||
"foo+123",
|
||||
"fooInfinity",
|
||||
"foo+Infinity"
|
||||
];
|
||||
|
||||
assert(isNaN(parseFloat()));
|
||||
assert(isNaN(parseFloat("", 123, Infinity)));
|
||||
|
||||
NAN_TEST_CASES.forEach(value => {
|
||||
assert(isNaN(parseFloat(value)));
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue