From 681cda22b3ef636c95135d84189f5ec1424a6838 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 15 Nov 2018 14:51:51 +0100 Subject: [PATCH] Moment extends Date (#400) * Moment extends Date * FIx tests * Add comments * Use toISOString * Use toISOString --- packages/types/src/Moment.spec.ts | 18 +++++++++++++---- packages/types/src/Moment.ts | 25 +++++++++--------------- packages/types/src/codec/Compact.spec.ts | 4 ++-- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/types/src/Moment.spec.ts b/packages/types/src/Moment.spec.ts index 4bf8814927..ae27a643b3 100644 --- a/packages/types/src/Moment.spec.ts +++ b/packages/types/src/Moment.spec.ts @@ -4,6 +4,7 @@ import BN from 'bn.js'; +import { CodecTo } from './types'; import Moment from './Moment'; import U64 from './U64'; @@ -11,25 +12,34 @@ describe('Moment', () => { describe('decode', () => { const testDecode = (type: string, input: Date | Moment | U64 | number, expected: string | number, toJSON: boolean = false) => it(`can decode from ${type}`, () => { - expect(new Moment(input)[toJSON ? 'toJSON' : 'toString']()).toBe(expected); + expect(new Moment(input)[toJSON ? 'toJSON' : 'toISOString']()).toBe(expected); }); - testDecode('Date', new Date(1537968546280), '2018-09-26T13:29:07.000Z'); + testDecode('Date', new Date(1537968546280), '2018-09-26T13:29:06.280Z'); testDecode('Moment', new Moment(1234), 1234, true); testDecode('number', 1234, 1234, true); testDecode('U64', new U64(69), 69, true); }); describe('encode', () => { - const testEncode = (to: 'toBn' | 'toJSON' | 'toString' | 'toNumber' | 'toU8a', expected: BN | number | string | Uint8Array) => + const testEncode = (to: 'toBn' | 'toISOString' | 'toNumber' | CodecTo, expected: BN | number | string | Uint8Array) => it(`can encode ${to}`, () => { expect(new Moment(421)[to]()).toEqual(expected); }); testEncode('toBn', new BN(421)); testEncode('toJSON', 421); - testEncode('toString', '1970-01-01T00:07:01.000Z'); + testEncode('toISOString', '1970-01-01T00:07:01.000Z'); + testEncode('toNumber', 421); testEncode('toU8a', Uint8Array.from([165, 1, 0, 0, 0, 0, 0, 0])); + + it(`can encode toString`, () => { + expect( + new Moment(421) + .toString() + .startsWith('Thu Jan 01 1970') // The time depends on the timezone this test is run in + ).toBe(true); + }); }); }); diff --git a/packages/types/src/Moment.ts b/packages/types/src/Moment.ts index 1a7011abed..7057e0fc6f 100644 --- a/packages/types/src/Moment.ts +++ b/packages/types/src/Moment.ts @@ -5,9 +5,8 @@ import BN from 'bn.js'; import { bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util'; -import Base from './codec/Base'; import { UIntBitLength } from './codec/UInt'; -import { AnyNumber } from './types'; +import { AnyNumber, Codec } from './types'; const BITLENGTH: UIntBitLength = 64; @@ -15,18 +14,20 @@ const BITLENGTH: UIntBitLength = 64; // second precicion (aligning with Rust), so any numbers passed an/out are always // per-second. For any encoding/decoding the 1000 multiplier would be applied to // get it in line with JavaScript formats -export default class Moment extends Base { +export default class Moment extends Date implements Codec { + public raw: Date; // FIXME Remove this once we convert all types out of Base + constructor (value: Moment | Date | AnyNumber = 0) { super( Moment.decodeMoment(value) ); + + this.raw = this; } static decodeMoment (value: Moment | Date | AnyNumber): Date { - if (value instanceof Moment) { - return value.raw; - } else if (value instanceof Date) { - return new Date(Math.ceil(value.getTime() / 1000) * 1000); + if (value instanceof Date) { + return value; } else if (isU8a(value)) { value = u8aToBn(value.subarray(0, BITLENGTH / 8), true); } else if (isString(value)) { @@ -46,10 +47,6 @@ export default class Moment extends Base { return BITLENGTH / 8; } - getTime (): number { - return this.raw.getTime(); - } - toHex (): string { return bnToHex(this.toBn(), BITLENGTH); } @@ -62,15 +59,11 @@ export default class Moment extends Base { return bnToU8a(this.toNumber(), BITLENGTH, true); } - toString (): string { - return this.raw.toISOString(); - } - toBn (): BN { return new BN(this.toNumber()); } toNumber (): number { - return Math.ceil(this.raw.getTime() / 1000); + return Math.ceil(this.getTime() / 1000); } } diff --git a/packages/types/src/codec/Compact.spec.ts b/packages/types/src/codec/Compact.spec.ts index 28c8ad904b..432bce7ff8 100644 --- a/packages/types/src/codec/Compact.spec.ts +++ b/packages/types/src/codec/Compact.spec.ts @@ -120,8 +120,8 @@ describe('Compact', () => { it('constructs properly via number as Moment', () => { expect( - new Compact(Moment, 1537968546).toString() - ).toEqual('2018-09-26T13:29:06.000Z'); + new Compact(Moment, 1537968546).toString().startsWith('Wed Sep 26 2018') // The time depends on the timezone this test is run in + ).toBe(true); }); });