index.ts 891 字节
Newer Older
1
import BigNumber from 'bignumber.js';
2 3 4
import moment from 'moment';

export const formatTime = (value: number, language: string, formatter = 'L LTS') =>
5
    moment(Math.floor(value), 'x').locale(language).format(formatter);
6

7
export const quantile = (values: number[], p: number) => {
8 9 10 11 12
    const n = values.length;
    if (!n) {
        return NaN;
    }
    if ((p = +p) <= 0 || n < 2) {
13
        return values[0];
14 15
    }
    if (p >= 1) {
16
        return values[n - 1];
17
    }
18 19 20 21 22
    const i = new BigNumber(p).multipliedBy(n - 1);
    const i0 = i.integerValue().toNumber();
    const value0 = new BigNumber(values[i0]);
    const value1 = new BigNumber(values[i0 + 1]);
    return value0.plus(value1.minus(value0).multipliedBy(i.minus(i0))).toNumber();
23
};
24 25 26

export const distance = (p1: [number, number], p2: [number, number]): number =>
    Math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2);