Skip to content

Instantly share code, notes, and snippets.

@kirjs
Created April 22, 2014 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirjs/11165115 to your computer and use it in GitHub Desktop.
Save kirjs/11165115 to your computer and use it in GitHub Desktop.
graspWrapper
/**
* Simple wrapper to add chainable syntax to grasp.js
*
* Retuns an object that could be used this way:
*
* grasp( code ).squery('if.test').search()
* grasp( code ).squery('if.test').replace('!({{}})');
*
*/
function graspWrapper(grasp) {
var Query = function(type, query, code) {
this.type = type;
this.code = code;
this.query = query;
}
Query.prototype = {
search: function() {
return grasp.search(this.type, this.query, this.code);
},
replace: function(replaceWith) {
return grasp.replace(this.type, this.query, replaceWith, this.code);
}
}
var Grasp = function(code) {
this.code = code;
}
Grasp.prototype = {
squery: function(query) {
return new Query('squery', query, this.code);
},
equery: function(query) {
return new Query('equery', query, this.code);
}
};
var result = function(code) {
return new Grasp(code);
};
result.VERSION = grasp.VERSION;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment