Skip to main content

Characters Count

A simple Node that counts the number of occurrences of a character in string. This Node has no View elements.

Features used in this example:

  • Read and write values to properties using context.get() and context.set().
  • Subscribe to changes on specific properties.
Note

This example does not have any visual HTML element so frameworks usage is not demostrated in this example.

Example

 

manifest.yaml
name: Characters Occurance
description: Count the occurrence of a character in certain text.
icon: mdi:counter
environments: browser
sandbox: browser.shadow
properties.yaml
string:
datatype: text
direction: input
required: true
character:
datatype: text
direction: input
required: true
count:
description: The number of occurrence of a character in string.
datatype: number
direction: output
src/main.jsx
let char = context.get('character');
let str = context.get('string');

function countCharacters() {
let num = 0;
let pos = str.indexOf(char);
while (pos > -1) {
pos = str.indexOf(char, pos + 1);
num++;
}
context.set('count', num);
}

context.subscribe('string', (v) => {
str = v;
countCharacters();
});

context.subscribe('character', (v) => {
char = v;
countCharacters();
});