Last active
August 3, 2018 22:19
-
-
Save mtvbrianking/2ce77092092a45b95b313327a36bcec9 to your computer and use it in GitHub Desktop.
Custom dustjs helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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