Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

collection-strong.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict';
  2. var create = require('../internals/object-create');
  3. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  4. var defineBuiltIns = require('../internals/define-built-ins');
  5. var bind = require('../internals/function-bind-context');
  6. var anInstance = require('../internals/an-instance');
  7. var isNullOrUndefined = require('../internals/is-null-or-undefined');
  8. var iterate = require('../internals/iterate');
  9. var defineIterator = require('../internals/iterator-define');
  10. var createIterResultObject = require('../internals/create-iter-result-object');
  11. var setSpecies = require('../internals/set-species');
  12. var DESCRIPTORS = require('../internals/descriptors');
  13. var fastKey = require('../internals/internal-metadata').fastKey;
  14. var InternalStateModule = require('../internals/internal-state');
  15. var setInternalState = InternalStateModule.set;
  16. var internalStateGetterFor = InternalStateModule.getterFor;
  17. module.exports = {
  18. getConstructor: function (wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER) {
  19. var Constructor = wrapper(function (that, iterable) {
  20. anInstance(that, Prototype);
  21. setInternalState(that, {
  22. type: CONSTRUCTOR_NAME,
  23. index: create(null),
  24. first: null,
  25. last: null,
  26. size: 0
  27. });
  28. if (!DESCRIPTORS) that.size = 0;
  29. if (!isNullOrUndefined(iterable)) iterate(iterable, that[ADDER], { that: that, AS_ENTRIES: IS_MAP });
  30. });
  31. var Prototype = Constructor.prototype;
  32. var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
  33. var define = function (that, key, value) {
  34. var state = getInternalState(that);
  35. var entry = getEntry(that, key);
  36. var previous, index;
  37. // change existing entry
  38. if (entry) {
  39. entry.value = value;
  40. // create new entry
  41. } else {
  42. state.last = entry = {
  43. index: index = fastKey(key, true),
  44. key: key,
  45. value: value,
  46. previous: previous = state.last,
  47. next: null,
  48. removed: false
  49. };
  50. if (!state.first) state.first = entry;
  51. if (previous) previous.next = entry;
  52. if (DESCRIPTORS) state.size++;
  53. else that.size++;
  54. // add to index
  55. if (index !== 'F') state.index[index] = entry;
  56. } return that;
  57. };
  58. var getEntry = function (that, key) {
  59. var state = getInternalState(that);
  60. // fast case
  61. var index = fastKey(key);
  62. var entry;
  63. if (index !== 'F') return state.index[index];
  64. // frozen object case
  65. for (entry = state.first; entry; entry = entry.next) {
  66. if (entry.key === key) return entry;
  67. }
  68. };
  69. defineBuiltIns(Prototype, {
  70. // `{ Map, Set }.prototype.clear()` methods
  71. // https://tc39.es/ecma262/#sec-map.prototype.clear
  72. // https://tc39.es/ecma262/#sec-set.prototype.clear
  73. clear: function clear() {
  74. var that = this;
  75. var state = getInternalState(that);
  76. var entry = state.first;
  77. while (entry) {
  78. entry.removed = true;
  79. if (entry.previous) entry.previous = entry.previous.next = null;
  80. entry = entry.next;
  81. }
  82. state.first = state.last = null;
  83. state.index = create(null);
  84. if (DESCRIPTORS) state.size = 0;
  85. else that.size = 0;
  86. },
  87. // `{ Map, Set }.prototype.delete(key)` methods
  88. // https://tc39.es/ecma262/#sec-map.prototype.delete
  89. // https://tc39.es/ecma262/#sec-set.prototype.delete
  90. 'delete': function (key) {
  91. var that = this;
  92. var state = getInternalState(that);
  93. var entry = getEntry(that, key);
  94. if (entry) {
  95. var next = entry.next;
  96. var prev = entry.previous;
  97. delete state.index[entry.index];
  98. entry.removed = true;
  99. if (prev) prev.next = next;
  100. if (next) next.previous = prev;
  101. if (state.first === entry) state.first = next;
  102. if (state.last === entry) state.last = prev;
  103. if (DESCRIPTORS) state.size--;
  104. else that.size--;
  105. } return !!entry;
  106. },
  107. // `{ Map, Set }.prototype.forEach(callbackfn, thisArg = undefined)` methods
  108. // https://tc39.es/ecma262/#sec-map.prototype.foreach
  109. // https://tc39.es/ecma262/#sec-set.prototype.foreach
  110. forEach: function forEach(callbackfn /* , that = undefined */) {
  111. var state = getInternalState(this);
  112. var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined);
  113. var entry;
  114. while (entry = entry ? entry.next : state.first) {
  115. boundFunction(entry.value, entry.key, this);
  116. // revert to the last existing entry
  117. while (entry && entry.removed) entry = entry.previous;
  118. }
  119. },
  120. // `{ Map, Set}.prototype.has(key)` methods
  121. // https://tc39.es/ecma262/#sec-map.prototype.has
  122. // https://tc39.es/ecma262/#sec-set.prototype.has
  123. has: function has(key) {
  124. return !!getEntry(this, key);
  125. }
  126. });
  127. defineBuiltIns(Prototype, IS_MAP ? {
  128. // `Map.prototype.get(key)` method
  129. // https://tc39.es/ecma262/#sec-map.prototype.get
  130. get: function get(key) {
  131. var entry = getEntry(this, key);
  132. return entry && entry.value;
  133. },
  134. // `Map.prototype.set(key, value)` method
  135. // https://tc39.es/ecma262/#sec-map.prototype.set
  136. set: function set(key, value) {
  137. return define(this, key === 0 ? 0 : key, value);
  138. }
  139. } : {
  140. // `Set.prototype.add(value)` method
  141. // https://tc39.es/ecma262/#sec-set.prototype.add
  142. add: function add(value) {
  143. return define(this, value = value === 0 ? 0 : value, value);
  144. }
  145. });
  146. if (DESCRIPTORS) defineBuiltInAccessor(Prototype, 'size', {
  147. configurable: true,
  148. get: function () {
  149. return getInternalState(this).size;
  150. }
  151. });
  152. return Constructor;
  153. },
  154. setStrong: function (Constructor, CONSTRUCTOR_NAME, IS_MAP) {
  155. var ITERATOR_NAME = CONSTRUCTOR_NAME + ' Iterator';
  156. var getInternalCollectionState = internalStateGetterFor(CONSTRUCTOR_NAME);
  157. var getInternalIteratorState = internalStateGetterFor(ITERATOR_NAME);
  158. // `{ Map, Set }.prototype.{ keys, values, entries, @@iterator }()` methods
  159. // https://tc39.es/ecma262/#sec-map.prototype.entries
  160. // https://tc39.es/ecma262/#sec-map.prototype.keys
  161. // https://tc39.es/ecma262/#sec-map.prototype.values
  162. // https://tc39.es/ecma262/#sec-map.prototype-@@iterator
  163. // https://tc39.es/ecma262/#sec-set.prototype.entries
  164. // https://tc39.es/ecma262/#sec-set.prototype.keys
  165. // https://tc39.es/ecma262/#sec-set.prototype.values
  166. // https://tc39.es/ecma262/#sec-set.prototype-@@iterator
  167. defineIterator(Constructor, CONSTRUCTOR_NAME, function (iterated, kind) {
  168. setInternalState(this, {
  169. type: ITERATOR_NAME,
  170. target: iterated,
  171. state: getInternalCollectionState(iterated),
  172. kind: kind,
  173. last: null
  174. });
  175. }, function () {
  176. var state = getInternalIteratorState(this);
  177. var kind = state.kind;
  178. var entry = state.last;
  179. // revert to the last existing entry
  180. while (entry && entry.removed) entry = entry.previous;
  181. // get next entry
  182. if (!state.target || !(state.last = entry = entry ? entry.next : state.state.first)) {
  183. // or finish the iteration
  184. state.target = null;
  185. return createIterResultObject(undefined, true);
  186. }
  187. // return step by kind
  188. if (kind === 'keys') return createIterResultObject(entry.key, false);
  189. if (kind === 'values') return createIterResultObject(entry.value, false);
  190. return createIterResultObject([entry.key, entry.value], false);
  191. }, IS_MAP ? 'entries' : 'values', !IS_MAP, true);
  192. // `{ Map, Set }.prototype[@@species]` accessors
  193. // https://tc39.es/ecma262/#sec-get-map-@@species
  194. // https://tc39.es/ecma262/#sec-get-set-@@species
  195. setSpecies(CONSTRUCTOR_NAME);
  196. }
  197. };