-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (37 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
function repeat(item, times) {
return Array.from({ length: times }).map(function() {
return item;
}).join(' ');
}
function getMatches(regex, string) {
var match = null;
var matches = [];
while ((match = regex.exec(string)) !== null) {
matches.push(match);
}
return matches;
}
function x() {
var args = Array.from(arguments)
var template = args.shift()
if (!template) {
throw new Error('First argument must be template')
}
return template.raw
.map(function (template, index) {
if (index === args.length) {
return template
}
var regex = /([\s\S]*?)([^\s']+$)/gm;
var matches = getMatches(regex, template);
var targetMatch = matches.pop();
var firstMatch = targetMatch[1]
var target = targetMatch[2]
var leftString = matches.map(function (match) { return match[0];}).join('');
var rightString = firstMatch + repeat(target, args[index]);
return leftString + rightString;
})
.join('');
}
module.exports = x;