diff --git a/eslint.config.mjs b/eslint.config.mjs index d72b451ff..b17f39e79 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -21,6 +21,7 @@ export default antfu( */ markdown: 'prettier', }, + ignores: ['examples/**'], }, { rules: { diff --git a/examples/function/dynamicSwitch.js b/examples/function/dynamicSwitch.js index ea0de0e15..1791e04a5 100644 --- a/examples/function/dynamicSwitch.js +++ b/examples/function/dynamicSwitch.js @@ -1,21 +1,18 @@ /** * @description: Simulated dynamic switch command - * @param {string | object} value - Payload - * @param {string} msgType - Message type, value is 'received' or 'publish' + * @param {string} message - Message payload + * @param {string} messageType - Message type, value is 'received' or 'publish' * @param {number} index - Index of the message, valid only when script is used in the publish message and timed message is enabled - * @return {object} - Return two commands alternately, { "command": "on" } or { "command": "off" } + * @return {string} - Return two commands alternately, { "command": "on" } or { "command": "off" } */ -function handlePayload(value, msgType, index) { - let _value = value - if (typeof value === 'string') { - _value = JSON.parse(value) - } +function handlePayload(message, messageType, index) { + let _message = JSON.parse(message || '{}') if (index % 2 === 0) { - _value.command = 'on' + _message.command = 'on' } else { - _value.command = 'off' + _message.command = 'off' } - return JSON.stringify(_value, null, 2) + return JSON.stringify(_message, null, 2) } -execute(handlePayload) +return execute(handlePayload) diff --git a/examples/function/tempAndHum.js b/examples/function/tempAndHum.js index 2f845c581..b2817c19e 100644 --- a/examples/function/tempAndHum.js +++ b/examples/function/tempAndHum.js @@ -1,21 +1,17 @@ -/** - * Simulated temperature and humidity reporting - * @return Return a simulated temperature and humidity JSON data - { "temperature": 23, "humidity": 40 } - * @param value, MQTT Payload - {} - */ - function random(min, max) { return Math.round(Math.random() * (max - min)) + min } -function handlePayload(value) { - let _value = value - if (typeof value === 'string') { - _value = JSON.parse(value) - } - _value.temperature = random(10, 30) - _value.humidity = random(20, 40) - return JSON.stringify(_value, null, 2) +/** + * @description: Simulated dynamic switch command + * @param {string} message - Message payload + * @return {string} - Return a simulated temperature and humidity data - { "temperature": 23, "humidity": 40 } + */ +function handlePayload(message) { + let _message = JSON.parse(message || '{}') + _message.temperature = random(10, 30) + _message.humidity = random(20, 40) + return JSON.stringify(_message, null, 2) } -execute(handlePayload) +return execute(handlePayload) diff --git a/examples/function/timestamp.js b/examples/function/timestamp.js index c3508ecdb..f9e686412 100644 --- a/examples/function/timestamp.js +++ b/examples/function/timestamp.js @@ -1,18 +1,17 @@ /** - * Convert timestamp to normal time. - * @return Return the UTC time - { "time": "2020-12-17 14:18:07" } - * @param value, MQTT Payload - { "time": 1608185887 } + * Convert timestamp to normal time + * @param {string} message - Message payload - { "time": 1608185887 } + * @return {string} - Return the message with the time converted to normal time - { "time": "17/12/2020, 06:18:07" } */ - -function handleTimestamp(value) { - let _value = value - if (typeof value === 'string') { - _value = JSON.parse(value) - } - // East Eight District needs an additional 8 hours - const date = new Date(_value.time * 1000 + 8 * 3600 * 1000) - _value.time = date.toJSON().substr(0, 19).replace('T', ' ') - return JSON.stringify(_value, null, 2) +function handlePayload(message) { + let _message = JSON.parse(message) + const date = new Date(_message.time * 1000) + _message.time = new Intl.DateTimeFormat('en-GB', { + timeZone: 'UTC', + dateStyle: 'short', + timeStyle: 'medium' + }).format(date) + return JSON.stringify(_message, null, 2) } -execute(handleTimestamp) +return execute(handlePayload)