From 03fa90bd7197ad97c353b65ffe30753bbce1c834 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 11 Dec 2019 11:51:48 +0100 Subject: [PATCH] Revert using field names to determine whether a value is a timestamp (see #847, #856) --- HISTORY.md | 4 ++++ docs/api.md | 9 ++++----- src/js/util.js | 26 +++----------------------- test/util.test.js | 8 -------- 4 files changed, 11 insertions(+), 36 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 09af556..9fe47e4 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,6 +7,10 @@ https://github.com/josdejong/jsoneditor - Implemented option `timestampFormat` which allows customizing the formatting of timestamp tags. See also option `timestampTag`. Thanks @smallp. +- Reverted the heuristics introduced in `v7.3.0` to check whether some field + contains a timestamp based on the field name, because they can give wrong + timestamps in case of values in seconds instead of the assumed milliseconds + (see #847, #856). ## 2019-12-08, version 7.5.0 diff --git a/docs/api.md b/docs/api.md index 98a482f..e3a2038 100644 --- a/docs/api.md +++ b/docs/api.md @@ -472,10 +472,8 @@ Constructs a new JSONEditor. If `true` (default), a tag with the date/time of a timestamp is displayed right from values containing a timestamp. By default, a value is - considered a timestamp when it is (a) an integer number with a value larger - than Jan 1th 2000, `946684800000`, or (b) it's field name contains any of the - following strings (case insensitive): `'date'`, `'time'`, `'created'`, - `'updated'`, `'deleted'`. + considered a timestamp when it is an integer number with a value larger + than Jan 1th 2000, `946684800000`. When `timestampTag` a is a function, a timestamp tag will be displayed when this function returns `true`. The function is invoked with an object as first @@ -490,7 +488,8 @@ Constructs a new JSONEditor. ``` Whether a value is a timestamp can be determined implicitly based on - the `value`, or explicitly based on `field` or `path`. + the `value`, or explicitly based on `field` or `path`. You can for example + test whether a field name contains a string like: `'date'` or `'time'`. Example: diff --git a/src/js/util.js b/src/js/util.js index a4bf66d..9202031 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -1355,38 +1355,18 @@ export function parseString (str) { return str } -const TIMESTAMP_FIELDS = [ - 'DATE', - 'TIME', - 'CREATED', - 'UPDATED', - 'DELETED' -] - /** - * Test whether some field contains a timestamp. This is based - * on the number being large has a naming like "date" or "time" + * Test whether some field contains a timestamp in milliseconds after the year 2000. * @param {string} field * @param {number} value * @return {boolean} */ export function isTimestamp (field, value) { - if (typeof value === 'number' && + return typeof value === 'number' && value > YEAR_2000 && isFinite(value) && Math.floor(value) === value && - !isNaN(new Date(value).valueOf()) - ) { - return true - } - - if (typeof field === 'string') { - const fieldUpper = field.toUpperCase() - - return TIMESTAMP_FIELDS.some(search => fieldUpper.indexOf(search) !== -1) - } - - return false + !isNaN(new Date(value).valueOf()); } /** diff --git a/test/util.test.js b/test/util.test.js index 946997e..fcbf205 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -509,14 +509,6 @@ describe('util', () => { it('should test whether a field is a timestamp', () => { assert.strictEqual(isTimestamp('foo', 1574809200000), true) assert.strictEqual(isTimestamp('foo', 1574809200000.2), false) - assert.strictEqual(isTimestamp('dateCreated', 0), true) - assert.strictEqual(isTimestamp('updatedAt', 0), true) - assert.strictEqual(isTimestamp('deletedAt', 0), true) - assert.strictEqual(isTimestamp('DATE', 0), true) - assert.strictEqual(isTimestamp('TIMESTAMP', 0), true) - assert.strictEqual(isTimestamp('timestamp', 0), true) - assert.strictEqual(isTimestamp('hello', 0), false) - assert.strictEqual(isTimestamp('TIM', 0), false) }) // TODO: thoroughly test all util methods