rchain / rchain-api

Obsoleted in favor of https://github.com/rchain-community/rchain-api. Use at your own risk.
Other
8 stars 4 forks source link

template tag: rho`...${x}...` #16

Closed dckc closed 5 years ago

dckc commented 5 years ago

Tonight's use of this brought up the issue that RHOCore doesn't have URIs.

// @flow strict

const { RHOCore } = require('rchain-api');
const { fromJSData, toRholang } = RHOCore;

/**
 * Rholang template string interpolation.
 * ISSUE: belongs in rchain-api's RHOCore.js
 */
// @flow strict
module.exports.rho = rho;
function rho(template /*: string[] */, ...subs /*: Json[] */) {
  const encoded = subs.map(it => toRholang(fromJSData(it)));

  const out = [];
  template.forEach((part, ix) => {
    out.push(part);
    out.push(encoded[ix]);
  });

  return out.join('');
}