@telemok/super-var
v0.0.16
Published
Javascript ES6 library for variable with assert, change event listener, clear state.
Downloads
16
Readme
super-var beta
https://www.npmjs.com/package/@telemok/super-var JS value with .set(), .get(), .assert(), and .addEventListener('change',(e)=>{}).
Features
- Add event listener to instantly view variable changes.
Installation:
- Create your NodeJs, Browser or Webview app.
- import { SuperVar, SuperBoolean } from "@telemok/super-var"
- let superVar = new SuperVar();
- ...
- superVar.destroy();
Examples:
- Parsing students database
import { valda } from "@telemok/valda"
import { SuperVar } from "@telemok/super-var"
let temperatureCelsius = new SuperVar();
temperatureCelsius.whyNotValue = (value) => {return valda.numberMinMax.whyNot(value, -64, 128);}
temperatureCelsius.addEventListener('change', ()=>{
let text = "???";
if(!temperatureCelsius.isClear())
text = temperatureCelsius.get() + "°";
document.getElementById("spanThemperature").innerText = text;
});
temperatureCelsius.addEventListener('change', ()=>{
if(!temperatureCelsius.isClear())
{
let t = temperatureCelsius.get();
if(t > 90)/*If temperature*/
fetch(`https://api.telegram.org/${token}/sendMessage?chat_id=${chatId}&text=overheat_${t}`);
/* then send message to Telegram */
}
});
function end() {
temperatureCelsius.destroy();//Will destroy all listeners
}
function receive(rawValue) {
rawValue = valda.integerMinMax.assert(rawValue, 0, 1023);
temperatureCelsius.set(rawValue / 16);//set data
}
- Assert and localStorage
Only for browser, localStorage don't work on Node.js, but window.localStorage can be installed for Node.js
import {nextUniqueVarName} from "@telemok/super-var"
let superVar = new SuperVar({
assert: (value)=>{
if(value > 0.9)
throw new RangeError(`Test assert: ${value} > 0.9`);
return value;
},
localStorageKey: "example_localStorage_testSuperVar",
});
console.log("value get = ",superVar.toString());
superVar.set(Math.random());
console.log("value set = ",superVar.toString());