Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active August 3, 2018 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/2ce77092092a45b95b313327a36bcec9 to your computer and use it in GitHub Desktop.
Save mtvbrianking/2ce77092092a45b95b313327a36bcec9 to your computer and use it in GitHub Desktop.
Custom dustjs helpers
// https://github.com/linkedin/dustjs-helpers
// https://github.com/rodw/common-dustjs-helpers
dust.helpers.slug = function (chunk, context, bodies, params) {
//replace all special characters | symbols with a space
params.value = params.value.replace(/[`~!@#$%^&*()_\-+=\[\]{};:'"\\|\/,.<>?\s]/g, ' ').toLowerCase();
// trim spaces at start and end of string
params.value = params.value.replace(/^\s+|\s+$/gm, '');
// replace space with dash/hyphen
params.value = params.value.replace(/\s+/g, '-');
return chunk.write(params.value);
};
>> Usage
{
"str":"Hello world"
}
{@slug value={str}/}
>> Output
hello-world
---------------------------------
dust.helpers.regexp = function(chunk, context, bodies, params) {
var regexp = new RegExp(params.pattern, params.flag);
if (regexp.test(params.term)) {
return chunk.render(bodies.block, context);
} else {
return chunk.render(bodies['else'], context);
}
}
>> Usage
{
"notes":"Starts with 's'"
}
{@regexp term={notes} pattern="^[sS](\w+)" flag="g"}
sample: {notes}
{:else}
{notes}
{/regexp}
>> Output
sample: Starts with 's'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment