map-or-similar.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mapOrSimilar = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. module.exports = function(forceSimilar) {
  3. if (typeof Map !== 'function' || forceSimilar) {
  4. var Similar = _dereq_('./similar');
  5. return new Similar();
  6. }
  7. else {
  8. return new Map();
  9. }
  10. }
  11. },{"./similar":2}],2:[function(_dereq_,module,exports){
  12. function Similar() {
  13. this.list = [];
  14. this.lastItem = undefined;
  15. this.size = 0;
  16. return this;
  17. }
  18. Similar.prototype.get = function(key) {
  19. var index;
  20. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  21. return this.lastItem.val;
  22. }
  23. index = this.indexOf(key);
  24. if (index >= 0) {
  25. this.lastItem = this.list[index];
  26. return this.list[index].val;
  27. }
  28. return undefined;
  29. };
  30. Similar.prototype.set = function(key, val) {
  31. var index;
  32. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  33. this.lastItem.val = val;
  34. return this;
  35. }
  36. index = this.indexOf(key);
  37. if (index >= 0) {
  38. this.lastItem = this.list[index];
  39. this.list[index].val = val;
  40. return this;
  41. }
  42. this.lastItem = { key: key, val: val };
  43. this.list.push(this.lastItem);
  44. this.size++;
  45. return this;
  46. };
  47. Similar.prototype.delete = function(key) {
  48. var index;
  49. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  50. this.lastItem = undefined;
  51. }
  52. index = this.indexOf(key);
  53. if (index >= 0) {
  54. this.size--;
  55. return this.list.splice(index, 1)[0];
  56. }
  57. return undefined;
  58. };
  59. // important that has() doesn't use get() in case an existing key has a falsy value, in which case has() would return false
  60. Similar.prototype.has = function(key) {
  61. var index;
  62. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  63. return true;
  64. }
  65. index = this.indexOf(key);
  66. if (index >= 0) {
  67. this.lastItem = this.list[index];
  68. return true;
  69. }
  70. return false;
  71. };
  72. Similar.prototype.forEach = function(callback, thisArg) {
  73. var i;
  74. for (i = 0; i < this.size; i++) {
  75. callback.call(thisArg || this, this.list[i].val, this.list[i].key, this);
  76. }
  77. };
  78. Similar.prototype.indexOf = function(key) {
  79. var i;
  80. for (i = 0; i < this.size; i++) {
  81. if (this.isEqual(this.list[i].key, key)) {
  82. return i;
  83. }
  84. }
  85. return -1;
  86. };
  87. // check if the numbers are equal, or whether they are both precisely NaN (isNaN returns true for all non-numbers)
  88. Similar.prototype.isEqual = function(val1, val2) {
  89. return val1 === val2 || (val1 !== val1 && val2 !== val2);
  90. };
  91. module.exports = Similar;
  92. },{}]},{},[1])(1)
  93. });