Skip to content

Commit

Permalink
Book formatting + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amark committed Feb 24, 2023
1 parent d732a9e commit 1bdab73
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,39 @@ function text(p){
return '|'+from(p).concat(p.list).sort().join('|')+'|'; // commenting out this sub-portion of code fixed a more basic test, but will probably cause a bug with a FROM + MEMORY.
}

B.encode = function(d, s){ s = s || "|";
switch(typeof d){
case 'string': // text
var i = d.indexOf(s), t = '';
while(i != -1){ t += s; i = d.indexOf(s, i+1) }
return t + '"' + d;
case 'number': return (d < 0)? ''+d : '+'+d;
case 'boolean': return d? '+' : '-';
case 'object': return d? "{TODO}" : ' ';
}
}
// deprecate this B.encode below:
B.encode = function(d, _){ _ = _ || "'";
if(typeof d == 'string'){
var i = d.indexOf(_), t = "";
while(i != -1){ t += _; i = d.indexOf(_, i+1) }
return t + _+d+_;
}
}

B.decode = function(t, s){ s = s || "|";
if('string' != typeof t){ return }
switch(t){ case ' ': return null; case '-': return false; case '+': return true; }
switch(t[0]){
case '-': case '+': return parseFloat(t);
}
}
function heal(l, s){ var i, c = 0;
if(0 > (i = l.indexOf(''))){ return l }
while('' == l[++c + i]){}; // get count of escape prefix
return heal(l.slice(0,i).concat(l.slice(i+c, i+c+c+1).join(s||'|'), l.slice(i+c+c+1)));
}

B.hash = function(s, c){ // via SO
if(typeof s !== 'string'){ return }
c = c || 0; // CPU schedule hashing by
Expand Down
89 changes: 89 additions & 0 deletions test/rad/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var names = ["Adalard","Adora","Aia","Albertina","Alfie","Allyn","Amabil","Ammam
var opt = {};
opt.file = 'radatatest';
var RAD = (setTimeout.RAD) || require('../../lib/radisk');
var Book = (setTimeout.Book) || require('../../lib/book');
//opt.store = ((Gun.window && Gun.window.RindexedDB) || require('../../lib/rfs'))(opt);
opt.chunk = 1000;
var rad = RAD(opt), esc = String.fromCharCode(27);
Expand Down Expand Up @@ -62,6 +63,27 @@ var names = ["Adalard","Adora","Aia","Albertina","Alfie","Allyn","Amabil","Ammam
indexedDB.deleteDatabase('radatatest').onsuccess = function(e){ done() }
});

describe('Book Format', function(done){
var B = Book;
it.skip('encode decode', function(){
expect(B.decode(B.encode(null))).to.be(null);
expect(B.decode(B.encode(false))).to.be(false);
expect(B.decode(B.encode(true))).to.be(true);
expect(B.decode(B.encode(0))).to.be(0);
expect(B.decode(B.encode(-Infinity))).to.be(-Infinity);
expect(B.decode(B.encode(Infinity))).to.be(Infinity);
expect(B.decode(B.encode(1))).to.be(1);
expect(B.decode(B.encode(2))).to.be(2);
expect(B.decode(B.encode(1.2))).to.be(1.2);
expect(B.decode(B.encode(1234.56789))).to.be(1234.56789);
console.log("RESUME HERE!"); return;
expect(B.decode(B.encode(''))).to.be('');
expect(B.decode(B.encode("hello world"))).to.be("hello world");
expect(B.decode(B.encode("he||o"))).to.be("he||o");
expect(B.decode(B.encode("ho|y ha|o"))).to.be("ho|y ha|o");
});

});
describe('BASIC API', function(done){

it('write', function(done){
Expand Down Expand Up @@ -193,6 +215,73 @@ var names = ["Adalard","Adora","Aia","Albertina","Alfie","Allyn","Amabil","Ammam

});

describe('Recursive Book Lookups', function(){

function gen(val){ return val + String.random(99,'a') }
var opt = {file: 'gen'}
//var rad = window.names = Book();
var rad = window.names = RAD(opt);
it('Generate more than 1 page', done => {

var i = 0;
names.forEach(function(name){
name = name.toLowerCase();
rad(name, gen(name));

clearTimeout(done.c)
done.c = setTimeout(done, 99);
});

});

it('Make sure parseless lookup works with incrementally parsed values', done => {

rad = RAD(opt);
rad('adora', function(err, page){
var n = page.get('adora');
expect(gen('adora')).to.be(n);

rad('aia', function(err, page){
var n = page.get('aia');
expect(gen('aia')).to.be(n);
done();
});
});

});

it('Read across the pages', done => {

rad = RAD(opt);
names.forEach(function(name){
name = name.toLowerCase();
rad(name, function(err, page){
var n = page.get(name);
expect(gen(name)).to.be(n);

clearTimeout(done.c);
done.c = setTimeout(done, 99);
});
});

});


/*it.skip('Correctly calculate size', done => {
var r = String.random(1000);
rad('a', r);
r = String.random(2000);
rad('b', r);
r = String.random(3000);
rad('c', r);
});*/

});

});

var ntmp = names;
Expand Down

0 comments on commit 1bdab73

Please sign in to comment.