es.string.raw.js 998 B

123456789101112131415161718192021222324252627
  1. var $ = require('../internals/export');
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var toIndexedObject = require('../internals/to-indexed-object');
  4. var toObject = require('../internals/to-object');
  5. var toString = require('../internals/to-string');
  6. var lengthOfArrayLike = require('../internals/length-of-array-like');
  7. var push = uncurryThis([].push);
  8. var join = uncurryThis([].join);
  9. // `String.raw` method
  10. // https://tc39.es/ecma262/#sec-string.raw
  11. $({ target: 'String', stat: true }, {
  12. raw: function raw(template) {
  13. var rawTemplate = toIndexedObject(toObject(template).raw);
  14. var literalSegments = lengthOfArrayLike(rawTemplate);
  15. if (!literalSegments) return '';
  16. var argumentsLength = arguments.length;
  17. var elements = [];
  18. var i = 0;
  19. while (true) {
  20. push(elements, toString(rawTemplate[i++]));
  21. if (i === literalSegments) return join(elements, '');
  22. if (i < argumentsLength) push(elements, toString(arguments[i]));
  23. }
  24. }
  25. });