var _ = require("underscore"); function empty(xs) { return _.size(xs) < 1; } function not(x) { return !x; } function min(a, b) { if (a < b) { return 1; } else if (a > b) { return -1; } else { return 0; } } function groupOps(ops) { return _.groupBy(ops.sort(), _.isEqual); } function find(f, haystack) { for(var i = 0; i < haystack.length; i++) { if (f(haystack[i])) { return i; } } return false; } function dict(pairs) { var o = {}; pairs.map(function(p) { o[p[0]] = p[1]; }); return o; } function flatten(xs) { if (!(xs instanceof Array)) { return xs; } if (xs.every(function (x) { return !(x instanceof Array); })) { return xs; } return [].concat.apply([], xs); } function extend(xs, ys) { xs.push.apply(xs, ys); return xs; } RegExp.escape= function(s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); }; function operatorMatch(ops) { var rstring = ops.sort(min).reduce( function(acc, x) { return acc + "(" + RegExp.escape(x) + ")|"; }, ""); var reg = new RegExp(rstring); return function(x) { var matched = reg.exec(x); if (matched[0]) { return matched[0]; } else { return false; } }; } module.exports = { not : not, groupOps : groupOps, opMatch : operatorMatch, dict: dict, extend : extend, empty : empty, };