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.

111 lines
3.4 KiB

4 years ago
  1. 'use strict';
  2. var trimStart = require('string.prototype.trimleft');
  3. var trimEnd = require('string.prototype.trimright');
  4. var inspect = require('object-inspect');
  5. var ES2018 = require('./es2018');
  6. var assign = require('./helpers/assign');
  7. var MAX_SAFE_INTEGER = require('./helpers/maxSafeInteger');
  8. var GetIntrinsic = require('./GetIntrinsic');
  9. var $TypeError = GetIntrinsic('%TypeError%');
  10. var ES2019 = assign(assign({}, ES2018), {
  11. // https://tc39.es/ecma262/#sec-add-entries-from-iterable
  12. AddEntriesFromIterable: function AddEntriesFromIterable(target, iterable, adder) {
  13. if (!this.IsCallable(adder)) {
  14. throw new $TypeError('Assertion failed: `adder` is not callable');
  15. }
  16. if (iterable == null) {
  17. throw new $TypeError('Assertion failed: `iterable` is present, and not nullish');
  18. }
  19. var iteratorRecord = this.GetIterator(iterable);
  20. while (true) { // eslint-disable-line no-constant-condition
  21. var next = this.IteratorStep(iteratorRecord);
  22. if (!next) {
  23. return target;
  24. }
  25. var nextItem = this.IteratorValue(next);
  26. if (this.Type(nextItem) !== 'Object') {
  27. var error = new $TypeError('iterator next must return an Object, got ' + inspect(nextItem));
  28. return this.IteratorClose(
  29. iteratorRecord,
  30. function () { throw error; } // eslint-disable-line no-loop-func
  31. );
  32. }
  33. try {
  34. var k = this.Get(nextItem, '0');
  35. var v = this.Get(nextItem, '1');
  36. this.Call(adder, target, [k, v]);
  37. } catch (e) {
  38. return this.IteratorClose(
  39. iteratorRecord,
  40. function () { throw e; }
  41. );
  42. }
  43. }
  44. },
  45. // https://ecma-international.org/ecma-262/10.0/#sec-flattenintoarray
  46. // eslint-disable-next-line max-params, max-statements
  47. FlattenIntoArray: function FlattenIntoArray(target, source, sourceLen, start, depth) {
  48. var mapperFunction;
  49. if (arguments.length > 5) {
  50. mapperFunction = arguments[5];
  51. }
  52. var targetIndex = start;
  53. var sourceIndex = 0;
  54. while (sourceIndex < sourceLen) {
  55. var P = this.ToString(sourceIndex);
  56. var exists = this.HasProperty(source, P);
  57. if (exists === true) {
  58. var element = this.Get(source, P);
  59. if (typeof mapperFunction !== 'undefined') {
  60. if (arguments.length <= 6) {
  61. throw new $TypeError('Assertion failed: thisArg is required when mapperFunction is provided');
  62. }
  63. element = this.Call(mapperFunction, arguments[6], [element, sourceIndex, source]);
  64. }
  65. var shouldFlatten = false;
  66. if (depth > 0) {
  67. shouldFlatten = this.IsArray(element);
  68. }
  69. if (shouldFlatten) {
  70. var elementLen = this.ToLength(this.Get(element, 'length'));
  71. targetIndex = this.FlattenIntoArray(target, element, elementLen, targetIndex, depth - 1);
  72. } else {
  73. if (targetIndex >= MAX_SAFE_INTEGER) {
  74. throw new $TypeError('index too large');
  75. }
  76. this.CreateDataPropertyOrThrow(target, this.ToString(targetIndex), element);
  77. targetIndex += 1;
  78. }
  79. }
  80. sourceIndex += 1;
  81. }
  82. return targetIndex;
  83. },
  84. // https://ecma-international.org/ecma-262/10.0/#sec-trimstring
  85. TrimString: function TrimString(string, where) {
  86. var str = this.RequireObjectCoercible(string);
  87. var S = this.ToString(str);
  88. var T;
  89. if (where === 'start') {
  90. T = trimStart(S);
  91. } else if (where === 'end') {
  92. T = trimEnd(S);
  93. } else if (where === 'start+end') {
  94. T = trimStart(trimEnd(S));
  95. } else {
  96. throw new $TypeError('Assertion failed: invalid `where` value; must be "start", "end", or "start+end"');
  97. }
  98. return T;
  99. }
  100. });
  101. module.exports = ES2019;