From a615f7ab91b81fc8bf724b301cd6a6fa2e818f29 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 20 Nov 2019 10:47:49 +0100 Subject: [PATCH] Fixed #847: allow customizing the rules determining whether a value is a timestamp or not --- HISTORY.md | 6 ++++++ docs/api.md | 11 ++++++++--- src/js/Node.js | 7 +------ src/js/treemode.js | 18 ++++++++++++++++++ src/js/util.js | 12 ++++++++++++ 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 856acb6..c48a71b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,12 @@ https://github.com/josdejong/jsoneditor +## not yet published, version 7.3.0 + +- Fixed #847: allow customizing the in rules determining whether a value + is a timestamp or not. + + ## 2019-10-27, version 7.2.1 - Fixed #826: editor not allowing indentation `0`. diff --git a/docs/api.md b/docs/api.md index a898635..aa8fe6c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -432,11 +432,16 @@ Constructs a new JSONEditor. } ``` -- `{boolean} timestampTag` +- `{boolean | function(value: any) -> boolean} timestampTag` If `true` (default), a tag with the date/time of a timestamp is displayed - right from timestamps. A value is considered a timestamp when it - has a value larger than Jan 1th 2000, `946684800000`. + right from values containing a timestamp. A value is considered a timestamp + when it is a number with value larger than Jan 1th 2000, `946684800000`. + + When `timestampTag` a is a function, a timestamp tag will be displayed for + values for which `timestampTag(value)` returns `true`. This way it is + possible to alter the default rules for determining whether a value + is a timestamp or not. - `{string} language` diff --git a/src/js/Node.js b/src/js/Node.js index 03b7f22..67e78e5 100644 --- a/src/js/Node.js +++ b/src/js/Node.js @@ -34,8 +34,6 @@ import { import { translate } from './i18n' import { DEFAULT_MODAL_ANCHOR } from './constants' -const YEAR_2000 = 946684800000 - /** * @constructor Node * Create a new Node @@ -1784,10 +1782,7 @@ export class Node { } // show date tag when value is a timestamp in milliseconds - if (this.editor.options.timestampTag && - typeof value === 'number' && - value > YEAR_2000 && - !isNaN(new Date(value).valueOf())) { + if (this.editor.showTimestampTag(value)) { if (!this.dom.date) { this.dom.date = document.createElement('div') this.dom.date.className = 'jsoneditor-date' diff --git a/src/js/treemode.js b/src/js/treemode.js index 3435233..f64c5d1 100644 --- a/src/js/treemode.js +++ b/src/js/treemode.js @@ -17,6 +17,7 @@ import { hasParentNode, improveSchemaError, isPromise, + isTimestamp, isValidValidationError, parse, removeClassName, @@ -1760,6 +1761,23 @@ treemode.getNodesByRange = function (start, end) { return serializableNodes } +/** + * Test whether to show a timestamp tag or not + * @param {*} value Any type of value + * @return {boolean} Returns true when the value is a timestamp + */ +treemode.showTimestampTag = function (value) { + if (typeof this.options.timestampTag === 'function') { + return this.options.timestampTag(value) + } + + if (this.options.timestampTag === true) { + return isTimestamp(value) + } + + return false +} + // define modes export const treeModeMixins = [ { diff --git a/src/js/util.js b/src/js/util.js index 2ae529a..94c3051 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -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. @@ -1354,6 +1355,17 @@ export function parseString (str) { return str } +/** + * Test whether some value contains a timestamp. + * @param {*} value Any type of value + * @return {boolean} Returns true when the value is a timestamp + */ +export function isTimestamp (value) { + return typeof value === 'number' && + value > YEAR_2000 && + !isNaN(new Date(value).valueOf()) +} + /** * Return a human readable document size * For example formatSize(7570718) outputs '7.2 MiB'