UNPKG

2.01 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7
8var _number = _interopRequireDefault(require("./number.js"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12var reA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g,
13 reB = new RegExp(reA.source, "g");
14
15function zero(b) {
16 return function () {
17 return b;
18 };
19}
20
21function one(b) {
22 return function (t) {
23 return b(t) + "";
24 };
25}
26
27function _default(a, b) {
28 var bi = reA.lastIndex = reB.lastIndex = 0,
29 // scan index for next number in b
30 am,
31 // current match in a
32 bm,
33 // current match in b
34 bs,
35 // string preceding current number in b, if any
36 i = -1,
37 // index in s
38 s = [],
39 // string constants and placeholders
40 q = []; // number interpolators
41 // Coerce inputs to strings.
42
43 a = a + "", b = b + ""; // Interpolate pairs of numbers in a & b.
44
45 while ((am = reA.exec(a)) && (bm = reB.exec(b))) {
46 if ((bs = bm.index) > bi) {
47 // a string precedes the next number in b
48 bs = b.slice(bi, bs);
49 if (s[i]) s[i] += bs; // coalesce with previous string
50 else s[++i] = bs;
51 }
52
53 if ((am = am[0]) === (bm = bm[0])) {
54 // numbers in a & b match
55 if (s[i]) s[i] += bm; // coalesce with previous string
56 else s[++i] = bm;
57 } else {
58 // interpolate non-matching numbers
59 s[++i] = null;
60 q.push({
61 i: i,
62 x: (0, _number.default)(am, bm)
63 });
64 }
65
66 bi = reB.lastIndex;
67 } // Add remains of b.
68
69
70 if (bi < b.length) {
71 bs = b.slice(bi);
72 if (s[i]) s[i] += bs; // coalesce with previous string
73 else s[++i] = bs;
74 } // Special optimization for only a single match.
75 // Otherwise, interpolate each of the numbers and rejoin the string.
76
77
78 return s.length < 2 ? q[0] ? one(q[0].x) : zero(b) : (b = q.length, function (t) {
79 for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t);
80
81 return s.join("");
82 });
83}
\No newline at end of file