From 1bdab7394a15c5966c3c55ad01edc36033c36400 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Thu, 23 Feb 2023 19:07:43 -0800 Subject: [PATCH] Book formatting + more tests --- lib/book.js | 26 ++++++++++++++ test/rad/book.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/lib/book.js b/lib/book.js index 482b1d952..6c7f67a42 100644 --- a/lib/book.js +++ b/lib/book.js @@ -127,6 +127,18 @@ 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 = ""; @@ -134,6 +146,20 @@ B.encode = function(d, _){ _ = _ || "'"; 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 diff --git a/test/rad/book.js b/test/rad/book.js index 57971691f..a70fbcee9 100644 --- a/test/rad/book.js +++ b/test/rad/book.js @@ -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); @@ -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){ @@ -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;