Elegant way to generate string from tagged template?

Peter Seliger peter.seliger at googlemail.com
Tue Dec 23 06:29:47 PST 2014


> Since the target (templateObj) is an array, I think the method should belong to Array, and it should behave like a join, but instead of joining array items with a string, it joins with many strings. Maybe something like Array.prototype.interspersedJoin (much like your String.default):
>
> [1,2,3].interspersedJoin(“a”, “b”) = “1a2b3”
> [1,2,3].interspersedJoin(“a”) = “1a23”
> [1,2,3].interspersedJoin(“a”,”b”,”c”) = “1a2b3c”

As for prototypal array approaches, one might rather think of
[interlock] for it more precisely names, what you are going to
achieve, and it also is not as specialized as your suggestion for an
[interspersedJoin] already needs to be - how about this approach?

<code><pre>
(function (Array) {


  var
    array_prototype = Array.prototype,


    isFunction = (function (TYPEOF_FUNCTION_TYPE) {
      return function (type) {

        return (
             (typeof type == TYPEOF_FUNCTION_TYPE)
          && (typeof type.call == TYPEOF_FUNCTION_TYPE)
          && (typeof type.apply == TYPEOF_FUNCTION_TYPE)
        );
      };
    }("function")),

    array_from = (isFunction(Array.from) && Array.from) || (function
(array_prototype_slice) {
      return function (listType) {

        return array_prototype_slice.call(listType);
      };
    }(array_prototype.slice))
  ;


  array_prototype.interlock = function (listType) {
    var
      arr_A = array_from(this),
      arr_B = array_from(listType),

      len_A = arr_A.length,
      len_B = arr_B.length,
      len = (len_A + len_B),

      idx_A,
      idx_B,
      idx = idx_A = idx_B = -1,

      arr = []
    ;
    arr.length = len;

    while ((idx + 1) < len) {
//console.log("within while :: 1st part :: idx, len : ", idx, len);

      if (++idx_A < len_A) {
        ++idx;
        (idx_A in arr_A) && (arr[idx] = arr_A[idx_A]);
//console.log("within A condition :: idx_A, len_A, arr : ", idx_A, len_A, arr);
      }

//console.log("within while :: 2nd part :: idx, len : ", idx, len);

      if (++idx_B < len_B) {
        ++idx;
        (idx_B in arr_B) && (arr[idx] = arr_B[idx_B]);
//console.log("within B condition :: idx_B, len_B, arr : ", idx_B, len_B, arr);
      }
    }
    return arr;
  };


}(Array));


console.log('[1,2,3].interlock("a");', [1,2,3].interlock("a"));
console.log('[1,2,3].interlock(["a"]);', [1,2,3].interlock(["a"]));
console.log('[1,2,3].interlock("a").join("");',
[1,2,3].interlock("a").join(""));
console.log('[1,2,3].interlock(["a"]).join("");',
[1,2,3].interlock(["a"]).join(""));

console.log('[1,2,3].interlock("ab");', [1,2,3].interlock("ab"));
console.log('[1,2,3].interlock(["a", "b"]);', [1,2,3].interlock(["a", "b"]));
console.log('[1,2,3].interlock("ab").join("");',
[1,2,3].interlock("ab").join(""));
console.log('[1,2,3].interlock(["a", "b"]).join("");',
[1,2,3].interlock(["a", "b"]).join(""));

console.log('[1,2,3].interlock("abc");', [1,2,3].interlock("abc"));
console.log('[1,2,3].interlock(["a", "b", "c"]);',
[1,2,3].interlock(["a", "b", "c"]));
console.log('[1,2,3].interlock("abc").join("");',
[1,2,3].interlock("abc").join(""));
console.log('[1,2,3].interlock(["a", "b", "c"]).join("");',
[1,2,3].interlock(["a", "b", "c"]).join(""));


/*

console.log("[1,3,5,7,9,11,13].interlock([2,4,6]);",
""+[1,3,5,7,9,11,13].interlock([2,4,6]));
console.log("[2,4,6].interlock([1,3,5,7,9,11,13]);",
""+[2,4,6].interlock([1,3,5,7,9,11,13]));

console.log("[2,,,4,,,6].interlock([1,3,5,7,9,11,13,]);",
""+[2,,,4,,,6].interlock([1,3,5,7,9,11,13,]));
console.log("[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,]);",
""+[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,]));
console.log("[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,,]);",
""+[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,,]));
console.log("[2,,,4,,,6].interlock([1,3,,5,7,,9,,11,13,,,]);",
""+[2,,,4,,,6].interlock([1,3,,5,7,,9,,11,13,,,]));
console.log("[2,,,4,,,6].interlock([1,,3,,5,,7,,,9,,,11,13,,,]);",
""+[2,,,4,,,6].interlock([1,,3,,5,,7,,,9,,,11,13,,,]));

*/
</pre></code>


More information about the es-discuss mailing list