es2016.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. var GetIntrinsic = require('./GetIntrinsic');
  3. var $Array = GetIntrinsic('%Array%');
  4. var hasSymbols = require('has-symbols')();
  5. var ES2015 = require('./es2015');
  6. var assign = require('./helpers/assign');
  7. var callBind = require('./helpers/callBind');
  8. var $arrayPush = callBind($Array.prototype.push);
  9. var $arraySlice = callBind($Array.prototype.slice);
  10. var $arrayJoin = callBind($Array.prototype.join);
  11. var ES2016 = assign(assign({}, ES2015), {
  12. // https://www.ecma-international.org/ecma-262/7.0/#sec-samevaluenonnumber
  13. SameValueNonNumber: function SameValueNonNumber(x, y) {
  14. if (typeof x === 'number' || typeof x !== typeof y) {
  15. throw new TypeError('SameValueNonNumber requires two non-number values of the same type.');
  16. }
  17. return this.SameValue(x, y);
  18. },
  19. // https://www.ecma-international.org/ecma-262/7.0/#sec-iterabletoarraylike
  20. IterableToArrayLike: function IterableToArrayLike(items) {
  21. var usingIterator;
  22. if (hasSymbols) {
  23. usingIterator = this.GetMethod(items, Symbol.iterator);
  24. } else if (this.IsArray(items)) {
  25. usingIterator = function () {
  26. var i = -1;
  27. var arr = this; // eslint-disable-line no-invalid-this
  28. return {
  29. next: function () {
  30. i += 1;
  31. return {
  32. done: i >= arr.length,
  33. value: arr[i]
  34. };
  35. }
  36. };
  37. };
  38. } else if (this.Type(items) === 'String') {
  39. var ES = this;
  40. usingIterator = function () {
  41. var i = 0;
  42. return {
  43. next: function () {
  44. var nextIndex = ES.AdvanceStringIndex(items, i, true);
  45. var value = $arrayJoin($arraySlice(items, i, nextIndex), '');
  46. i = nextIndex;
  47. return {
  48. done: nextIndex > items.length,
  49. value: value
  50. };
  51. }
  52. };
  53. };
  54. }
  55. if (typeof usingIterator !== 'undefined') {
  56. var iterator = this.GetIterator(items, usingIterator);
  57. var values = [];
  58. var next = true;
  59. while (next) {
  60. next = this.IteratorStep(iterator);
  61. if (next) {
  62. var nextValue = this.IteratorValue(next);
  63. $arrayPush(values, nextValue);
  64. }
  65. }
  66. return values;
  67. }
  68. return this.ToObject(items);
  69. }
  70. });
  71. module.exports = ES2016;