• Stars
    star
    203
  • Rank 189,010 (Top 4 %)
  • Language
    JavaScript
  • Created almost 15 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extensible JavaScript Transformations
 ___ ___    _____  _______  _______
|   |   | _|     ||     __||_     _|
|-     -||       ||__     |  |   |
|___|___||_______||_______|  |___|

Extensible JavaScript Transformations Build Status

XJST is a DSL for universal data transformations with compiler written on top of the Node.js and Ometa/JS and output code working in any browser or on server-side.

Data transformations?

Yes, traverse any data in specific flow using matching against conditions set to generate any output (see binary tree prefixer).

For example, XJST can be used as:

  • HTTP request router
  • template engine
  • AST transformator
  • parser

Extensible

XJST makes possible to extend your previous transformation by overriding or specializing some of it's parts (example below is extending this.url === '/login' condition with redirection for logged in users).

XJST is a superset of JavaScript so you can use any popular libraries (that is jquery or underscore) within your transformation and write condition bodies in JavaScript.

Creating your own DSL based on XJST is also possible, because it's syntax parser is powered by ometajs.

Basic example

Input:

template(this.url === '/')(function() {
  return render('home page')
});

template(this.url === '/login')(function() {
  return render('login form')
});

template(this.url === '/login', this.cookie.is_logined)(function() {
  return redirect('user page')
});

Output (simplified):

switch (this.url) {
  case '/login':
    switch (this.cookie.is_logined) {
      case true:
        return redirect('user page')
      default:
        return render('login form')
    }
  case '/':
    return render('home page')
}

More examples

Installation

npm install xjst

Public API

var xjst = require('xjst'),

    fn = xjst.compile('template string', 'filename.xjst', options);

fn({ your: 'data' });

Syntax

XJST extends JavaScript syntax with a following keywords: template, local, apply, applyNext.

Template

template(expression1 === value1, ... , expressionN === valueN)(function() {
  // will be run if condition above equals to true
})

Multiple template statements will be grouped to construct optimal conditions graph. Order of the template statements matters, the priority decreases from the bottom to the top.

There're few restrictions for templates:

  • Expressions in template predicate should have no side-effects (that is should not change transformation context).

  • It's preferred to use function calls or equality comparisons joined by logical && operator for expressions, as it can be better optimized at compilation time.

Local

var obj = { x: 1 };

console.log(local(obj)({ x: 2 })(obj.x)); // 2
console.log(obj.x); // 1

local allows you to make temporary changes to a visible variables scope. Every assignment put inside parens will be reverted immediately after the expression execution.

You can make multiple assignments in the one statement:

local({ x: 2, y: 3 })(/* your code */)

Or use local with a block:

local({ ... })(function() { var a = 1; return a * 2; });

Or as an expression:

var newX = local(this)({ x: 2 })(this.x);

Apply

template(true)(function() {
  return apply({ type: 'first' });
});

template(this.type === 'first')(function() {
  return apply({ type: 'second' });
});

template(this.type === 'second')(function() {
  return 'here am I';
});

XJST is intended to be applied recursively to the same data, while making small temporary changes to it (all changes will be reverted back after operation). apply keyword works exactly like a local (applying changes in the parens and reverting them after the execution), but with small distinction - apply doesn't have a body, so it's just doing some changes to the data and applying template recursively (the context will be preserved).

Apply next

template(this.page === 'home' && this.action === 'login')(function() {
  // match here
});

template(this.page === 'home')(function() {
  applyNext();
});

applyNext() call will reapply all templates, except one where it was called, to the inputs data.

CLI interface

$ bin/xjst --help

Usage:
  xjst [OPTIONS] [ARGS]


Options:
  -h, --help : Help
  -i INPUT, --input=INPUT : Input file (default: stdin)
  -o OUTPUT, --output=OUTPUT : Output file (default: stdout)

$ bin/xjst -i template.xjst

.... some code ...

Optimizations

Optimized graph

XJST takes all the template statements and produces a tree with comparisons in nodes and template bodies in leafs. apply are handled and replaced by direct calls to the tree nodes (some of comparisons can be skipped, using known context state).

Input:

template(this.type === 'a')(function() {
  // body 1
});
template(this.type === 'b')(function() {
  // body 2
});

Output (simplified):

switch (this.type) {
  case 'a':
    // body 1
    break;
  case 'b':
    // body 2
    break;
}

Documentation

Here is the documented source.

Some technical details (in Russian) can be found in doc/tech.ru.md.

Contributors

More Repositories

1

coa

Command-Option-Argument: Get more from defining your command line interface
JavaScript
143
star
2

krasota.js

Syntactic transformations of JavaScript code, with taking care of whitespaces and comments.
JavaScript
96
star
3

dotfiles

Personal configs
Lua
14
star
4

shmakowiki

Yet another wiki dialect, inspired by WackoWiki and WikiCreole
JavaScript
13
star
5

ometa-highlighter

Code highlighter based on Ometa/JS (little inspired by Pygments)
JavaScript
9
star
6

yajsh

Yet Another JavaScript Highlighter
JavaScript
8
star
7

jquery.jsonrpc

Yet another JSON-RPC plugin
7
star
8

bem-components-react

EXPERIMENT, DO NOT USE
JavaScript
7
star
9

tinyjira

Tiny interface for Atlassian JIRA based on JSON-RPC
JavaScript
7
star
10

bem-method

Методология ведения и использования исходного кода проекта
6
star
11

jscreole

JavaScript Creole 1.0 Wiki Markup Parser
JavaScript
4
star
12

ChicoryScript

Do we need yet another CoffeeScript or what?
4
star
13

jquery.hashhistory

jQuery hash change event
JavaScript
3
star
14

icfpc2013

ICFPC 2013
Java
3
star
15

nme

Nyctergatis Markup Engine
C
2
star
16

thaipass-check

Check for ThaiPass status
TypeScript
2
star
17

json4xml

Yet another JSON to XML convention
JavaScript
2
star
18

ometa-js-old

Fork http://www.tinlizzie.org/~awarth/svn/ometa-js/
JavaScript
2
star
19

odessajs-bem

My talk for OdessaJS 2015
JavaScript
2
star
20

veged.github.com

My GitHub Page
2
star
21

bem-i18n

BEM internationalization
JavaScript
2
star
22

bablo

Personal finance tracker (frozen project)
Python
2
star
23

touchide

Programming on iPhone
JavaScript
2
star
24

bem-react-core-todomvc

TodoMVC with help of bem-react-core
2
star
25

test-webpack-bem

EXPERIMENT
JavaScript
1
star
26

morninggrace.ru

Morning Grace music band site
JavaScript
1
star
27

ya-shri-bem-pract

Примеры для лекции в ШРИ "БЭМ: Практика"
JavaScript
1
star
28

narwhal-v8cgi

v8cgi engine support for Narwhal
JavaScript
1
star
29

icfpc2012

http://icfpcontest2012.wordpress.com/
JavaScript
1
star
30

bemhtml2other-stub

Use BEMHTML for generate other templates
JavaScript
1
star