enumerate.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. "use strict";
  2. var _assign = require("object-assign");
  3. /**
  4. * Copyright (c) 2013-present, Facebook, Inc.
  5. *
  6. * This source code is licensed under the MIT license found in the
  7. * LICENSE file in the root directory of this source tree.
  8. *
  9. *
  10. */
  11. var KIND_KEYS = 'keys';
  12. var KIND_VALUES = 'values';
  13. var KIND_ENTRIES = 'entries';
  14. /**
  15. * Specific Array iterators.
  16. */
  17. var ArrayIterators = function () {
  18. var hasNative = hasNativeIterator(Array);
  19. var ArrayIterator;
  20. if (!hasNative) {
  21. ArrayIterator =
  22. /*#__PURE__*/
  23. function () {
  24. // 22.1.5.1 CreateArrayIterator Abstract Operation
  25. function ArrayIterator(array, kind) {
  26. this._iteratedObject = array;
  27. this._kind = kind;
  28. this._nextIndex = 0;
  29. } // 22.1.5.2.1 %ArrayIteratorPrototype%.next()
  30. var _proto = ArrayIterator.prototype;
  31. _proto.next = function next() {
  32. if (this._iteratedObject == null) {
  33. return {
  34. value: undefined,
  35. done: true
  36. };
  37. }
  38. var array = this._iteratedObject;
  39. var len = this._iteratedObject.length;
  40. var index = this._nextIndex;
  41. var kind = this._kind;
  42. if (index >= len) {
  43. this._iteratedObject = undefined;
  44. return {
  45. value: undefined,
  46. done: true
  47. };
  48. }
  49. this._nextIndex = index + 1;
  50. if (kind === KIND_KEYS) {
  51. return {
  52. value: index,
  53. done: false
  54. };
  55. } else if (kind === KIND_VALUES) {
  56. return {
  57. value: array[index],
  58. done: false
  59. };
  60. } else if (kind === KIND_ENTRIES) {
  61. return {
  62. value: [index, array[index]],
  63. done: false
  64. };
  65. }
  66. }; // 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator]()
  67. _proto[Symbol.iterator] = function () {
  68. return this;
  69. };
  70. return ArrayIterator;
  71. }();
  72. }
  73. return {
  74. keys: hasNative ? function (array) {
  75. return array.keys();
  76. } : function (array) {
  77. return new ArrayIterator(array, KIND_KEYS);
  78. },
  79. values: hasNative ? function (array) {
  80. return array.values();
  81. } : function (array) {
  82. return new ArrayIterator(array, KIND_VALUES);
  83. },
  84. entries: hasNative ? function (array) {
  85. return array.entries();
  86. } : function (array) {
  87. return new ArrayIterator(array, KIND_ENTRIES);
  88. }
  89. };
  90. }(); // -----------------------------------------------------------------
  91. /**
  92. * Specific String iterators.
  93. */
  94. var StringIterators = function () {
  95. var hasNative = hasNativeIterator(String);
  96. var StringIterator;
  97. if (!hasNative) {
  98. StringIterator =
  99. /*#__PURE__*/
  100. function () {
  101. // 21.1.5.1 CreateStringIterator Abstract Operation
  102. function StringIterator(string) {
  103. this._iteratedString = string;
  104. this._nextIndex = 0;
  105. } // 21.1.5.2.1 %StringIteratorPrototype%.next()
  106. var _proto2 = StringIterator.prototype;
  107. _proto2.next = function next() {
  108. if (this._iteratedString == null) {
  109. return {
  110. value: undefined,
  111. done: true
  112. };
  113. }
  114. var index = this._nextIndex;
  115. var s = this._iteratedString;
  116. var len = s.length;
  117. if (index >= len) {
  118. this._iteratedString = undefined;
  119. return {
  120. value: undefined,
  121. done: true
  122. };
  123. }
  124. var ret;
  125. var first = s.charCodeAt(index);
  126. if (first < 0xD800 || first > 0xDBFF || index + 1 === len) {
  127. ret = s[index];
  128. } else {
  129. var second = s.charCodeAt(index + 1);
  130. if (second < 0xDC00 || second > 0xDFFF) {
  131. ret = s[index];
  132. } else {
  133. ret = s[index] + s[index + 1];
  134. }
  135. }
  136. this._nextIndex = index + ret.length;
  137. return {
  138. value: ret,
  139. done: false
  140. };
  141. }; // 21.1.5.2.2 %StringIteratorPrototype%[@@iterator]()
  142. _proto2[Symbol.iterator] = function () {
  143. return this;
  144. };
  145. return StringIterator;
  146. }();
  147. }
  148. return {
  149. keys: function keys() {
  150. throw TypeError("Strings default iterator doesn't implement keys.");
  151. },
  152. values: hasNative ? function (string) {
  153. return string[Symbol.iterator]();
  154. } : function (string) {
  155. return new StringIterator(string);
  156. },
  157. entries: function entries() {
  158. throw TypeError("Strings default iterator doesn't implement entries.");
  159. }
  160. };
  161. }();
  162. function hasNativeIterator(classObject) {
  163. return typeof classObject.prototype[Symbol.iterator] === 'function' && typeof classObject.prototype.values === 'function' && typeof classObject.prototype.keys === 'function' && typeof classObject.prototype.entries === 'function';
  164. } // -----------------------------------------------------------------
  165. /**
  166. * Generic object iterator.
  167. */
  168. var ObjectIterator =
  169. /*#__PURE__*/
  170. function () {
  171. function ObjectIterator(object, kind) {
  172. this._iteratedObject = object;
  173. this._kind = kind;
  174. this._keys = Object.keys(object);
  175. this._nextIndex = 0;
  176. }
  177. var _proto3 = ObjectIterator.prototype;
  178. _proto3.next = function next() {
  179. var len = this._keys.length;
  180. var index = this._nextIndex;
  181. var kind = this._kind;
  182. var key = this._keys[index];
  183. if (index >= len) {
  184. this._iteratedObject = undefined;
  185. return {
  186. value: undefined,
  187. done: true
  188. };
  189. }
  190. this._nextIndex = index + 1;
  191. if (kind === KIND_KEYS) {
  192. return {
  193. value: key,
  194. done: false
  195. };
  196. } else if (kind === KIND_VALUES) {
  197. return {
  198. value: this._iteratedObject[key],
  199. done: false
  200. };
  201. } else if (kind === KIND_ENTRIES) {
  202. return {
  203. value: [key, this._iteratedObject[key]],
  204. done: false
  205. };
  206. }
  207. };
  208. _proto3[Symbol.iterator] = function () {
  209. return this;
  210. };
  211. return ObjectIterator;
  212. }();
  213. /**
  214. * Generic object iterator, iterates over all own enumerable
  215. * properties. Used only if if no specific iterator is available,
  216. * and object don't implement iterator protocol.
  217. */
  218. var GenericIterators = {
  219. keys: function keys(object) {
  220. return new ObjectIterator(object, KIND_KEYS);
  221. },
  222. values: function values(object) {
  223. return new ObjectIterator(object, KIND_VALUES);
  224. },
  225. entries: function entries(object) {
  226. return new ObjectIterator(object, KIND_ENTRIES);
  227. }
  228. }; // -----------------------------------------------------------------
  229. /**
  230. * Main iterator function. Returns default iterator based
  231. * on the class of an instance.
  232. */
  233. function enumerate(object, kind) {
  234. // First check specific iterators.
  235. if (typeof object === 'string') {
  236. return StringIterators[kind || KIND_VALUES](object);
  237. } else if (Array.isArray(object)) {
  238. return ArrayIterators[kind || KIND_VALUES](object); // Then see if an object implements own.
  239. } else if (object[Symbol.iterator]) {
  240. return object[Symbol.iterator](); // And fallback to generic with entries.
  241. } else {
  242. return GenericIterators[kind || KIND_ENTRIES](object);
  243. }
  244. }
  245. _assign(enumerate, {
  246. /**
  247. * Export constants
  248. */
  249. KIND_KEYS: KIND_KEYS,
  250. KIND_VALUES: KIND_VALUES,
  251. KIND_ENTRIES: KIND_ENTRIES,
  252. /**
  253. * Convenient explicit iterators for special kinds.
  254. */
  255. keys: function keys(object) {
  256. return enumerate(object, KIND_KEYS);
  257. },
  258. values: function values(object) {
  259. return enumerate(object, KIND_VALUES);
  260. },
  261. entries: function entries(object) {
  262. return enumerate(object, KIND_ENTRIES);
  263. },
  264. generic: GenericIterators.entries
  265. });
  266. module.exports = enumerate;