Tools
userfull es6 mini library
What it does provide
In this library you can find utilities for
Examples
I think that the test suite, the dom utilities, and the observable
and reactive
interfaces deserve quick but in depth view
Test suite
This mini test class is super easy to use but work super well!
you can define your test scenario and if something goes wrong (ie if something is thrown, like an unhandled runtime error), then the test will fail and a fail message will be printed the console. Otherwise it will pass and a passed message in console will be printed.
import Test from "./test.js";
import { ASSERT } from "./debug.js";
const sum = (...arg) => {
if(!arg.length){
throw new Error("no arguments!");
}
return arg.reduce((prev, curr) => {
return curr + prev
}, 0)
}
const test = new Test("2 + 2 shuld return 4", (N) => {
ASSERT(sum(2, 2), 4)
var args = []
for(var i = 1; i <= N; i++) {
args.push(i)
}
ASSERT(sum.apply(this, args), N * (N + 1) / 2 )
})
test.run(100)
Dom utilities
A set of a fiew userfull utilities for building user interfaces
import * from dom from "./dom.js";
const myTitle = document.createElement('h2')
document.body.appendChild(dom.html `
<article>
${myTitle}
</article>
`)
myTitle.innerHTML = 'hello world'
And it work with an emmet-like sintax too!
const myTitle2 = document.createElement('h2')
document.body.appendChild(
dom.emmet `article>${myTitle2}.title>span{hello }`
)
myTitle2.innerHTML += 'emmet!'
Observer
It implements the observer pattern in the objects where is injected.
For example:
import observable from "./observe.js"
class FireworkClass {
now(...args){
this.fire('fireworks!', ...args)
this.schedule()
}
schedule() {
setTimeout(
this.now.bind(this),
Math.random() * 500
)
}
}
observable.call(FireworkClass.prototype)
The observable.call(FireworkClass.prototype)
will add the function for handle events
fire will dispatch the event
let firework = new FireworkClass()
firework.on('fireworks!', (...args) => {
console.log('this fireworks are beautiful', ...args)
})
firework.now( )
Reactive
Sincronize variation between different objects
import reactive from "./reactive.js"
class MyReactiveClass {
constructor() {
this.bindable("magicProperty")
}
}
reactive.call(MyReactiveClass.prototype)
After you have injected the interface, the instances can have some bindable properties
let myReactiveInstance = new MyReactiveClass()
let myObj = {}
myReactiveInstance.bind('magicProperty', myObj)
myReactiveInstance.magicProperty = 'hello reactive!'
console.log(myObj.magicProperty)
This can be userfull when building user interfaces because...
let element = document.querySelector('#my-element')
if(element) {
myReactiveInstance.bind(
'magicProperty',
element,
'innerHTML'
)
}
myReactiveInstance.magicProperty = 'hello again!'
sometimes can be userfull also to bind a method of an object to a bindable reactive's property
myReactiveInstance.bind(
'magicProperty',
console.log.bind(console)
)
myReactiveInstance.magicProperty = 'woooow!!'