esnext.string.cooked.js 1.2 KB

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