mirror of
https://github.com/amark/gun.git
synced 2025-06-03 12:46:43 +00:00
Lex builder (#1249)
* lex builder * updated lex builder * added args to map * added match to lex-builder * update match * moved matach and gave Lex to Global Gun * fix match in lex
This commit is contained in:
parent
87652467b5
commit
9c9a5fd293
93
lib/lex.js
Normal file
93
lib/lex.js
Normal file
@ -0,0 +1,93 @@
|
||||
(function (Gun, u) {
|
||||
/**
|
||||
*
|
||||
* credits:
|
||||
* github:bmatusiak
|
||||
*
|
||||
*/
|
||||
var lex = (gun) => {
|
||||
function Lex() {}
|
||||
|
||||
Lex.prototype = Object.create(Object.prototype, {
|
||||
constructor: {
|
||||
value: Lex
|
||||
}
|
||||
});
|
||||
Lex.prototype.toString = function () {
|
||||
return JSON.stringify(this);
|
||||
}
|
||||
Lex.prototype.more = function (m) {
|
||||
this[">"] = m;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.less = function (le) {
|
||||
this["<"] = le;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.in = function () {
|
||||
var l = new Lex();
|
||||
this["."] = l;
|
||||
return l;
|
||||
}
|
||||
Lex.prototype.of = function () {
|
||||
var l = new Lex();
|
||||
this.hash(l)
|
||||
return l;
|
||||
}
|
||||
Lex.prototype.hash = function (h) {
|
||||
this["#"] = h;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.prefix = function (p) {
|
||||
this["*"] = p;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.return = function (r) {
|
||||
this["="] = r;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.limit = function (l) {
|
||||
this["%"] = l;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.reverse = function (rv) {
|
||||
this["-"] = rv || 1;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.includes = function (i) {
|
||||
this["+"] = i;
|
||||
return this;
|
||||
}
|
||||
Lex.prototype.map = function (...args) {
|
||||
return gun.map(this, ...args);
|
||||
}
|
||||
Lex.prototype.match = lex.match;
|
||||
|
||||
return new Lex();
|
||||
};
|
||||
|
||||
lex.match = function(t,o){ var tmp, u;
|
||||
o = o || this || {};
|
||||
if('string' == typeof o){ o = {'=': o} }
|
||||
if('string' !== typeof t){ return false }
|
||||
tmp = (o['='] || o['*'] || o['>'] || o['<']);
|
||||
if(t === tmp){ return true }
|
||||
if(u !== o['=']){ return false }
|
||||
tmp = (o['*'] || o['>']);
|
||||
if(t.slice(0, (tmp||'').length) === tmp){ return true }
|
||||
if(u !== o['*']){ return false }
|
||||
if(u !== o['>'] && u !== o['<']){
|
||||
return (t >= o['>'] && t <= o['<'])? true : false;
|
||||
}
|
||||
if(u !== o['>'] && t >= o['>']){ return true }
|
||||
if(u !== o['<'] && t <= o['<']){ return true }
|
||||
return false;
|
||||
}
|
||||
|
||||
Gun.Lex = lex;
|
||||
|
||||
Gun.chain.lex = function () {
|
||||
return lex(this);
|
||||
}
|
||||
|
||||
})((typeof window !== "undefined") ? window.Gun : require('../gun'))
|
Loading…
x
Reference in New Issue
Block a user