More improvements in the built-in `isTimestamp` function (see #847)
This commit is contained in:
parent
7be97ceffb
commit
51410c363f
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
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
|
||||
}
|
||||
|
||||
if (typeof field === 'string') {
|
||||
const fieldUpper = field.toUpperCase()
|
||||
|
||||
return TIMESTAMP_FIELDS.some(search => fieldUpper.indexOf(search) !== -1)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue