Revert using field names to determine whether a value is a timestamp (see #847, #856)

This commit is contained in:
jos 2019-12-11 11:51:48 +01:00
parent a55977830f
commit 03fa90bd71
4 changed files with 11 additions and 36 deletions

View File

@ -7,6 +7,10 @@ https://github.com/josdejong/jsoneditor
- Implemented option `timestampFormat` which allows customizing the formatting - Implemented option `timestampFormat` which allows customizing the formatting
of timestamp tags. See also option `timestampTag`. Thanks @smallp. 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 ## 2019-12-08, version 7.5.0

View File

@ -472,10 +472,8 @@ Constructs a new JSONEditor.
If `true` (default), a tag with the date/time of a timestamp is displayed 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 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 considered a timestamp when it is an integer number with a value larger
than Jan 1th 2000, `946684800000`, or (b) it's field name contains any of the than Jan 1th 2000, `946684800000`.
following strings (case insensitive): `'date'`, `'time'`, `'created'`,
`'updated'`, `'deleted'`.
When `timestampTag` a is a function, a timestamp tag will be displayed when 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 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 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: Example:

View File

@ -1355,38 +1355,18 @@ export function parseString (str) {
return str return str
} }
const TIMESTAMP_FIELDS = [
'DATE',
'TIME',
'CREATED',
'UPDATED',
'DELETED'
]
/** /**
* Test whether some field contains a timestamp. This is based * Test whether some field contains a timestamp in milliseconds after the year 2000.
* on the number being large has a naming like "date" or "time"
* @param {string} field * @param {string} field
* @param {number} value * @param {number} value
* @return {boolean} * @return {boolean}
*/ */
export function isTimestamp (field, value) { export function isTimestamp (field, value) {
if (typeof value === 'number' && return typeof value === 'number' &&
value > YEAR_2000 && value > YEAR_2000 &&
isFinite(value) && isFinite(value) &&
Math.floor(value) === value && Math.floor(value) === value &&
!isNaN(new Date(value).valueOf()) !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
} }
/** /**

View File

@ -509,14 +509,6 @@ describe('util', () => {
it('should test whether a field is a timestamp', () => { it('should test whether a field is a timestamp', () => {
assert.strictEqual(isTimestamp('foo', 1574809200000), true) assert.strictEqual(isTimestamp('foo', 1574809200000), true)
assert.strictEqual(isTimestamp('foo', 1574809200000.2), false) 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 // TODO: thoroughly test all util methods