Merge pull request #19 from adafruit/tylerdcooper-patch-2

docs: compare text block
This commit is contained in:
Loren Norman 2025-08-15 12:22:53 -04:00 committed by GitHub
commit 0116c9726d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,47 +6,40 @@ export default {
colour: 180,
inputsInline: true,
primaryCategory: "Logic",
description: "Compare two chunks of text for equality, inequality, or inclusion.",
description: "Compare any two pieces of text or data to build conditional logic in your Actions. Perfect for creating if/then statements like 'if device status equals online', 'if user name is not guest', or 'if error message contains timeout'. Works with feed values, variables, user input, or any text-based data.",
connections: {
mode: "value",
output: "expression",
},
template: `%A %OP %B`,
inputs: {
A: {
description: "The left side of the comparison. Will be coerced to a string",
description: "The first value to compare (left side). Can be feed data, variable content, user input, or any text. Numbers and other data types will be automatically converted to text for comparison.",
check: "expression",
shadow: 'io_text'
},
B: {
description: "The right side of the comparison. Will be coerced to a string",
description: "The second value to compare (right side). Can be literal text like 'online', variable content, feed values, or any data you want to compare against the first input. Also automatically converted to text.",
check: "expression",
shadow: 'io_text'
},
},
fields: {
OP: {
description: "Select what kind of comparison to do:",
description: "Choose how to compare the two text inputs:",
options: [
['=', 'EQ', "Returns true if the the inputs are the same."],
['\u2260', 'NEQ', "Returns true if the inputs not the same."],
['includes', 'INC', "Returns true if input A includes input B."],
['=', 'EQ', "Exact match: Returns true only if both inputs are identical (e.g., 'online' = 'online' is true, but 'Online' = 'online' is false due to case sensitivity)."],
['\u2260', 'NEQ', "Not equal: Returns true if the inputs are different in any way (e.g., useful for 'if status is not offline' or 'if username is not empty' conditions)."],
['includes', 'INC', "Contains: Returns true if the first input contains the second input anywhere within it (e.g., 'sensor error timeout' includes 'error' would be true)."],
]
}
},
generators: {
json: (block, generator) => {
const
comparator = block.getFieldValue('OP'),
leftExp = generator.valueToCode(block, 'A', 0) || null,
rightExp = generator.valueToCode(block, 'B', 0) || null,
blockPayload = JSON.stringify({
textCompare: {
left: JSON.parse(leftExp),
@ -54,11 +47,9 @@ export default {
right: JSON.parse(rightExp),
},
})
return [ blockPayload, 0 ]
}
},
regenerators: {
json: (blockObject, helpers) => {
const
@ -70,7 +61,6 @@ export default {
A: helpers.expressionToBlock(left, { shadow: "io_text" }),
B: helpers.expressionToBlock(right, { shadow: "io_text" }),
}
return { type: 'text_compare', fields, inputs }
}
}