mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:07:44 +00:00
LibJS: Implement ECMA-402 BigInt.prototype.toLocaleString
This commit is contained in:
parent
d6e926e5b1
commit
687276fc38
2 changed files with 95 additions and 1 deletions
|
@ -6,11 +6,14 @@
|
||||||
|
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <AK/TypeCasts.h>
|
#include <AK/TypeCasts.h>
|
||||||
|
#include <LibJS/Runtime/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/BigIntObject.h>
|
#include <LibJS/Runtime/BigIntObject.h>
|
||||||
#include <LibJS/Runtime/BigIntPrototype.h>
|
#include <LibJS/Runtime/BigIntPrototype.h>
|
||||||
#include <LibJS/Runtime/Completion.h>
|
#include <LibJS/Runtime/Completion.h>
|
||||||
#include <LibJS/Runtime/Error.h>
|
#include <LibJS/Runtime/Error.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/Intl/NumberFormat.h>
|
||||||
|
#include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
|
@ -61,9 +64,21 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntPrototype::to_string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 21.2.3.2 BigInt.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-bigint.prototype.tolocalestring
|
// 21.2.3.2 BigInt.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-bigint.prototype.tolocalestring
|
||||||
|
// 19.3.1 BigInt.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-bigint.prototype.tolocalestring
|
||||||
JS_DEFINE_NATIVE_FUNCTION(BigIntPrototype::to_locale_string)
|
JS_DEFINE_NATIVE_FUNCTION(BigIntPrototype::to_locale_string)
|
||||||
{
|
{
|
||||||
return to_string(vm, global_object);
|
auto locales = vm.argument(0);
|
||||||
|
auto options = vm.argument(1);
|
||||||
|
|
||||||
|
// 1. Let x be ? thisBigIntValue(this value).
|
||||||
|
auto* bigint = TRY(this_bigint_value(global_object, vm.this_value(global_object)));
|
||||||
|
|
||||||
|
// 2. Let numberFormat be ? Construct(%NumberFormat%, « locales, options »).
|
||||||
|
auto* number_format = static_cast<Intl::NumberFormat*>(TRY(construct(global_object, *global_object.intl_number_format_constructor(), locales, options)));
|
||||||
|
|
||||||
|
// 3. Return ? FormatNumeric(numberFormat, x).
|
||||||
|
auto formatted = Intl::format_numeric(global_object, *number_format, bigint);
|
||||||
|
return js_string(vm, move(formatted));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 21.2.3.4 BigInt.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-bigint.prototype.valueof
|
// 21.2.3.4 BigInt.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-bigint.prototype.valueof
|
||||||
|
|
|
@ -8,3 +8,82 @@ test("calling with non-BigInt |this|", () => {
|
||||||
BigInt.prototype.toLocaleString.call("foo");
|
BigInt.prototype.toLocaleString.call("foo");
|
||||||
}).toThrowWithMessage(TypeError, "Not an object of type BigInt");
|
}).toThrowWithMessage(TypeError, "Not an object of type BigInt");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("default", () => {
|
||||||
|
expect(1n.toLocaleString("en")).toBe("1");
|
||||||
|
expect(12n.toLocaleString("en")).toBe("12");
|
||||||
|
expect(123n.toLocaleString("en")).toBe("123");
|
||||||
|
expect(123456789123456789123456789123456789n.toLocaleString("en")).toBe(
|
||||||
|
"123,456,789,123,456,789,123,456,789,123,456,789"
|
||||||
|
);
|
||||||
|
|
||||||
|
const ar = new Intl.NumberFormat("ar");
|
||||||
|
expect(1n.toLocaleString("ar")).toBe("\u0661");
|
||||||
|
expect(12n.toLocaleString("ar")).toBe("\u0661\u0662");
|
||||||
|
expect(123n.toLocaleString("ar")).toBe("\u0661\u0662\u0663");
|
||||||
|
expect(123456789123456789123456789123456789n.toLocaleString("ar")).toBe(
|
||||||
|
"\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669\u066c\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669\u066c\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669\u066c\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("integer digits", () => {
|
||||||
|
expect(1n.toLocaleString("en", { minimumIntegerDigits: 2 })).toBe("01");
|
||||||
|
expect(12n.toLocaleString("en", { minimumIntegerDigits: 2 })).toBe("12");
|
||||||
|
expect(123n.toLocaleString("en", { minimumIntegerDigits: 2 })).toBe("123");
|
||||||
|
|
||||||
|
expect(1n.toLocaleString("ar", { minimumIntegerDigits: 2 })).toBe("\u0660\u0661");
|
||||||
|
expect(12n.toLocaleString("ar", { minimumIntegerDigits: 2 })).toBe("\u0661\u0662");
|
||||||
|
expect(123n.toLocaleString("ar", { minimumIntegerDigits: 2 })).toBe("\u0661\u0662\u0663");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("significant digits", () => {
|
||||||
|
expect(
|
||||||
|
1n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("1.000");
|
||||||
|
expect(
|
||||||
|
12n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("12.00");
|
||||||
|
expect(
|
||||||
|
123n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("123.0");
|
||||||
|
expect(
|
||||||
|
1234n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("1,234");
|
||||||
|
expect(
|
||||||
|
12345n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("12,345");
|
||||||
|
expect(
|
||||||
|
123456n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("123,456");
|
||||||
|
expect(
|
||||||
|
1234567n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("1,234,570");
|
||||||
|
expect(
|
||||||
|
1234561n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("1,234,560");
|
||||||
|
|
||||||
|
expect(
|
||||||
|
1n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u066b\u0660\u0660\u0660");
|
||||||
|
expect(
|
||||||
|
12n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u0662\u066b\u0660\u0660");
|
||||||
|
expect(
|
||||||
|
123n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u0662\u0663\u066b\u0660");
|
||||||
|
expect(
|
||||||
|
1234n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u066c\u0662\u0663\u0664");
|
||||||
|
expect(
|
||||||
|
12345n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u0662\u066c\u0663\u0664\u0665");
|
||||||
|
expect(
|
||||||
|
123456n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u0662\u0663\u066c\u0664\u0665\u0666");
|
||||||
|
expect(
|
||||||
|
1234567n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0667\u0660");
|
||||||
|
expect(
|
||||||
|
1234561n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||||
|
).toBe("\u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0666\u0660");
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue