Skip to content

Commit

Permalink
feat(internal): add utility methods for dealing with dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Aug 9, 2017
1 parent 6c96e98 commit c40450b
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/internal/Util.ts
@@ -1,4 +1,14 @@
import {OnmsError} from '../api/OnmsError';

import {Address4, Address6} from 'ip-address';
import {Moment} from 'moment';

/** @hidden */
// tslint:disable-next-line
const moment = require('moment');

/** @hidden */
const dateFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZZ';

/**
* A utility class for random stuff.
Expand All @@ -19,4 +29,44 @@ export class Util {
}
return undefined;
}

/**
* Whether or not the passed object is already a date. (Either a [[Moment]] object, or
* a JavaScript [[Date]] object.)
*/
public static isDateObject(date: any) {
return moment.isMoment(date) || date instanceof Date;
}

/**
* Create a [[Moment]] from any form of date (JavaScript [[Date]], [[Moment]], or epoch).
* [[Moment]] dates in OpenNMS.js will always be converted internally to UTC to avoid time
* zone issues.
*/
public static toMoment(date: Date|Moment|string|number): Moment {
if (date === undefined || date === null) {
return undefined;
} else if (moment.isMoment(date)) {
return (date as Moment).utc();
} else if (typeof(date) === 'number' || date instanceof Date
|| typeof(date) === 'string' || date instanceof String) {
return moment(date).utc();
} else {
throw new OnmsError('Unable to parse type "' + typeof(date) + '" as a date.');
}
}

/**
* Create a date string from any form of date (JavaScript [[Date]], [[Moment]], or epoch).
* Dates in OpenNMS.js will always be converted internally to UTC before stringifying to
* avoid time zone issues.
*/
public static toDateString(date: Date|Moment|number) {
const ret = Util.toMoment(date);
if (ret) {
return ret.utc().format(dateFormat);
} else {
return undefined;
}
}
}
80 changes: 79 additions & 1 deletion test/internal/Util.spec.ts
@@ -1,7 +1,19 @@
declare const describe, beforeEach, it, expect;
declare const describe, beforeEach, it, expect, require;

import {Util} from '../../src/internal/Util';

import {Address4, Address6} from 'ip-address';
import {Moment} from 'moment';

/** @hidden */
// tslint:disable-next-line
const moment = require('moment');

/** @hidden */
const ARBITRARY_STRING = '2017-08-08T12:29:56.000+0000';

/** @hidden */
const ARBITRARY_EPOCH = 1502195396000;

describe('Util.toIPAddress()', () => {
it('undefined', () => {
Expand All @@ -23,3 +35,69 @@ describe('Util.toIPAddress()', () => {
expect(addr.isValid()).toEqual(true);
});
});

describe('Util.isDateObject()', () => {
it('undefined', () => {
expect(Util.isDateObject(undefined)).toEqual(false);
});
it('null', () => {
expect(Util.isDateObject(null)).toEqual(false);
});
it('moment(0)', () => {
expect(Util.isDateObject(moment(0))).toEqual(true);
});
it('0', () => {
expect(Util.isDateObject(0)).toEqual(false);
});
it('new Date()', () => {
expect(Util.isDateObject(new Date(ARBITRARY_EPOCH))).toEqual(true);
});
it(ARBITRARY_STRING, () => {
expect(Util.isDateObject(ARBITRARY_STRING)).toEqual(false);
});
it('2017-08-08T12:29:56Z', () => {
expect(Util.isDateObject('2017-08-08T12:29:56Z')).toEqual(false);
});
});

describe('Util.toMoment()', () => {
it('undefined', () => {
expect(Util.toMoment(undefined)).toBeUndefined();
});
it('null', () => {
expect(Util.toMoment(null)).toBeUndefined();
});
it('moment(0)', () => {
expect(Util.toMoment(moment(0)).valueOf()).toEqual(0);
});
it('0', () => {
expect(Util.toMoment(0).valueOf()).toEqual(0);
});
it('new Date()', () => {
expect(Util.toMoment(new Date(ARBITRARY_EPOCH)).valueOf()).toEqual(ARBITRARY_EPOCH);
});
it(ARBITRARY_STRING, () => {
expect(Util.toMoment(ARBITRARY_STRING).valueOf()).toEqual(ARBITRARY_EPOCH);
});
it('2017-08-08T12:29:56Z', () => {
expect(Util.toMoment('2017-08-08T12:29:56Z').valueOf()).toEqual(ARBITRARY_EPOCH);
});
});

describe('Util.toDateString()', () => {
it('undefined', () => {
expect(Util.toDateString(undefined)).toBeUndefined();
});
it('null', () => {
expect(Util.toDateString(null)).toBeUndefined();
});
it('moment(0)', () => {
expect(Util.toDateString(moment(0))).toEqual('1970-01-01T00:00:00.000+0000');
});
it('0', () => {
expect(Util.toDateString(0)).toEqual('1970-01-01T00:00:00.000+0000');
});
it('new Date()', () => {
expect(Util.toDateString(new Date(ARBITRARY_EPOCH))).toEqual(ARBITRARY_STRING);
});
});

0 comments on commit c40450b

Please sign in to comment.