radix2.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ;(function(){
  2. function Radix(){
  3. var radix = function(key, val, t){
  4. key = ''+key;
  5. if(!t && u !== val){
  6. radix.last = (key < radix.last)? radix.last : key;
  7. delete (radix.$||{})[_];
  8. }
  9. t = t || radix.$ || (radix.$ = {});
  10. if(!key && Object.keys(t).length){ return t }
  11. var i = 0, l = key.length-1, k = key[i], at, tmp;
  12. while(!(at = t[k]) && i < l){
  13. k += key[++i];
  14. }
  15. radix.at = t; /// caching to external access.
  16. if(!at){
  17. if(!map(t, function(r, s){
  18. var ii = 0, kk = '';
  19. if((s||'').length){ while(s[ii] == key[ii]){
  20. kk += s[ii++];
  21. } }
  22. if(kk){
  23. if(u === val){
  24. if(ii <= l){ return }
  25. return (tmp || (tmp = {}))[s.slice(ii)] = r;
  26. }
  27. var __ = {};
  28. __[s.slice(ii)] = r;
  29. ii = key.slice(ii);
  30. ('' === ii)? (__[''] = val) : ((__[ii] = {})[''] = val);
  31. t[kk] = __;
  32. delete t[s];
  33. return true;
  34. }
  35. })){
  36. if(u === val){ return; }
  37. (t[k] || (t[k] = {}))[''] = val;
  38. }
  39. if(u === val){
  40. return tmp;
  41. }
  42. } else
  43. if(i == l){
  44. if(u === val){ return (u === (tmp = at['']))? at : tmp }
  45. at[''] = val;
  46. } else {
  47. if(u !== val){ delete at[_] }
  48. return radix(key.slice(++i), val, at || (at = {}));
  49. }
  50. }
  51. return radix;
  52. };
  53. Radix.map = function map(radix, cb, opt, pre){ pre = pre || [];
  54. var t = ('function' == typeof radix)? radix.$ || {} : radix;
  55. if(!t){ return }
  56. var keys = (t[_]||no).sort || (t[_] = function $(){ $.sort = Object.keys(t).sort(); return $ }()).sort;
  57. //var keys = Object.keys(t).sort();
  58. opt = (true === opt)? {branch: true} : (opt || {});
  59. if(opt.reverse){ keys = keys.slice().reverse() }
  60. var start = opt.start, end = opt.end;
  61. var i = 0, l = keys.length;
  62. for(;i < l; i++){ var key = keys[i], tree = t[key], tmp, p, pt;
  63. if(!tree || '' === key || _ === key){ continue }
  64. p = pre.slice(); p.push(key);
  65. pt = p.join('');
  66. if(u !== start && pt < (start||'').slice(0,pt.length)){ continue }
  67. if(u !== end && (end || '\uffff') < pt){ continue }
  68. if(u !== (tmp = tree[''])){
  69. tmp = cb(tmp, pt, key, pre);
  70. if(u !== tmp){ return tmp }
  71. } else
  72. if(opt.branch){
  73. tmp = cb(u, pt, key, pre);
  74. if(u !== tmp){ return tmp }
  75. }
  76. pre = p;
  77. tmp = map(tree, cb, opt, pre);
  78. if(u !== tmp){ return tmp }
  79. pre.pop();
  80. }
  81. };
  82. Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) }
  83. if(typeof window !== "undefined"){
  84. var Gun = window.Gun;
  85. window.Radix = Radix;
  86. } else {
  87. var Gun = require('../gun');
  88. try{ module.exports = Radix }catch(e){}
  89. }
  90. var map = Gun.obj.map, no = {}, u;
  91. var _ = String.fromCharCode(24);
  92. }());