Skip to content

Latest commit

 

History

History
85 lines (61 loc) · 1.56 KB

Get Started.md

File metadata and controls

85 lines (61 loc) · 1.56 KB

🚄 Get Started

escript is available via both npmjs repository and CDN. It is compatible with both NodeJS and the browser environment.

Init project

npm init

Config package.json

{
  ...
  "type": "module",
  ...
  "scripts": {
    "start": "node index.js"
  },
}

Install dependencies

npm i @chientrm/es fs prompt

Source code

index.js

import { eEval } from "@chientrm/es";
import { readFileSync } from "fs";
import prompt from "prompt";

const text = readFileSync("main.es", { encoding: "utf8", flag: "r" });
eEval([{ prompt, console }], "main.es", text);

main.es

Create shorthand log = console.log

Init prompt via prompt.start()

Define sum = [a b] => (a + b)

prompt.get(['firstNumber' 'secondNumber'] [err result] => (
    err && log(err)
    err || log(
            "Sum of" result.firstNumber
            "and" result.secondNumber
            "is" sum(result.firstNumber * 1 result.secondNumber * 1)
        )
    )
)

Execution

npm start

Output

prompt: firstNumber:  1
prompt: secondNumber:  2.34
Sum of 1 and 2.34 is 3.34

Discussion

  • escript is fully compatible with JavaScript modules.
  • escript is smart enough to skip non-expression inside the script. Ex: Shorthand log, Init prompt via, Define function
  • All characters included in \r\n\t\f ;,:?\\#@$ are considered space and got skipped by escript so that you can either insert them or not.

Conclusion

Developers are free to define their coding syntax and linting with escript.