graph-common
Version:
Open Graph API core js lib
72 lines (61 loc) • 1.69 kB
JavaScript
(function() {
var GQL, Query;
Query = require('./query');
GQL = (function() {
function GQL() {}
GQL.parse = function(script, callback) {
var lines, queries;
queries = [];
lines = script.split("\n");
lines.forEach(function(line) {
return queries.push(GQL.parse_line(line, callback));
});
return queries;
};
GQL.parse_line = function(line, callback) {
var action, data, node, query;
query = null;
node = null;
GQL.parse_node(line, function(parsed, rest) {
node = parsed;
return line = rest;
});
action = null;
GQL.parse_action(line, function(parsed, rest) {
action = parsed;
return line = rest;
});
data = line.trim();
query = new Query(node, action, data);
if (callback) {
callback(query);
}
return query;
};
GQL.parse_node = function(line, callback) {
var path, rest, splitted;
line.replace(/^\s+/, '');
splitted = line.split(' ');
path = splitted.shift();
rest = splitted.join(' ');
callback(path, rest);
return path;
};
GQL.parse_action = function(line, callback) {
var action, rest, splitted;
line.replace(/^\s+/, '');
splitted = line.split(' ');
action = splitted.shift().toLowerCase();
if (action === 'create' || action === 'read' || action === 'update' || action === 'delete') {
rest = splitted.join(' ');
} else {
action = 'read';
rest = line;
}
callback(action, rest);
return action;
};
return GQL;
})();
module.exports = GQL;
}).call(this);