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