• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16package std.core;
17
18// NOTE(aleksander-sotov) Move all code below into the namespace (#20417)
19export class NumberFormat {
20
21    /**
22     * The Intl.NumberFormat() constructor creates Intl.NumberFormat objects.
23     *
24     * @param locale locale from which to load formats and symbols for number formatting
25     */
26    public constructor(locale: string = "") {
27        if (locale == "") {
28            locale = "en-US";  // en-US as default locale
29        }
30        this.locale = locale;
31    }
32
33    /**
34     * Formats a number according to the locale and formatting options of this Intl.NumberFormat object.
35     *
36     * @param value A number to format.
37     *
38     * @returns string representing the given number formatted according to the locale and formatting options of this Intl.NumberFormat object.
39     */
40    public format(value: number): string {
41        return NumberFormat.formatDouble(this.locale, value);
42    }
43
44    /**
45     * Formats a number according to the locale and formatting options of this Intl.NumberFormat object.
46     *
47     * @param value A number or BigInt to format
48     *
49     * @returns A string representing the given number formatted according to the locale and formatting options of this Intl.NumberFormat object.
50     */
51    public format(value: number | bigint): string {
52        if (value instanceof BigInt) {
53            return NumberFormat.formatDecStr(this.locale, value.toString());
54        }
55        return NumberFormat.formatDouble(this.locale, value);
56    }
57
58    /**
59     * Formats a range of numbers according to the locale and formatting options of this Intl.NumberFormat object.
60     *
61     * @param start Start range to format a number or BigInt
62     *
63     * @param end End range to format a number or BigInt
64     *
65     * @throws TypeError if start or end is NaN
66     *
67     * @returns A string representing the given range of numbers formatted according to the locale and formatting options of this Intl.NumberFormat object.
68     */
69    public formatRange(start: number | bigint, end: number | bigint): string {
70        let isStartBigInt: boolean = false;
71        if (start instanceof BigInt) {
72            isStartBigInt = true;
73        } else if (isNaN(start as number)) {
74            throw new TypeError("Invalid start: NaN");
75        }
76        let isEndBigInt: boolean = false;
77        if (end instanceof BigInt) {
78            isEndBigInt = true;
79        } else if (isNaN(end as number)) {
80            throw new TypeError("Invalid end: NaN");
81        }
82
83        if (!isStartBigInt && !isEndBigInt) {
84            return NumberFormat.formatRangeDoubleDouble(this.locale, start as double, end as double);
85        } else if (!isStartBigInt && isEndBigInt) {
86            return NumberFormat.formatRangeDoubleDecStr(this.locale, start as double, (end as BigInt).toString());
87        } else if (isStartBigInt && !isEndBigInt) {
88            return NumberFormat.formatRangeDecStrDouble(this.locale, (start as BigInt).toString(), end as double);
89        }
90        return NumberFormat.formatRangeDecStrDecStr(this.locale, (start as BigInt).toString(), (end as BigInt).toString());
91    }
92
93    private static native formatDouble(locale: string, value: double): string;
94    private static native formatDecStr(locale: string, value: string): string;
95    private static native formatRangeDoubleDouble(locale: string, start: number, end: number): string;
96    private static native formatRangeDoubleDecStr(locale: string, start: number, end: string): string;
97    private static native formatRangeDecStrDouble(locale: string, start: string, end: number): string;
98    private static native formatRangeDecStrDecStr(locale: string, start: string, end: string): string;
99
100    private locale: String;
101}
102