Esy is a new JS preprocessor allows you to use custom block structures.
Right now in JavaScript and many other languages that support the block structure, you're only able to use some predefined structures like:
- if (){...}
- for (){...}
- while (){...}
- do {...} while();
- switch (){...}
- else {...}
- try {...}
- catch (){...}
- finally (){...}
- etc...
Those are all familiar structures to you, but have you ever thought about the following structure?
timeout 200 {
console.log("Hello World!");
}
Certainly, it's easier than what we have in JS
right now:
setTimeout(function() {
console.log("Hello World!");
}, 200);
Or even with arrow functions
:
setTimeout(() => {
console.log("Hello World!");
}, 200);
That why Esy
comes from Easy
You can install this package globally by running:
npm install esy-language -g
but if you're interested in the core API and wants to use it in your own package just run:
npm install esy-language --save
After installing the Esy
, save this file as ex.esy
// Cache sum for 500ms
cache 500 sum(a,b){
console.log('Computing...');
return a+b;
} key (c,d){
// We don't care about numbers order in sum function (a+b=b+a)
return [c, d].sort();
}
// Compute 5+7 once
console.log(sum(5,7))
// Load theme from cache without computing
console.log(sum(5,7))
console.log(sum(7,5))
// Wait 100ms more than cache's lifetime.
timeout 600{
// It should compute 5+7 again
console.log(sum(7,5))
}
and then cd to the directory that your file is and run this command to run program:
esy ex.esy
for saving result to a file run:
esy compile ex.esy -s
Read official docs for more details.
To run tests just run:
git clone https://github.com/Slye-team/esy-language.git
cd esy-language
npm run test