Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LEX fix #1268

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

LEX fix #1268

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions examples/basic/lex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!DOCTYPE html>

<style>
#app
{
padding: 20px;
border: 1px solid red;
border-radius: 5px;
box-shadow: 0px 0px 5px black;
}
</style>

<h1>LEX</h1>

<div id="app"></div>

<script src="../jquery.js"></script>
<script src="../../../gun/gun.js"></script>
<script src="../../../gun/sea.js"></script>

<script>
var gun = GUN();
var gun2 = GUN();

var date = new Date().toISOString();
var Bob = {pub: 'GJeqY1ehreDAZClgW-PhVtksbGJdQDpv0xJK2pNT9I0.97QmEMb03NNqwnTnybZ9qXfRsFTyllY2SYq4tiCKMQQ', priv: 'NPN5_kzpgFpMPxZ5dGuGoUtTyjvS0P0FErSW_Jntboc', epub: 'MrNEMSY-ivK3wYJPABepgs-XtJNQ38YZdBbU8bfSzys.5yT3CpSRJbtaIpxG7W-FdTqIvPetdXBrWLUBAYQUsWc', epriv: 'kYBlQ3NFlk-dNFMsZ_q4u24E91X-91tlVcYqiX8Zz-4'};
var Alice = {pub: '71ovADGEThMmJulYtzPR4kXvZBqvIO2MU3-55dPxeyw.sm_mNTSiFOyDVHXLH-BbMqREnyLIrm7ZuVyfITweG8M', priv: 'wEmX7hysN3hImd5taexKaRA246roFvRplrJeTKp8OXo', epub: 'Xa42tyohvvp6ynH1wm_6gOjNA7X76dTH7icS5QSCHlc.lDhFr7YTzIhCCJm-B5FFXNYdbsY_AF84EiXBC_6Sk4I', epriv: '0WeVu6Eck5QpkipVgkb_IYTICdIA_lSSmaIe64YmMas'};

gun.user().auth(Bob);

(async () => {
// Creates a new url record to be added to the list
var url = { url: 'https://gun.eco' }

// Stringifies the object and uses SEA.work to generate a base64 hash for it
var text = JSON.stringify(url);
var operator = '<';
var matchDate = date;
// var matchDate = '2022-08-02T21:15:10.382Z';

var certificate = await SEA.certify(Bob.pub, { '*':'list' }, Alice);

gun
.user(Alice.pub)
.get('list')
.get(date)
.put(text, ack => {
if (ack.err) {
document.querySelector('#app').innerHTML += '<br/><br/>';
document.querySelector('#app').innerHTML += ` ${ack.err} `;
}
}, {opt: {cert: certificate}});

document.querySelector('#app').innerHTML += date;
document.querySelector('#app').innerHTML += ` ${operator} `;
document.querySelector('#app').innerHTML += matchDate;

const match = {
// lexical queries are kind of like a limited RegEx or Glob.
'.': {
// property selector
[operator]: matchDate, // find any indexed property larger ~3 hours ago
},
};

gun
.user(Alice.pub)
.get('list')
.get(match)
.map()
.once((d,k)=> {
document.querySelector('#app').innerHTML += '<br/><br/>';
document.querySelector('#app').innerHTML += `${k} - ${d}`;
})
})();

</script>
34 changes: 16 additions & 18 deletions gun.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,16 @@
while(l-- > 0){ s += c.charAt(Math.floor(Math.random() * c.length)) }
return s;
}
String.match = function(t, o){ var tmp, u;
if('string' !== typeof t){ return false }
if('string' == typeof o){ o = {'=': o} }
String.match = function(t, o){
if(typeof o === 'string'){ o = {'=': o} }
o = o || {};
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;
return (typeof t === 'string' && Object.keys(o).every((e) => {
if (e === '*') { return t.slice(0, (o[e]||'').length) === o[e] }
else if (e === '=') { return t === o[e] }
else if (e === '>') { return t > o[e] }
else if (e === '<') { return t < o[e] }
else return true
}))
}
String.hash = function(s, c){ // via SO
if(typeof s !== 'string'){ return }
Expand Down Expand Up @@ -1293,7 +1287,7 @@
if(tmp = ((tmp = lex['#'])||'')['='] || tmp){ return gun.get(tmp) }
(tmp = gun.chain()._).lex = lex; // LEX!
gun.on('in', function(eve){
if(String.match(eve.get|| (eve.put||'')['.'], lex['.'] || lex['#'] || lex)){
if(eve.get || String.match((eve.put||'')['.'], lex['.'] || lex['#'] || lex)){
tmp.on('in', eve);
}
this.to.next(eve);
Expand Down Expand Up @@ -1323,9 +1317,13 @@
return chain;
}
function map(msg){ this.to.next(msg);
var cat = this.as, gun = msg.$, at = gun._, put = msg.put, tmp;
var cat = this.as, gun = msg.$, at = gun._, put = msg.put, tmp, n;
if(!at.soul && !msg.$$){ return } // this line took hundreds of tries to figure out. It only works if core checks to filter out above chains during link tho. This says "only bother to map on a node" for this layer of the chain. If something is not a node, map should not work.
if((tmp = cat.lex) && !String.match(msg.get|| (put||'')['.'], tmp['.'] || tmp['#'] || tmp)){ return }

if (tmp = cat.lex) {
n = (typeof at.back.get === 'undefined' || at.back.get.charAt(0) === '~') ? (put||'')['.'] : msg.get; // Check if user or public graph
if(!String.match(n, tmp['.'] || tmp['#'] || tmp)){ return }
}
Gun.on.link(msg, cat);
}
var noop = function(){}, event = {stun: noop, off: noop}, u;
Expand Down
Loading