Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xjst

Package Overview
Dependencies
Maintainers
3
Versions
162
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xjst

XSLT inspired JavaScript templates (with spices)

  • 0.5.21
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
113
decreased by-57.84%
Maintainers
3
Weekly downloads
 
Created
Source
 ___ ___    _____  _______  _______
|   |   | _|     ||     __||_     _|
|-     -||       ||__     |  |   |
|___|___||_______||_______|  |___|

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).

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 (i.e. jquery or underscore) within your transformation and write condition's 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 === '/') {
  return render('home page')
}

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

template(this.url === '/login' && this.cookie.is_logined) {
  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, extends, super.

Template

template(expression1 === value1 && ... && expressionN === valueN) {
  // 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's predicate should have no side-effects (i.e. 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 x = 1;

console.log(local(x = 2) x); // 2
console.log(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(this.x = 2, this.y = 3) ...

Or use local with a block:

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

Or as an expression:

var newX = local(x = 2) x;

Extends

extends 'relative/or/absolute/path/to/xjst/file'

// ... your template statements ..

Extend current transformation with one passed into extends, not that comparisons in current file will have a higher priority.

Apply

template(true) {
  return apply(this.type = 'first');
}

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

template(this.type === 'second') {
  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).

Super apply

template(this.page === 'home') {
  // do something
}
// second.xjst
extends 'first'

template(this.page === 'home') {
  super apply(); // Will call "do something" in upper template
}

Super call gives you an ability to pass execution into transformations that you're extending.

Apply next

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

template(this.page === 'home') {
  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's bodies in leafs. apply are handled and replaced by direct calls to the tree's nodes (some of comparisons can be skipped, using known context's state).

Input:

template(this.type === 'a') {
  // body 1
}
template(this.type === 'b') {
  // 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

FAQs

Package last updated on 16 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc