From 5c7e1e6fe9776dc641bed8a34af799239d0253a7 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 11 Dec 2019 12:08:31 +0100 Subject: [PATCH] Changed the behavior of `timestampTag` to fallback on the built-in rules when the function does not return a boolean. See #856. --- HISTORY.md | 4 +++- docs/api.md | 8 ++++++-- src/js/Node.js | 14 +++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index fbd1422..6bf1bcd 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,10 +3,12 @@ https://github.com/josdejong/jsoneditor -## 2019-12-11, version 7.6.0 +## 2019-12-11, version 8.0.0 - Implemented option `timestampFormat` which allows customizing the formatting of timestamp tags. See also option `timestampTag`. Thanks @smallp. +- Changed the behavior of `timestampTag` to fallback on the built-in rules when + the function does not return a boolean. See #856. - 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 diff --git a/docs/api.md b/docs/api.md index e3a2038..8e16c43 100644 --- a/docs/api.md +++ b/docs/api.md @@ -476,8 +476,12 @@ Constructs a new JSONEditor. 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 - parameter: + this function returns `true`, and no timestamp is displayed when the function + returns `false`. When the function returns a non-boolean value like `null` + or `undefined`, JSONEditor will fallback on the built-in rules to determine + whether or not to show a timestamp. + + The function is invoked with an object as first parameter: ``` { diff --git a/src/js/Node.js b/src/js/Node.js index ef669ec..6345a2e 100644 --- a/src/js/Node.js +++ b/src/js/Node.js @@ -1961,18 +1961,22 @@ export class Node { const timestampTag = this.editor.options.timestampTag if (typeof timestampTag === 'function') { - return timestampTag({ + const result = timestampTag({ field: this.field, value: this.value, path: this.getPath() }) - } - if (timestampTag === true) { + if (typeof result === 'boolean') { + return result + } else { + return isTimestamp(this.field, this.value) + } + } else if (timestampTag === true) { return isTimestamp(this.field, this.value) + } else { + return false } - - return false } /**