Welcome to the documentation for the Jsel library, a TypeScript library designed to add a new programming language executed on JavaScript. This documentation provides a comprehensive guide on using the Jsel library and covers various features, syntax, and examples.
- Installation
- Getting Started
- Basic Syntax
- Control Flow
- Functions
- Standard Library
- Advanced Features
- Examples
- Conclusion
- Support and Contribution
To use the Jsel library in your TypeScript project, follow these steps:
npm install @dexodus/jsel
The core components of the Jsel library are the Jsel
class and the JselContext
class. The Jsel
class represents the interpreter, while the JselContext
class provides the initial context for the interpreter. Here's a basic example of how to create and use them:
import { Jsel, JselContext } from 'jsel-library';
const context = new JselContext({
// ... initial context data ...
});
const jsel = new Jsel(context);
// Now you can use jsel to execute Jsel code
Jsel supports variables, and you can declare and use them as follows:
const jsel = new Jsel(new JselContext({
a: 5,
b: 'Hello',
c: true,
}));
console.log(jsel.exec('a')); // Output: 5
console.log(jsel.exec('b + " World"')); // Output: Hello World
console.log(jsel.exec('c')); // Output: true
Jsel supports various arithmetic operations, including addition, subtraction, multiplication, division, and exponentiation:
const jsel = new Jsel(new JselContext({
x: 5,
y: 3,
}));
console.log(jsel.exec('x + y')); // Output: 8
console.log(jsel.exec('x - y')); // Output: 2
console.log(jsel.exec('x * y')); // Output: 15
console.log(jsel.exec('x / y')); // Output: 1.666...
console.log(jsel.exec('x ** y')); // Output: 125
You can access object properties using dot notation:
const jsel = new Jsel(new JselContext({
person: {
name: 'John',
age: 25,
},
}));
console.log(jsel.exec('person.name')); // Output: John
console.log(jsel.exec('person.age')); // Output: 25
Jsel supports string concatenation using the +
operator:
const jsel = new Jsel(new JselContext({
greeting: 'Hello',
name: 'John',
}));
console.log(jsel.exec('greeting + " " + name')); // Output: Hello John
You can use assignment operations to update variable values:
const jsel = new Jsel(new JselContext({
a: 5,
}));
console.log(jsel.exec('a += 3')); // Output: 8
console.log(jsel.exec('a = a * 2')); // Output: 16
Jsel supports if statements for conditional execution:
const jsel = new Jsel(new JselContext({
condition: true,
value: 42,
}));
jsel.exec(`
if (condition) {
value = value * 2;
}
`);
console.log(jsel.exec('value')); // Output: 84
You can use while statements for repeated execution:
const jsel = new Jsel(new JselContext({
counter: 0,
}));
jsel.exec(`
while (counter < 5) {
counter++;
}
`);
console.log(jsel.exec('counter')); // Output: 5
Jsel supports foreach statements for iterating over arrays:
const jsel = new Jsel(new JselContext({
numbers: [1, 2, 3, 4, 5],
result: 0,
}));
jsel.exec(`
foreach (numbers as value) {
result += value;
}
`);
console.log(jsel.exec('result')); // Output: 15
Jsel provides a set of built-in functions for common operations:
const jsel = new Jsel(new JselContext({
value: 'Hello, World!',
}));
console.log(jsel.exec('length(value)')); // Output: 13
console.log(jsel.exec('toUpperCase(value)')); // Output: HELLO, WORLD!
You can define custom functions and use them in your Jsel code:
const jsel = new Jsel(new JselContext({
sum: (a, b) => a + b,
}));
console.log(jsel.exec('sum(3, 5)')); // Output: 8
Jsel allows you to declare functions for later use:
const jsel = new Jsel(new JselContext({}));
jsel.exec(`
add = (a, b) => {
a + b
}
`);
console.log(jsel.exec('add(2, 3)')); // Output: 5
Library
Jsel provides standard library functions for working with paths:
const jsel = new Jsel(new JselContext({
path: 'entity.users[12].name',
}));
console.log(jsel.exec(`getPathParent("${path}")`)); // Output: entity.users[12]
console.log(jsel.exec(`removeOldParentFromPath("${path}")`)); // Output: users[12].name
console.log(jsel.exec(`getCurrentIndexFromPath("${path}")`)); // Output: 12
Jsel includes functions for checking variable types:
const jsel = new Jsel(new JselContext({
value: true,
}));
console.log(jsel.exec('getType(value)')); // Output: boolean
console.log(jsel.exec('isBoolean(value)')); // Output: true
console.log(jsel.exec('isString(value)')); // Output: false
Jsel standard library provides other useful functions:
const jsel = new Jsel(new JselContext({}));
console.log(jsel.exec('jsonParse(\'{"a": "test"}\')')); // Output: { a: 'test' }
console.log(jsel.exec('log("Hello, Jsel!")')); // Output: undefined
Jsel introduces the reflex feature for dynamic variable access:
const jsel = new Jsel(new JselContext({
someVariable: 123,
path: 'someVariable',
}));
jsel.exec(`
valueByReflex = $path
`);
console.log(jsel.exec('valueByReflex')); // Output: 123
const jsel = new Jsel(new JselContext({
person: {
name: 'Alice',
age: 30,
},
}));
jsel.exec(`
person.name = "Bob";
person.age += 5;
`);
console.log(jsel.exec('person')); // Output: { name: 'Bob', age: 35 }
const jsel = new Jsel(new JselContext({
number: 42,
}));
jsel.exec(`
if (number > 50) {
number = number / 2;
} else {
number = number * 2;
}
`);
console.log(jsel.exec('number')); // Output: 84
const jsel = new Jsel(new JselContext({
numbers: [1, 2, 3, 4, 5],
sum: 0,
}));
jsel.exec(`
foreach (numbers as value) {
sum += value;
}
`);
console.log(jsel.exec('sum')); // Output: 15
const jsel = new Jsel(new JselContext({
customFunction: (a, b) => a * b,
}));
console.log(jsel.exec('customFunction(3, 4)')); // Output: 12
const jsel = new Jsel(new JselContext({
entity: {
id: 1,
name: 'Organization',
parts: [
{
question: 'When was I born?',
type: 'radio',
radio: [
{ answer: '23.09.2002', right: true },
{ answer: '23.09.2022', right: false },
],
},
],
},
newParts: [],
}));
jsel.exec(`
foreach (entity.parts as index => part) {
newPart = {
type: part.type,
question: part.question,
}
if (part.type === 'radio') {
newPart.data = {
answers: [],
answerPrices: [],
}
foreach (part.radio as answerIndex => answerObject) {
// Process radio answers
}
}
newParts[index] = newPart;
}
entity.parts = newParts;
`);
console.log(jsel.exec('entity')); // Updated entity object
This concludes the documentation for the Jsel library. We hope this guide helps you understand the features and capabilities of Jsel, enabling you to use it effectively in your TypeScript projects.
For support, bug reports, or contributions, please visit the Jsel GitHub repository. We welcome any feedback and contributions from the community.
Thank you for choosing Jsel!