UNPKG

mozu-node-sdk

Version:

Mozu JavaScript SDK for Node.js and Arc.js environments

35 lines (33 loc) 942 B
'use strict'; /** * Memoized function to turn URI Template text strings into Template objects. * * Assumes that unescaped URI Template variables are required, * since they're always base URLs in the current codegen. * * @param {String} templateText The URI template string. * @returns {Template} Object with a `render` method and a `keysUsed` object. */ var expRe = /\{.+?\}/g; var varnameRe = /[\w_-]+/; function findKeys(rawTpt) { var matches = rawTpt.match(expRe); if (!matches) return []; return matches.map(function (x) { return x.match(varnameRe)[0]; }); } var uritemplate = require('uri-template'); var cache = {}; module.exports = function (templateText) { if (cache[templateText]) { return cache[templateText]; } var tpt = uritemplate.parse(templateText); return cache[templateText] = { render: function render(x) { return tpt.expand(x); }, keysUsed: findKeys(templateText) }; };