JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 30
  • Score
    100M100P100Q54868F
  • License MIT

Tiny library for dynamic text hydrating

Package Exports

  • hydrate-text

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hydrate-text) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hydrate-text

Tiny library for dynamic text hydrating

Version MIT License GitHub Release

Minified size Minified and gzipped size Dependencies Status devDependencies Status

Build Status Code Coverage Maintainability Known Vulnerabilities

Installation

npm install hydrate-text

Features

  • Dependency-free
  • Light-weight
  • Works with variables as an object and an array
  • Flexible variable syntax change
  • Strongly typed with TypeScript

Example

import { hydrateText } from 'hydrate-text';

const text1 = 'Hello, {username}!';
const text2 = 'I have to {0} and {1}.';
const route = '/users/:userId';

// 'Hello, John Doe!'
console.log(hydrateText(text1, { username: 'John Doe' }));

// 'I have to run tests and make tea.'
console.log(hydrateText(text2, ['run tests', 'make tea']));

// '/users/1'
console.log(hydrateText(route, { userId: 1 }, { start: ':' }));

/* Can be configured */
import { configureHydrateText } from 'hydrate-text';

const route = '/users/:userId';
const routeWithDifferentBorders = '/users/(userId)';

const replaceRouteVariables = configureHydrateText({ start: ':' });

// '/users/1'
replaceRouteVariables(route, { userId: 1 });

// '/users/1'
replaceRouteVariables(
  routeWithDifferentBorders,
  { userId: 1 },
  {
    start: '(',
    end: ')',
  },
);