Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
hokey-cokey
Advanced tools
Render values into templates OR extract values out of templates. That's what it's all about.
Hokey Cokey is a quick tool for matching placeholders from, and inserting placeholders into, template strings. Useful for cases where RegExps are too fussy and you want something chunky and robust without having to worry about edge cases.
Match: Match a target string against a template and return a list of found values
e.g. places/:country/{city}
+ places/france/paris
= { country: "france", city: "paris" }
Render: Take a template string, merge in some values, and return the rendered string.
e.g. places/${country}/:city
+ { country: "france", city: "paris" }
= places/france/paris
Things to know:
:express
, {jsx}
, {{handlebars}}
or ${es6}
[a-zA-Z][a-zA-Z0-9]
*
placeholder (matched values are zero-indexed)npm install hokey-cokey
match(templates: string|string[], target: string)
Matches a template string against a target string and return an object containing values corresponding to the placeholders.
templates
can be...
target
)target
)target
must be the string to match against templates
placeholder: value
formatconst { match } = require('hokey-cokey');
// Match named placeholders in template.
match("places/{country}/{city}", "places/france/paris"); // { country: "france", city: "paris" }
match("places/:country/:city", "places/france/paris"); // { country: "france", city: "paris" }
// Match numbered placeholders in template.
match("*-*-*", "A-B-C"); // { "0": "A", "1": "B", "2": "C }
// Match several possible templates.
const templates = [
"${dog}===${cat}",
"{name}@{domain}",
"places/:country/:city",
];
match(templates, "places/france/paris"); // { country: "france", city: "paris" }
Template must have one character between each placeholder or an error will be thrown:
match("{placeholders}{with}{no}{gap}", "abcd"); // throws SyntaxError "template: Placeholders must be separated by at least one character"
render(template: string, values: Object|Function|string)
Render a set of values into a template string.
template
must be a string containing one or more placeholders in any allowed formatvalues
can be:
const { render } = require("hokey-cokey");
// Render named values into template.
render("blogs-:category-:slug", { category: "cheeses", slug: "stilton" }); // blogs-cheeses-stilton
render("blogs-:category-:slug", "Arrrrgh"); // blogs-Arrrrgh-Arrrrgh
render("blogs-:category-:slug", (p) => p.toUpperCase()); // blogs-CATEGORY-SLUG
// Render numbered values into template (using an array works!)
render("*-*-*", ["A", "B", "C"]); // A-B-C
If using an object with values the keys must correspond to placeholders in the template or render will fail:
render("{name}-{date}", { name: "Dave" }); // Throws ReferenceError "values.date: Must be defined"
render("*-*", { "0": "Dave" }); // Throws ReferenceError "values.1: Must be defined"
placeholders(template: string)
Parse a template string and return an array of found placeholders.
template
must be a string containing one or more placeholders in any allowed formatconst { placeholders } = require("hokey-cokey");
// Extract the placeholder names.
placeholders("{username}@{domain}"); // ["username", "domain"]
placeholders(":name // ${age}"); // ["name", "age"]
FAQs
Render values into templates OR extract values out of templates. That's what it's all about.
The npm package hokey-cokey receives a total of 1 weekly downloads. As such, hokey-cokey popularity was classified as not popular.
We found that hokey-cokey demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.