Giter Site home page Giter Site logo

chiffon's Introduction

Chiffon

Build Status

A very small ECMAScript parser, tokenizer and minify written in JavaScript.

chiffon.min.js is 3KB now.

Installation

In Browser:

<script src="chiffon.js"></script>

or

<script src="chiffon.min.js"></script>

Object Chiffon will defined in the global scope.

In Node.js:

npm install chiffon
var Chiffon = require('chiffon');

bower:

bower install chiffon

Tokenize

Tokenize a string code.

  • {Array} Chiffon.tokenize ( code [, options ] )
    @param {string} code Target code.
    @param {Object} [options] Tokenize options.
    @return {Array} Return an array of the parsed tokens.
var tokens = Chiffon.tokenize('var a = 1');
console.log(tokens);
/*
[ { type: 'Keyword',    value: 'var' },
  { type: 'Identifier', value: 'a' },
  { type: 'Punctuator', value: '=' },
  { type: 'Numeric',    value: '1' } ]
*/

JavaScript AST is not currently supported.

Chiffon work simply tokenizing.

Defined token type

  • Comment
  • LineTerminator
  • Template
  • String
  • Punctuator
  • RegularExpression
  • Numeric
  • UnicodeEscapeSequence
  • Identifier
  • Null
  • Boolean
  • Keyword

Options

  • comment : {boolean} default=false
    true = Keep comment tokens.

  • lineTerminator : {boolean} default=false
    true = Keep line terminator tokens.

Untokenize

Concatenate to string from the parsed tokens.

  • {string} Chiffon.untokenize ( tokens )
    @param {Array} tokens An array of the parsed tokens.
    @return {string} Return a concatenated string.

Minify

Minify JavaScript code.

  • {string} Chiffon.minify ( code )
    @param {string} code Target code.
    @return {string} Return a minified code.
var min = Chiffon.minify('var a = 1 + 1; // comment');
console.log(min); // var a=1+1;

Minify is a simple implementation as following.

function minify(code) {
  return untokenize(tokenize(code, { lineTerminator: true }));
}

Demo

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.