npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-manager

v2.0.2

Published

Lightweight extensible text manager allows to manage texts, e.g. localize texts in web app

Downloads

4

Readme

Text manager

npm version npm

Build Status Coverage Status License: MIT

Lightweight, extensible text manager for managing texts, such as localizing texts in a web application

Installation

npm i text-manager -S

TL;DR

// 1. initialize
import { createDefaultTextManager } from 'text-manager';
const textManager = createDefaultTextManager();

// 2. register texts
const buttonTexts = {
  'button.open': 'Open',
  'button.open_in': 'Open in {{0}} seconds',
  'button.close_in': 'Close in {{seconds}} seconds',
};
textManager.addTexts('buttons', buttonTexts);

// 3. use
textManager.getText('button.open') === 'Open'; // get a text without parameters
textManager.getText('button.open_in', [5]) === 'Open in 5 seconds'; // get a text with ordered parameters
textManager.getText('button.close_in', { seconds: 5 }) === 'Close in 5 seconds'; // get a text with named parameters

Getting started

Text manager is used to get a text or a parameterized text by the code. It can also be extended for additional text changes or modifications via middleware, e.g. hiding sensitive information.

Texts set

Texts set is a plain object where a key is the code and a value is the text. Text may contain or may not contain parameters. Parameter must be surrounded with double braces {{<parameter>}}. Parameter must be specified by the name or a sequence number.

Example:

{
  "button.open": "Open",
  "button.open_in": "Open in {{0}} seconds",
  "button.close_in": "Close in {{seconds}} seconds"
}

Default text manager

Default TextManager is used get a parameterized text by the code. If a text is not found it returns the code.

Example:

import { createDefaultTextManager } from 'text-manager';
const textManager = createDefaultTextManager();
textManager.addTexts('buttons', buttonTexts);

textManager.getText('button.open'); // 'Open'
textManager.getText('button.open_in', [5]); // 'Open in 5 seconds'
textManager.getText('button.close_in', { seconds: 5 }); // 'Close in 5 seconds'
textManager.getText('button.save'); // 'button.save' - there is no text for the code

Middleware

In some cases addition text modifications are needed, e.g. parsing markdown, formatting numbers, dates and etc. This can be reached by extending TextManager with a custom list of middleware.

Middleware is a function which receives text, parameters and code and must return a text. Middleware functions are being executed one by one in sequence. Each of them receives text from the previous one. The first one receives original text or undefined if it doesn't exist. The result of the last one will be returned by TextManger.getText.

The following middleware functions are built-in and used in createDefaultTextManager

  • import { insert-params } from 'text-manage' - inserts params in the text
  • import { no-text-fallback } from 'text-manage' - returns the code if a text is not found

Example:

import TextManager from 'text-manager';

function CustomMiddleware1(code, text) {
  return text + ' Hello';
}

function CustomMiddleware2(code, text) {
  return text + ' World!';
}

const buttonTexts = {
  'button.open': 'Open',
};
const textManager = new TextManager([CustomMiddleware1, CustomMiddleware2]);
textManager.addTexts('buttons', buttonTexts);

// get text
textManager.getText('button.open') === 'Open Hello World!';

API

TextManager

  • constructor(middleware)

    • middleware, array of middleware functions
  • addTexts(id, texts)

    • id, string - required, the id of a texts
    • texts, object - required, a flat object where key is a code and value is a text, e.g. { 'button.open': 'Open' }
  • getText(code, parameters)

    • code, string - required, the code of a text
    • parameters, array/object - parameters, e.g. [5, 'abc'], { seconds: 5 }

Middleware

  • function(code, text, parameters) - returns string
    • code, string - the code of a text
    • text, string - initial text or text from previous middleware
    • parameters, object/array - parameters