esnext.string.cooked.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. var $ = require('../internals/export');
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var toIndexedObject = require('../internals/to-indexed-object');
  4. var toString = require('../internals/to-string');
  5. var lengthOfArrayLike = require('../internals/length-of-array-like');
  6. var $TypeError = TypeError;
  7. var ArrayPrototype = Array.prototype;
  8. var push = uncurryThis(ArrayPrototype.push);
  9. var join = uncurryThis(ArrayPrototype.join);
  10. // `String.cooked` method
  11. // https://github.com/tc39/proposal-string-cooked
  12. $({ target: 'String', stat: true, forced: true }, {
  13. cooked: function cooked(template /* , ...substitutions */) {
  14. var cookedTemplate = toIndexedObject(template);
  15. var literalSegments = lengthOfArrayLike(cookedTemplate);
  16. var argumentsLength = arguments.length;
  17. var elements = [];
  18. var i = 0;
  19. while (literalSegments > i) {
  20. var nextVal = cookedTemplate[i++];
  21. if (nextVal === undefined) throw $TypeError('Incorrect template');
  22. push(elements, toString(nextVal));
  23. if (i === literalSegments) return join(elements, '');
  24. if (i < argumentsLength) push(elements, toString(arguments[i]));
  25. }
  26. }
  27. });