From d80870e83550fc4109d33f272ae6dfb9dc03b9aa Mon Sep 17 00:00:00 2001 From: Eric Tuvesson Date: Mon, 23 Sep 2024 21:11:32 +0200 Subject: [PATCH] fix(runtime): Passing in invalid date to "Date To String" node causes node scope to fail (#75) --- .../src/nodes/std-library/datetostring.js | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/packages/noodl-runtime/src/nodes/std-library/datetostring.js b/packages/noodl-runtime/src/nodes/std-library/datetostring.js index 93e0b3e..edf50ce 100644 --- a/packages/noodl-runtime/src/nodes/std-library/datetostring.js +++ b/packages/noodl-runtime/src/nodes/std-library/datetostring.js @@ -31,8 +31,6 @@ const DateToStringNode = { this._internal.currentInput = _value; this._format(); - this.flagOutputDirty('currentValue'); - this.sendSignalOnOutput('inputChanged'); } } }, @@ -49,30 +47,45 @@ const DateToStringNode = { type: 'signal', displayName: 'Date Changed', group: 'Signals' + }, + onError: { + type: 'signal', + displayName: 'Invalid Date', + group: 'Signals' } }, methods: { _format() { - const t = this._internal.currentInput; - const format = this._internal.formatString; - const date = ('0' + t.getDate()).slice(-2); - const month = ('0' + (t.getMonth() + 1)).slice(-2); - const monthShort = new Intl.DateTimeFormat('en-US', { month: 'short' }).format(t); - const year = t.getFullYear(); - const yearShort = year.toString().substring(2); - const hours = ('0' + t.getHours()).slice(-2); - const minutes = ('0' + t.getMinutes()).slice(-2); - const seconds = ('0' + t.getSeconds()).slice(-2); + try { + const t = this._internal.currentInput; + const format = this._internal.formatString; + const date = ('0' + t.getDate()).slice(-2); + const month = ('0' + (t.getMonth() + 1)).slice(-2); + const monthShort = new Intl.DateTimeFormat('en-US', { month: 'short' }).format(t); + const year = t.getFullYear(); + const yearShort = year.toString().substring(2); + const hours = ('0' + t.getHours()).slice(-2); + const minutes = ('0' + t.getMinutes()).slice(-2); + const seconds = ('0' + t.getSeconds()).slice(-2); - this._internal.dateString = format - .replace(/\{date\}/g, date) - .replace(/\{month\}/g, month) - .replace(/\{monthShort\}/g, monthShort) - .replace(/\{year\}/g, year) - .replace(/\{yearShort\}/g, yearShort) - .replace(/\{hours\}/g, hours) - .replace(/\{minutes\}/g, minutes) - .replace(/\{seconds\}/g, seconds); + this._internal.dateString = format + .replace(/\{date\}/g, date) + .replace(/\{month\}/g, month) + .replace(/\{monthShort\}/g, monthShort) + .replace(/\{year\}/g, year) + .replace(/\{yearShort\}/g, yearShort) + .replace(/\{hours\}/g, hours) + .replace(/\{minutes\}/g, minutes) + .replace(/\{seconds\}/g, seconds); + } catch (error) { + // Set the output to be blank, makes it easier to handle. + this._internal.dateString = ''; + this.flagOutputDirty('onError'); + } + + // Flag that the value have changed + this.flagOutputDirty('currentValue'); + this.sendSignalOnOutput('inputChanged'); } } };