Yaffle / BigDecimal

a polyfill for decimal propocal
MIT License
51 stars 7 forks source link

TypeScript definitions #13

Closed imirkin closed 1 year ago

imirkin commented 1 year ago

I've developed some (slightly partial, but easily completed) TypeScript definitions for my project (.d.ts file). It's typical for libraries to ship with them directly though. Would this be something interesting to include here?

If so, I'll spend some time cleaning up + making sure it works as part of the library.

Yaffle commented 1 year ago

it should not hurt, right?

imirkin commented 1 year ago

It would hurt if I did the work and you didn't integrate it :) Since it'd be an hour or so to get everything right, figured I'd ask first.

Yaffle commented 1 year ago
type RoundingMode = "floor" | "down" | "ceil" | "up" | "half-even" | "half-up" | "half-down";

interface Rounding {
    roundingMode: RoundingMode;
    maximumSignificantDigits?: number;
    maximumFractionDigits?: number;
}

declare class BigDecimal {
    static BigDecimal(value: bigint | string | number | BigDecimal): BigDecimal;
    static toBigInt(a: BigDecimal): bigint;
    static toNumber(a: BigDecimal): number;
    static unaryMinus(a: BigDecimal): BigDecimal;
    static add(a: BigDecimal, b: BigDecimal, rounding?: Rounding): BigDecimal;
    static subtract(a: BigDecimal, b: BigDecimal, rounding?: Rounding): BigDecimal;
    static multiply(a: BigDecimal, b: BigDecimal, rounding?: Rounding): BigDecimal;
    static divide(a: BigDecimal, b: BigDecimal, rounding?: Rounding): BigDecimal;

    static lessThan(a: BigDecimal, b: BigDecimal): boolean;
    static greaterThan(a: BigDecimal, b: BigDecimal): boolean;
    static equal(a: BigDecimal, b: BigDecimal): boolean;
    static round(a: BigDecimal, rounding: Rounding): BigDecimal;

    toString(): string;
    toFixed(fractionDigits: number, roundingMode?: RoundingMode): string;
    toPrecision(precision: number, roundingMode?: RoundingMode): string;
    toExponential(fractionDigits: number, roundingMode?: RoundingMode): string;

    static log(a: BigDecimal, rounding: Rounding): BigDecimal;
    static exp(a: BigDecimal, rounding: Rounding): BigDecimal;
    static sin(a: BigDecimal, rounding: Rounding): BigDecimal;
    static cos(a: BigDecimal, rounding: Rounding): BigDecimal;
    static atan(a: BigDecimal, rounding: Rounding): BigDecimal;
    static sqrt(a: BigDecimal, rounding: Rounding): BigDecimal;

    static abs(a: BigDecimal): BigDecimal;
    static sign(a: BigDecimal): number;
    static max(a: BigDecimal, b: BigDecimal): BigDecimal;
    static min(a: BigDecimal, b: BigDecimal): BigDecimal;
}

export default BigDecimal;

is it correct ?

imirkin commented 1 year ago

What I have locally is this:

    export type RoundingMode = "floor" | "down" | "ceil" | "up" | "half-even" | "half-up" | "half-down";

    export interface Rounding {
        maximumSignificantDigits?: number;
        maximumFractionDigits?: number;
        roundingMode: RoundingMode;
    }

    export class BigDecimal {
        toString(): string;
        toFixed(fractionDigits: number, roundingMode?: RoundingMode): string;
        toPrecision(precision: number, roundingMode?: RoundingMode): string;
        toExponential(fractionDigits: number, roundingMode?: RoundingMode): string;
    }

    export namespace BigDecimal {
        export function BigDecimal(value: bigint | number | string | BigDecimal): BigDecimal;
        export function toBigInt(value: BigDecimal): bigint;
        export function toNumber(value: BigDecimal): number;
        export function unaryMinus(value: BigDecimal): BigDecimal;
        export function add(a: BigDecimal, b: BigDecimal, rounding?: Rounding | null): BigDecimal;
        export function subtract(a: BigDecimal, b: BigDecimal, rounding?: Rounding | null): BigDecimal;
        export function multiply(a: BigDecimal, b: BigDecimal, rounding?: Rounding | null): BigDecimal;
        export function divide(a: BigDecimal, b: BigDecimal, rounding?: Rounding | null): BigDecimal;

        export function lessThan(a: BigDecimal, b: BigDecimal): boolean;
        export function greaterThan(a: BigDecimal, b: BigDecimal): boolean;
        export function equal(a: BigDecimal, b: BigDecimal): boolean;

        export function round(value: BigDecimal, rounding: Rounding): BigDecimal;

        export function abs(value: BigDecimal): BigDecimal;
        export function sign(value: BigDecimal): number;
        export function max(a: BigDecimal, b: BigDecimal): BigDecimal;
        export function min(a: BigDecimal, b: BigDecimal): BigDecimal;
    }

Which misses some of the advanced math functions (because I don't use them). It's also missing BigFloat.

I'm not sure if it's better to declare as static methods on a class, or whether to do the merged class + namespace as I have it.

imirkin commented 1 year ago

Basically what needs to happen is primary a bunch of testing to ensure that the appropriate ways of importing work as expected with TS. And ensuring that things like "instanceof" behave as one might expect.

Yaffle commented 1 year ago

@imirkin I have added https://github.com/Yaffle/BigDecimal/blob/master/BigDecimsl.d.ts

imirkin commented 1 year ago

Great, thanks! I'll test it out and let you know if there are any issues. I see you left out BigFloat - is that on purpose? I don't need it, but I guess it's in the lib, so good to have an export?

I was thinking maybe something clever can be done with typescript types to avoid the duplication.