Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hokey-cokey

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hokey-cokey

Render values into templates OR extract values out of templates. That's what it's all about.

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
Maintainers
1
Weekly downloads
 
Created
Source

Hokey Cokey! Simple string template rendering and parameter value extracting

Travis CI semantic-release Conventional Commits Prettier npm

Hokey Cokey is a quick tool for extracting parameters from, and inserting parameters into, template strings:

Extract:

  • Match a target string against a template e.g. places/france/paris and places/:country/{city}
  • Return an object listing found values e.g. { country: "france", city: "paris" }

Render:

  • Take a template string and merge in some values e.g. places/${country}/:city and { country: "france", city: "paris" }
  • Return the rendered string after variables have been inserted e.g. places/france/paris

Things to know:

  • Works with parameters in :express, {jsx}, {{handlebars}} or ${es6} format.
  • Doesn't do anything complicated (like RegExp patterns, * glob patterns, or optional parameters).
  • Parameter names must match the simple format [a-zA-Z][a-zA-Z0-9].
  • When extracting values the template must have one character between each template parameter.
  • Template destructuring is cached internally for performance.
  • Well unit-tested and 100% covered.
  • Useful for cases where RegExps are too fussy and you want something chunky and robust.

Installation

npm install hokey-cokey

Usage

extract(template: string, target: string) Extract values from a string

Extracts a set of values out of a target string based on a template string.

  • template must be a string containing one or more parameters in any allowed format
  • Returns an object in parameter: value format
const { extract } = require('hokey-cokey');

// Extract the values.
extract("places/{country}/{city}", "places/france/paris"); // { country: "france", city: "paris" }
extract("places/:country/:city", "places/france/paris"); // { country: "france", city: "paris" }

render(template, values) Inject values into a template

Render a set of values into a template string.

  • template must be a string containing one or more parameters in any allowed format
  • values can be:
    • An object containing string or function keys (keys must correspond to parameters in the template or render will fail)
    • A function called for each parameter (receives the parameter name)
    • A single string used for all parameters
  • Returns the rendered string
const { render } = require("hokey-cokey");

// Render the template with an object.
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

params(template) Get parameter names from template string

Parse a template string and return an array of found variables that were found.

  • template must be a string containing one or more parameters in any allowed format
  • Returns an array of string parameter names that were found in the template
const { params } = require("hokey-cokey");

// Extract the parameter names.
params("{username}@{domain}"); // ["username", "domain"]
params(":name // ${age}"); // ["name", "age"]

Keywords

FAQs

Package last updated on 18 Jan 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc