Moment extends Date (#400)

* Moment extends Date

* FIx tests

* Add comments

* Use toISOString

* Use toISOString
This commit is contained in:
Amaury Martiny
2018-11-15 14:51:51 +01:00
committed by GitHub
parent 185ecd6fde
commit 681cda22b3
3 changed files with 25 additions and 22 deletions
+14 -4
View File
@@ -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);
});
});
});
+9 -16
View File
@@ -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<Date> {
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<Date> {
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<Date> {
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);
}
}
+2 -2
View File
@@ -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);
});
});