Coder Social home page Coder Social logo

gerhobbelt / aster-squery Goto Github PK

View Code? Open in Web Editor NEW

This project forked from asterjs/aster-squery

0.0 1.0 0.0 10 KB

Replace nodes with CSS-like selectors in aster.

Home Page: https://npmjs.org/package/aster-squery

License: MIT License

JavaScript 100.00%

aster-squery's Introduction

aster-squery

NPM version Build Status

Replace nodes with CSS-like selectors in aster.

Allows to use CSS-style selectors for finding nodes and replacing them with results of corresponding handlers.

Uses grasp-squery behind the scenes, so check out official documentation for syntax details.

Usage

First, install aster-squery as a development dependency:

npm install --save-dev aster-squery

Then, add it to your build script:

var aster = require('aster');
var squery = require('aster-squery');

aster.src('src/**/*.js')
.map(squery({
  'if[then=return][else=return]': function (node) {
    return {
        type: 'ReturnStatement',
        argument: {
            type: 'ConditionalExpression',
            test: node.test,
            consequent: node.consequent.argument,
            alternate: node.alternate.argument
        }
    };
  }
  // , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);

can be also written as:

var aster = require('aster');
var equery = require('aster-equery');

aster.src('src/**/*.js')
.map(equery({
  'if[then=return][else=return]': 'return <%= test %> ? <%= consequent.argument %> : <%= alternate.argument %>'
  // , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);

API

squery(mappings)

mappings

Type: {pattern: handler}

Replacement mappings.

pattern

Type: String

CSS-style node selector.

handler (option 1: callback)

Type: Function(node, named)

Callback to be called on each found match. It will get two arguments - matched node object and hashmap of named subpatterns.

handler (option 2: template)

Type: String

estemplate string to be used for generating AST.

License

MIT License

aster-squery's People

Contributors

gerhobbelt avatar rreverser avatar

Watchers

 avatar

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.