Add BN consts and timeToString (#610)

* Add BN consts and timeToString

* docs

* lint

* fixes

* lint

* lint

* add tests, ms

* fix

* test
This commit is contained in:
Keith Ingram
2020-06-09 03:55:21 +12:00
committed by GitHub
parent cd20fe6c8b
commit 56d151fe66
7 changed files with 184 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BN_ZERO, BN_ONE, BN_TEN, BN_HUNDRED, BN_THOUSAND } from '.';
import BN from 'bn.js';
describe('consts', (): void => {
it('BN_ZERO equals 0', (): void => {
expect(
BN_ZERO
).toEqual(new BN(0));
});
it('BN_ONE equals 1', (): void => {
expect(
BN_ONE
).toEqual(new BN(1));
});
it('BN_TEN equals 10', (): void => {
expect(
BN_TEN
).toEqual(new BN(10));
});
it('BN_HUNDRED equals 100', (): void => {
expect(
BN_HUNDRED
).toEqual(new BN(100));
});
it('BN_THOUSAND equals 1000', (): void => {
expect(
BN_THOUSAND
).toEqual(new BN(1000));
});
});
+43
View File
@@ -0,0 +1,43 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
/**
* @name BN_ZERO
* @summary BN constant for 0.
*/
const BN_ZERO: BN = new BN(0);
/**
* @name BN_ONE
* @summary BN constant for 1.
*/
const BN_ONE: BN = new BN(1);
/**
* @name BN_TEN
* @summary BN constant for 10.
*/
const BN_TEN: BN = new BN(10);
/**
* @name BN_HUNDRED
* @summary BN constant for 100.
*/
const BN_HUNDRED: BN = new BN(100);
/**
* @name BN_THOUSAND
* @summary BN constant for 1000.
*/
const BN_THOUSAND: BN = new BN(1000);
export {
BN_ZERO,
BN_ONE,
BN_TEN,
BN_HUNDRED,
BN_THOUSAND
};
+1
View File
@@ -6,6 +6,7 @@
* @summary Utility methods to convert to and from `BN` objects
*/
export * from './consts';
export { default as bnFromHex } from './fromHex';
export { default as bnMax } from './max';
export { default as bnMin } from './min';
+20
View File
@@ -0,0 +1,20 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { extractTime } from '.';
describe('extractTime', (): void => {
const milliseconds = 1e9 + 123;
it('extracts time components correctly', (): void => {
expect(extractTime(milliseconds))
.toEqual({
days: 11,
hours: 13,
milliseconds: 123,
minutes: 46,
seconds: 40
});
});
});
+73
View File
@@ -0,0 +1,73 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Time } from './types';
const HRS = 60 * 60;
const DAY = HRS * 24;
/**
* @name addTime
* @summary Add together two Time arrays
*/
function addTime (a: Time, b: Time): Time {
return {
days: a.days + b.days,
hours: a.hours + b.hours,
milliseconds: a.milliseconds + b.milliseconds,
minutes: a.minutes + b.minutes,
seconds: a.seconds + b.seconds
};
}
const ZERO = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
/**
* @name extractTime
* @summary Convert a quantity of seconds to Time array representing accumulated {days, minutes, hours, seconds, milliseconds}
* @example
* <BR>
*
* ```javascript
* import { extractTime } from '@polkadot/util';
*
* const { days, minutes, hours, seconds, milliseconds } = extractTime(6000); // 0, 0, 10, 0, 0
* ```
*/
export default function extractTime (milliseconds?: number): Time {
if (!milliseconds) {
return ZERO;
} else if (milliseconds < 1000) {
return { ...ZERO, milliseconds };
}
const secs = milliseconds / 1000;
if (secs < 60) {
const seconds = Math.floor(secs);
return addTime({ ...ZERO, seconds }, extractTime(milliseconds - (seconds * 1000)));
}
const mins = secs / 60;
if (mins < 60) {
const minutes = Math.floor(mins);
return addTime({ ...ZERO, minutes }, extractTime(milliseconds - (minutes * 60 * 1000)));
}
const hrs = mins / 60;
if (hrs < 24) {
const hours = Math.floor(hrs);
return addTime({ ...ZERO, hours }, extractTime(milliseconds - (hours * HRS * 1000)));
}
const days = Math.floor(hrs / 24);
return addTime({ ...ZERO, days }, extractTime(milliseconds - (days * DAY * 1000)));
}
+1
View File
@@ -10,6 +10,7 @@ import './polyfill';
export { default as assert } from './assert';
export { default as detectPackage } from './detectPackage';
export { default as extractTime } from './extractTime';
export { default as logger } from './logger';
export { default as promisify } from './promisify';
+8
View File
@@ -40,3 +40,11 @@ export type BnList = {
0: BN;
1: BN;
} & BN[];
export interface Time {
days: number;
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
}