Passed
Push — main ( 5cacf5...da0f78 )
by Pedro
02:28
created

src/index.ts   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 73
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 3
eloc 36
mnd 3
bc 3
fnc 0
dl 0
loc 73
ccs 10
cts 11
cp 0.9091
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
/*
2
 * decimal.js-i18n v0.2.6
3
 * Full internationalization support for decimal.js.
4
 * MIT License
5
 * Copyright (c) 2022 Pedro José Batista <[email protected]>
6
 * https://github.com/pjbatista/decimal.js-i18n
7
 */
8
/* eslint-disable @typescript-eslint/naming-convention,@typescript-eslint/no-unsafe-argument */
9 1
import Decimal from "decimal.js";
10 1
import Format from "./format";
11
import type { FormatLocale, FormatOptions, FormatNotation, FormatStyle } from "./format";
12
13 1
const main = (Decimal: Decimal.Constructor) => {
14
    // Do not attempt to redefine the module, if already extended
15 2
    if (typeof Decimal.Format !== "undefined") {
16
        return;
17
    }
18
19 1
    Object.defineProperty(Decimal, "Format", { value: Format });
20 1
    Object.defineProperty(Decimal.prototype, "toLocaleString", {
21
        value: function (this: Decimal.Instance, locales, options) {
22 4
            return new Decimal.Format(locales, options).format(this);
23
        } as typeof Decimal.prototype.toLocaleString,
24
    });
25
};
26
27 2
main(globalThis.__Decimal__Class__Global__ ?? require("decimal.js"));
28
29
declare module "decimal.js" {
30
    export interface Decimal {
31
        /**
32
         * Returns a string with a language-sensitive representation of this decimal number.
33
         *
34
         * @template TNotation Numeric notation of formatting.
35
         * @template TStyle Numeric style of formatting.
36
         * @param locales A string with a [BCP 47](https://www.rfc-editor.org/info/bcp47) language tag, or an
37
         *   array of such strings.
38
         *
39
         *   For the general form and interpretation of this parameter, see the [Intl page on
40
         *   MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl).
41
         * @param options Object used to configure the behavior of the string localization.
42
         * @returns A localized and formatted string.
43
         */
44
        toLocaleString: <TNotation extends FormatNotation = "standard", TStyle extends FormatStyle = "decimal">(
45
            locales?: FormatLocale | FormatLocale[],
46
            options?: FormatOptions<TNotation, TStyle>,
47
        ) => string;
48
    }
49
50
    // In order to appropriately represent the Format class, it needs to be placed on Decimal as a 'static' member:
51
52
    // eslint-disable-next-line @typescript-eslint/no-namespace
53
    export namespace Decimal {
54
        /**
55
         * The `Decimal.Format` object enables language-sensitive decimal number formatting. It is entirely
56
         * based on `Intl.NumberFormat`, with the options of the latter being 100% compatible with it.
57
         *
58
         * This class, however, extend the numeric digits constraints of `Intl.NumberFormat` from 21 to
59
         * 1000000000 in order to fully take advantage of the arbitrary-precision of `decimal.js`.
60
         */
61
        export { Format };
62
    }
63
}
64
65
declare global {
66
    export class globalThis {
67
        /** Used by the `extend` submodule to prevent it from loading directly from `decimal.js`. */
68
        static __Decimal__Class__Global__: Decimal.Constructor | undefined;
69
    }
70
}
71 1
export { Decimal };
72
export default Decimal;
73