index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // ::- Persistent data structure representing an ordered mapping from
  2. // strings to values, with some convenient update methods.
  3. function OrderedMap(content) {
  4. this.content = content;
  5. }
  6. OrderedMap.prototype = {
  7. constructor: OrderedMap,
  8. find: function(key) {
  9. for (var i = 0; i < this.content.length; i += 2)
  10. if (this.content[i] === key) return i
  11. return -1
  12. },
  13. // :: (string) → ?any
  14. // Retrieve the value stored under `key`, or return undefined when
  15. // no such key exists.
  16. get: function(key) {
  17. var found = this.find(key);
  18. return found == -1 ? undefined : this.content[found + 1]
  19. },
  20. // :: (string, any, ?string) → OrderedMap
  21. // Create a new map by replacing the value of `key` with a new
  22. // value, or adding a binding to the end of the map. If `newKey` is
  23. // given, the key of the binding will be replaced with that key.
  24. update: function(key, value, newKey) {
  25. var self = newKey && newKey != key ? this.remove(newKey) : this;
  26. var found = self.find(key), content = self.content.slice();
  27. if (found == -1) {
  28. content.push(newKey || key, value);
  29. } else {
  30. content[found + 1] = value;
  31. if (newKey) content[found] = newKey;
  32. }
  33. return new OrderedMap(content)
  34. },
  35. // :: (string) → OrderedMap
  36. // Return a map with the given key removed, if it existed.
  37. remove: function(key) {
  38. var found = this.find(key);
  39. if (found == -1) return this
  40. var content = this.content.slice();
  41. content.splice(found, 2);
  42. return new OrderedMap(content)
  43. },
  44. // :: (string, any) → OrderedMap
  45. // Add a new key to the start of the map.
  46. addToStart: function(key, value) {
  47. return new OrderedMap([key, value].concat(this.remove(key).content))
  48. },
  49. // :: (string, any) → OrderedMap
  50. // Add a new key to the end of the map.
  51. addToEnd: function(key, value) {
  52. var content = this.remove(key).content.slice();
  53. content.push(key, value);
  54. return new OrderedMap(content)
  55. },
  56. // :: (string, string, any) → OrderedMap
  57. // Add a key after the given key. If `place` is not found, the new
  58. // key is added to the end.
  59. addBefore: function(place, key, value) {
  60. var without = this.remove(key), content = without.content.slice();
  61. var found = without.find(place);
  62. content.splice(found == -1 ? content.length : found, 0, key, value);
  63. return new OrderedMap(content)
  64. },
  65. // :: ((key: string, value: any))
  66. // Call the given function for each key/value pair in the map, in
  67. // order.
  68. forEach: function(f) {
  69. for (var i = 0; i < this.content.length; i += 2)
  70. f(this.content[i], this.content[i + 1]);
  71. },
  72. // :: (union<Object, OrderedMap>) → OrderedMap
  73. // Create a new map by prepending the keys in this map that don't
  74. // appear in `map` before the keys in `map`.
  75. prepend: function(map) {
  76. map = OrderedMap.from(map);
  77. if (!map.size) return this
  78. return new OrderedMap(map.content.concat(this.subtract(map).content))
  79. },
  80. // :: (union<Object, OrderedMap>) → OrderedMap
  81. // Create a new map by appending the keys in this map that don't
  82. // appear in `map` after the keys in `map`.
  83. append: function(map) {
  84. map = OrderedMap.from(map);
  85. if (!map.size) return this
  86. return new OrderedMap(this.subtract(map).content.concat(map.content))
  87. },
  88. // :: (union<Object, OrderedMap>) → OrderedMap
  89. // Create a map containing all the keys in this map that don't
  90. // appear in `map`.
  91. subtract: function(map) {
  92. var result = this;
  93. map = OrderedMap.from(map);
  94. for (var i = 0; i < map.content.length; i += 2)
  95. result = result.remove(map.content[i]);
  96. return result
  97. },
  98. // :: () → Object
  99. // Turn ordered map into a plain object.
  100. toObject: function() {
  101. var result = {};
  102. this.forEach(function(key, value) { result[key] = value; });
  103. return result
  104. },
  105. // :: number
  106. // The amount of keys in this map.
  107. get size() {
  108. return this.content.length >> 1
  109. }
  110. };
  111. // :: (?union<Object, OrderedMap>) → OrderedMap
  112. // Return a map with the given content. If null, create an empty
  113. // map. If given an ordered map, return that map itself. If given an
  114. // object, create a map from the object's properties.
  115. OrderedMap.from = function(value) {
  116. if (value instanceof OrderedMap) return value
  117. var content = [];
  118. if (value) for (var prop in value) content.push(prop, value[prop]);
  119. return new OrderedMap(content)
  120. };
  121. export default OrderedMap;