UNPKG

1.02 kBJavaScriptView Raw
1'use strict';
2var uncurryThis = require('../internals/function-uncurry-this');
3var toIndexedObject = require('../internals/to-indexed-object');
4var toString = require('../internals/to-string');
5var lengthOfArrayLike = require('../internals/length-of-array-like');
6
7var $TypeError = TypeError;
8var push = uncurryThis([].push);
9var join = uncurryThis([].join);
10
11// `String.cooked` method
12// https://tc39.es/proposal-string-cooked/
13module.exports = function cooked(template /* , ...substitutions */) {
14 var cookedTemplate = toIndexedObject(template);
15 var literalSegments = lengthOfArrayLike(cookedTemplate);
16 if (!literalSegments) return '';
17 var argumentsLength = arguments.length;
18 var elements = [];
19 var i = 0;
20 while (true) {
21 var nextVal = cookedTemplate[i++];
22 if (nextVal === undefined) throw new $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};