4390 - [UTCTimeFormat] Convert to ES6 Plugin (#4412)

* 4390 - [UTCTimeFormat] Convert to ES6 Plugin

* Added test for the plugin

* change the key propery name from ISO_KEY to UTC_KEY

Co-authored-by: John Hill <john.c.hill@nasa.gov>
Co-authored-by: Jamie V <jamie.j.vigliotta@nasa.gov>
This commit is contained in:
Mariusz Rosinski 2021-12-06 21:29:05 +01:00 committed by GitHub
parent 749e84611e
commit 420edb75f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 168 additions and 55 deletions

View File

@ -22,14 +22,11 @@
define([
"./src/FormatProvider",
"./src/UTCTimeFormat",
"./src/DurationFormat"
], function (
FormatProvider,
UTCTimeFormat,
DurationFormat
) {
return {
name: "platform/commonUI/formats",
definition: {
@ -47,10 +44,6 @@ define([
}
],
"formats": [
{
"key": "utc",
"implementation": UTCTimeFormat
},
{
"key": "duration",
"implementation": DurationFormat

View File

@ -289,6 +289,7 @@ define([
this.install(this.plugins.ObjectInterceptors());
this.install(this.plugins.NonEditableFolder());
this.install(this.plugins.DeviceClassifier());
this.install(this.plugins.UTCTimeFormat());
}
MCT.prototype = Object.create(EventEmitter.prototype);

View File

@ -20,44 +20,38 @@
* at runtime from the About dialog for additional information.
*****************************************************************************/
define([
'moment'
], function (
moment
) {
const DATE_FORMAT = "YYYY-MM-DD HH:mm:ss.SSS";
const DATE_FORMATS = [
DATE_FORMAT,
DATE_FORMAT + "Z",
"YYYY-MM-DD HH:mm:ss",
"YYYY-MM-DD HH:mm",
"YYYY-MM-DD"
];
import moment from 'moment';
/**
* @typedef Scale
* @property {number} min the minimum scale value, in ms
* @property {number} max the maximum scale value, in ms
*/
/**
* Formatter for UTC timestamps. Interprets numeric values as
* milliseconds since the start of 1970.
*
* @implements {Format}
* @constructor
* @memberof platform/commonUI/formats
*/
function UTCTimeFormat() {
this.key = "utc";
/**
* Formatter for UTC timestamps. Interprets numeric values as
* milliseconds since the start of 1970.
*
* @implements {Format}
* @constructor
* @memberof platform/commonUI/formats
*/
export default class UTCTimeFormat {
constructor() {
this.key = 'utc';
this.DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss.SSS';
this.DATE_FORMATS = [
this.DATE_FORMAT,
this.DATE_FORMAT + 'Z',
'YYYY-MM-DD HH:mm:ss',
'YYYY-MM-DD HH:mm',
'YYYY-MM-DD'
];
}
/**
* @param {string} formatString
* @returns the value of formatString if the value is a string type and exists in the DATE_FORMATS array; otherwise the DATE_FORMAT value.
*/
function validateFormatString(formatString) {
return typeof formatString === 'string' && DATE_FORMATS.includes(formatString) ? formatString : DATE_FORMAT;
validateFormatString(formatString) {
return typeof formatString === 'string'
&& this.DATE_FORMATS.includes(formatString)
? formatString
: this.DATE_FORMAT;
}
/**
@ -68,27 +62,26 @@ define([
* formatted values will be returned. Where a value could not be formatted, `undefined` will be returned at its position
* in the array.
*/
UTCTimeFormat.prototype.format = function (value, formatString) {
format(value, formatString) {
if (value !== undefined) {
const format = validateFormatString(formatString);
const format = this.validateFormatString(formatString);
const utc = moment.utc(value);
return moment.utc(value).format(format) + (formatString ? '' : 'Z');
return utc.format(format) + (formatString ? '' : 'Z');
} else {
return value;
}
};
}
UTCTimeFormat.prototype.parse = function (text) {
parse(text) {
if (typeof text === 'number') {
return text;
}
return moment.utc(text, DATE_FORMATS).valueOf();
};
return moment.utc(text, this.DATE_FORMATS).valueOf();
}
UTCTimeFormat.prototype.validate = function (text) {
return moment.utc(text, DATE_FORMATS, true).isValid();
};
return UTCTimeFormat;
});
validate(text) {
return moment.utc(text, this.DATE_FORMATS, true).isValid();
}
}

View File

@ -0,0 +1,29 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2021, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
import UTCTimeFormat from './UTCTimeFormat';
export default function () {
return function install(openmct) {
openmct.telemetry.addFormat(new UTCTimeFormat());
};
}

View File

@ -0,0 +1,94 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2021, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
import UTCTimeFormat from './UTCTimeFormat.js';
describe('the plugin', () => {
const UTC_KEY = 'utc';
const JUNK = 'junk';
const MOON_LANDING_TIMESTAMP = -14256000000;
const MOON_LANDING_DEFAULT_FORMAT = '1969-07-20 00:00:00.000Z';
const MOON_LANDING_FORMATTED_DATES = [
'1969-07-20 00:00:00.000',
'1969-07-20 00:00:00.000+00:00',
'1969-07-20 00:00:00',
'1969-07-20 00:00',
'1969-07-20'
];
let utcFormatter;
beforeEach(() => {
utcFormatter = new UTCTimeFormat();
});
describe('creates a new UTC based formatter', function () {
it("with the key 'utc'", () => {
expect(utcFormatter.key).toBe(UTC_KEY);
});
it('that will format a timestamp in UTC Standard Date', () => {
//default format
expect(utcFormatter.format(MOON_LANDING_TIMESTAMP)).toBe(
MOON_LANDING_DEFAULT_FORMAT
);
//possible formats
const formattedDates = utcFormatter.DATE_FORMATS.map((format) =>
utcFormatter.format(MOON_LANDING_TIMESTAMP, format)
);
expect(formattedDates).toEqual(MOON_LANDING_FORMATTED_DATES);
});
it('that will parse an UTC Standard Date into milliseconds', () => {
//default format
expect(utcFormatter.parse(MOON_LANDING_DEFAULT_FORMAT)).toBe(
MOON_LANDING_TIMESTAMP
);
//possible formats
const parsedDates = MOON_LANDING_FORMATTED_DATES.map((format) =>
utcFormatter.parse(format)
);
parsedDates.forEach((v) => expect(v).toEqual(MOON_LANDING_TIMESTAMP));
});
it('that will validate correctly', () => {
//default format
expect(utcFormatter.validate(MOON_LANDING_DEFAULT_FORMAT)).toBe(
true
);
//possible formats
const validatedFormats = MOON_LANDING_FORMATTED_DATES.map((date) =>
utcFormatter.validate(date)
);
validatedFormats.forEach((v) => expect(v).toBe(true));
//junk
expect(utcFormatter.validate(JUNK)).toBe(false);
});
});
});

View File

@ -72,8 +72,9 @@ define([
'./timeline/plugin',
'./hyperlink/plugin',
'./clock/plugin',
'./timer/plugin',
'./DeviceClassifier/plugin'
'./DeviceClassifier/plugin',
'./UTCTimeFormat/plugin',
'./timer/plugin'
], function (
_,
UTCTimeSystem,
@ -126,8 +127,9 @@ define([
Timeline,
Hyperlink,
Clock,
Timer,
DeviceClassifier
DeviceClassifier,
UTCTimeFormat,
Timer
) {
const bundleMap = {
LocalStorage: 'platform/persistence/local',
@ -236,6 +238,7 @@ define([
plugins.Clock = Clock.default;
plugins.Timer = Timer.default;
plugins.DeviceClassifier = DeviceClassifier.default;
plugins.UTCTimeFormat = UTCTimeFormat.default;
return plugins;
});