index.cjs 4.3 KB

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