ValueSet.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var ValueSet = module.exports = function ValueSet(vals) {
  2. this.set = {};
  3. if (vals instanceof Array)
  4. this.insertRange(vals);
  5. }, klass = ValueSet, proto = klass.prototype;
  6. proto._getKey = JSON.stringify;
  7. proto.hasKey = function hasKey(key) {
  8. return key in this.set;
  9. };
  10. proto.has = function has(val) {
  11. return this._getKey(val) in this.set;
  12. };
  13. proto.insert = function insert(val) {
  14. var valKey = this._getKey(val);
  15. if (!this.hasKey(valKey)) {
  16. this.set[valKey] = val;
  17. return valKey;
  18. }
  19. return undefined;
  20. };
  21. proto.insertRange = function insertRange(vals) {
  22. var results = [];
  23. for (var i = 0, l = vals.length; i < l; i++)
  24. results.push(this.insert(vals[i]));
  25. return results;
  26. };
  27. proto.equals = function equals(other) {
  28. for (var key in this.set) {
  29. if (!other.hasKey(key))
  30. return false;
  31. }
  32. for (var otherKey in other.set) {
  33. if (!this.hasKey(otherKey))
  34. return false;
  35. }
  36. return true;
  37. };
  38. proto.values = function values() {
  39. var vals = [];
  40. for (var key in this.set)
  41. vals.push(this.set[key]);
  42. return vals;
  43. };