mirror of
https://github.com/nasa/openmct.git
synced 2025-06-18 15:18:12 +00:00
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:
@ -22,14 +22,11 @@
|
|||||||
|
|
||||||
define([
|
define([
|
||||||
"./src/FormatProvider",
|
"./src/FormatProvider",
|
||||||
"./src/UTCTimeFormat",
|
|
||||||
"./src/DurationFormat"
|
"./src/DurationFormat"
|
||||||
], function (
|
], function (
|
||||||
FormatProvider,
|
FormatProvider,
|
||||||
UTCTimeFormat,
|
|
||||||
DurationFormat
|
DurationFormat
|
||||||
) {
|
) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: "platform/commonUI/formats",
|
name: "platform/commonUI/formats",
|
||||||
definition: {
|
definition: {
|
||||||
@ -47,10 +44,6 @@ define([
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"formats": [
|
"formats": [
|
||||||
{
|
|
||||||
"key": "utc",
|
|
||||||
"implementation": UTCTimeFormat
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"key": "duration",
|
"key": "duration",
|
||||||
"implementation": DurationFormat
|
"implementation": DurationFormat
|
||||||
|
@ -289,6 +289,7 @@ define([
|
|||||||
this.install(this.plugins.ObjectInterceptors());
|
this.install(this.plugins.ObjectInterceptors());
|
||||||
this.install(this.plugins.NonEditableFolder());
|
this.install(this.plugins.NonEditableFolder());
|
||||||
this.install(this.plugins.DeviceClassifier());
|
this.install(this.plugins.DeviceClassifier());
|
||||||
|
this.install(this.plugins.UTCTimeFormat());
|
||||||
}
|
}
|
||||||
|
|
||||||
MCT.prototype = Object.create(EventEmitter.prototype);
|
MCT.prototype = Object.create(EventEmitter.prototype);
|
||||||
|
@ -20,44 +20,38 @@
|
|||||||
* at runtime from the About dialog for additional information.
|
* at runtime from the About dialog for additional information.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
define([
|
import moment from 'moment';
|
||||||
'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"
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef Scale
|
* Formatter for UTC timestamps. Interprets numeric values as
|
||||||
* @property {number} min the minimum scale value, in ms
|
* milliseconds since the start of 1970.
|
||||||
* @property {number} max the maximum scale value, in ms
|
*
|
||||||
*/
|
* @implements {Format}
|
||||||
|
* @constructor
|
||||||
/**
|
* @memberof platform/commonUI/formats
|
||||||
* Formatter for UTC timestamps. Interprets numeric values as
|
*/
|
||||||
* milliseconds since the start of 1970.
|
export default class UTCTimeFormat {
|
||||||
*
|
constructor() {
|
||||||
* @implements {Format}
|
this.key = 'utc';
|
||||||
* @constructor
|
this.DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss.SSS';
|
||||||
* @memberof platform/commonUI/formats
|
this.DATE_FORMATS = [
|
||||||
*/
|
this.DATE_FORMAT,
|
||||||
function UTCTimeFormat() {
|
this.DATE_FORMAT + 'Z',
|
||||||
this.key = "utc";
|
'YYYY-MM-DD HH:mm:ss',
|
||||||
|
'YYYY-MM-DD HH:mm',
|
||||||
|
'YYYY-MM-DD'
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} formatString
|
* @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.
|
* @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) {
|
validateFormatString(formatString) {
|
||||||
return typeof formatString === 'string' && DATE_FORMATS.includes(formatString) ? formatString : DATE_FORMAT;
|
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
|
* formatted values will be returned. Where a value could not be formatted, `undefined` will be returned at its position
|
||||||
* in the array.
|
* in the array.
|
||||||
*/
|
*/
|
||||||
UTCTimeFormat.prototype.format = function (value, formatString) {
|
format(value, formatString) {
|
||||||
if (value !== undefined) {
|
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 {
|
} else {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
UTCTimeFormat.prototype.parse = function (text) {
|
parse(text) {
|
||||||
if (typeof text === 'number') {
|
if (typeof text === 'number') {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
return moment.utc(text, DATE_FORMATS).valueOf();
|
return moment.utc(text, this.DATE_FORMATS).valueOf();
|
||||||
};
|
}
|
||||||
|
|
||||||
UTCTimeFormat.prototype.validate = function (text) {
|
validate(text) {
|
||||||
return moment.utc(text, DATE_FORMATS, true).isValid();
|
return moment.utc(text, this.DATE_FORMATS, true).isValid();
|
||||||
};
|
}
|
||||||
|
}
|
||||||
return UTCTimeFormat;
|
|
||||||
});
|
|
29
src/plugins/UTCTimeFormat/plugin.js
Normal file
29
src/plugins/UTCTimeFormat/plugin.js
Normal 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());
|
||||||
|
};
|
||||||
|
}
|
94
src/plugins/UTCTimeFormat/pluginSpec.js
Normal file
94
src/plugins/UTCTimeFormat/pluginSpec.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -72,8 +72,9 @@ define([
|
|||||||
'./timeline/plugin',
|
'./timeline/plugin',
|
||||||
'./hyperlink/plugin',
|
'./hyperlink/plugin',
|
||||||
'./clock/plugin',
|
'./clock/plugin',
|
||||||
'./timer/plugin',
|
'./DeviceClassifier/plugin',
|
||||||
'./DeviceClassifier/plugin'
|
'./UTCTimeFormat/plugin',
|
||||||
|
'./timer/plugin'
|
||||||
], function (
|
], function (
|
||||||
_,
|
_,
|
||||||
UTCTimeSystem,
|
UTCTimeSystem,
|
||||||
@ -126,8 +127,9 @@ define([
|
|||||||
Timeline,
|
Timeline,
|
||||||
Hyperlink,
|
Hyperlink,
|
||||||
Clock,
|
Clock,
|
||||||
Timer,
|
DeviceClassifier,
|
||||||
DeviceClassifier
|
UTCTimeFormat,
|
||||||
|
Timer
|
||||||
) {
|
) {
|
||||||
const bundleMap = {
|
const bundleMap = {
|
||||||
LocalStorage: 'platform/persistence/local',
|
LocalStorage: 'platform/persistence/local',
|
||||||
@ -236,6 +238,7 @@ define([
|
|||||||
plugins.Clock = Clock.default;
|
plugins.Clock = Clock.default;
|
||||||
plugins.Timer = Timer.default;
|
plugins.Timer = Timer.default;
|
||||||
plugins.DeviceClassifier = DeviceClassifier.default;
|
plugins.DeviceClassifier = DeviceClassifier.default;
|
||||||
|
plugins.UTCTimeFormat = UTCTimeFormat.default;
|
||||||
|
|
||||||
return plugins;
|
return plugins;
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user