MCPcopy Create free account
hub / github.com/breck7/scroll / interpolateString

Function interpolateString

external/.d3.js:3677–3723  ·  view source on GitHub ↗
(a, b)

Source from the content-addressed store, hash-verified

3675}
3676
3677function interpolateString(a, b) {
3678 var bi = reA.lastIndex = reB.lastIndex = 0, // scan index for next number in b
3679 am, // current match in a
3680 bm, // current match in b
3681 bs, // string preceding current number in b, if any
3682 i = -1, // index in s
3683 s = [], // string constants and placeholders
3684 q = []; // number interpolators
3685
3686 // Coerce inputs to strings.
3687 a = a + "", b = b + "";
3688
3689 // Interpolate pairs of numbers in a & b.
3690 while ((am = reA.exec(a))
3691 && (bm = reB.exec(b))) {
3692 if ((bs = bm.index) > bi) { // a string precedes the next number in b
3693 bs = b.slice(bi, bs);
3694 if (s[i]) s[i] += bs; // coalesce with previous string
3695 else s[++i] = bs;
3696 }
3697 if ((am = am[0]) === (bm = bm[0])) { // numbers in a & b match
3698 if (s[i]) s[i] += bm; // coalesce with previous string
3699 else s[++i] = bm;
3700 } else { // interpolate non-matching numbers
3701 s[++i] = null;
3702 q.push({i: i, x: interpolateNumber(am, bm)});
3703 }
3704 bi = reB.lastIndex;
3705 }
3706
3707 // Add remains of b.
3708 if (bi < b.length) {
3709 bs = b.slice(bi);
3710 if (s[i]) s[i] += bs; // coalesce with previous string
3711 else s[++i] = bs;
3712 }
3713
3714 // Special optimization for only a single match.
3715 // Otherwise, interpolate each of the numbers and rejoin the string.
3716 return s.length < 2 ? (q[0]
3717 ? one(q[0].x)
3718 : zero(b))
3719 : (b = q.length, function(t) {
3720 for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t);
3721 return s.join("");
3722 });
3723}
3724
3725function interpolate$2(a, b) {
3726 var t = typeof b, c;

Callers

nothing calls this directly

Calls 5

interpolateNumberFunction · 0.85
oneFunction · 0.85
zeroFunction · 0.85
sliceMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected