More improvements in the built-in `isTimestamp` function (see #847)

This commit is contained in:
jos 2019-11-27 15:45:48 +01:00
parent 7be97ceffb
commit 51410c363f
5 changed files with 38 additions and 18 deletions

View File

@ -436,8 +436,9 @@ 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 number and it's field name contains any
of the following strings (case insensitive): `'date'`, `'time'`, `'created'`,
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'`.
When `timestampTag` a is a function, a timestamp tag will be displayed when

View File

@ -135,7 +135,7 @@ gulp.task('embed-json-worker', function (done) {
const workerDataUrl = 'data:application/javascript;base64,' + btoa(workerScript)
fs.writeFileSync(workerEmbeddedFile, 'module.exports = \'' + workerDataUrl + '\';\n')
fs.writeFileSync(workerEmbeddedFile, 'module.exports = \'' + workerDataUrl + '\'\n')
done()
})

View File

@ -18,7 +18,7 @@ import {
getAbsoluteTop,
getInnerText,
getType,
isTimestampFieldName,
isTimestamp,
isUrl,
isValidColor,
makeFieldTooltip,
@ -1954,7 +1954,7 @@ export class Node {
}
if (timestampTag === true) {
return isTimestampFieldName(this.field)
return isTimestamp(this.field, this.value)
}
return false

View File

@ -7,6 +7,7 @@ import jsonMap from 'json-source-map'
import { translate } from './i18n'
const MAX_ITEMS_FIELDS_COLLECTION = 10000
const YEAR_2000 = 946684800000
/**
* Parse JSON using the parser built-in in the browser.
@ -1363,14 +1364,29 @@ const TIMESTAMP_FIELDS = [
]
/**
* Test whether some field has a naming like "date" or "time"
* Test whether some field contains a timestamp. This is based
* on the number being large has a naming like "date" or "time"
* @param {string} field
* @param {number} value
* @return {boolean}
*/
export function isTimestampFieldName (field) {
const fieldUpper = field.toUpperCase()
export function isTimestamp (field, value) {
if (typeof value === 'number' &&
value > YEAR_2000 &&
isFinite(value) &&
Math.floor(value) === value &&
!isNaN(new Date(value).valueOf())
) {
return true
}
return TIMESTAMP_FIELDS.some(search => fieldUpper.indexOf(search) !== -1)
if (typeof field === 'string') {
const fieldUpper = field.toUpperCase()
return TIMESTAMP_FIELDS.some(search => fieldUpper.indexOf(search) !== -1)
}
return false
}
/**

View File

@ -6,7 +6,8 @@ import {
get,
getChildPaths,
getIndexForPosition,
isObject, isTimestampFieldName,
isObject,
isTimestamp,
limitCharacters,
makeFieldTooltip,
parsePath,
@ -477,14 +478,16 @@ describe('util', () => {
})
it('should test whether a field is a timestamp', () => {
assert.strictEqual(isTimestampFieldName('dateCreated'), true)
assert.strictEqual(isTimestampFieldName('updatedAt'), true)
assert.strictEqual(isTimestampFieldName('deletedAt'), true)
assert.strictEqual(isTimestampFieldName('DATE'), true)
assert.strictEqual(isTimestampFieldName('TIMESTAMP'), true)
assert.strictEqual(isTimestampFieldName('timestamp'), true)
assert.strictEqual(isTimestampFieldName('hello'), false)
assert.strictEqual(isTimestampFieldName('TIM'), false)
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