Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. export class NestedFrequencyTable
  2. constructor( initialData ) {
  3. this._categories = {};
  4. if( initialData )
  5. dojo.mixin( this._categories, initialData );
  6. }
  7.  
  8. // get the sum of all the category counts
  9. total() {
  10. // calculate total if necessary
  11. var t = 0;
  12. for( var k in this._categories ) {
  13. var v = this._categories[k];
  14. t += v.total ? v.total() : v;
  15. }
  16. return t;
  17. }
  18.  
  19. // decrement the count for the given category
  20. decrement( slotName, amount ) {
  21. if( ! amount )
  22. amount = 1;
  23.  
  24. if( !slotName )
  25. slotName = 'default';
  26. else
  27. slotName = slotName.toString();
  28.  
  29. if( this._categories[slotName] )
  30. return this._categories[slotName] = Math.max( 0 , this._categories[slotName] - amount );
  31. else
  32. return 0;
  33. },
  34.  
  35. // increment the count for the given category
  36. increment( slotName, amount ) {
  37. if( ! amount )
  38. amount = 1;
  39.  
  40. if( !slotName )
  41. slotName = 'default';
  42. else
  43. slotName = slotName.toString();
  44. return ( this._categories[slotName] = (this._categories[slotName] || 0) + amount );
  45. },
  46.  
  47.  
  48. // get the value of the given category. may be a number or a
  49. // frequency table.
  50. get( slotName ) {
  51. return this._categories[slotName] || 0;
  52. },
  53.  
  54. // get a given category as a frequency table
  55. getNested( path ) {
  56. if( typeof path == 'string' )
  57. path = path.split('/');
  58.  
  59. if( ! path.length )
  60. return this;
  61.  
  62. var slotName = path[0].toString();
  63. var slot = this._categories[slotName];
  64. if( ! slot || ! slot._categories )
  65. slot = this._categories[slotName] = new NestedFrequencyTable( slot ? { "default": slot+0 } : {} );
  66.  
  67. if( path.length > 1 ) {
  68. return slot.getNested( path.slice(1) );
  69. } else
  70. return slot;
  71. },
  72.  
  73. // returns array of category names that are present
  74. categories() {
  75. return Object.keys( this._categories );
  76. },
  77.  
  78. toString() {
  79. return this.total().toPrecision(6).toString().replace(/\.?0+$/,'');
  80. },
  81.  
  82. valueOf() {
  83. return this.total();
  84. },
  85.  
  86. // iterate through the categories and counts, call like:
  87. //
  88. // tbl.forEach( function( count, categoryName ) {
  89. // // do something
  90. // }, this );
  91. //
  92. forEach( func, ctx ) {
  93. var c = this._categories;
  94. if( ctx ) {
  95. for( var slotName in c ) {
  96. func.call( ctx, c[slotName], slotName );
  97. }
  98. } else {
  99. for( var slotName in c ) {
  100. func( c[slotName], slotName );
  101. }
  102. }
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement