Module to format texts with variables and functions. It supports multiple dictionaries.
$ npm install texting-squirrel
import Text from 'texting-squirrel';
console.log(Text.format('Hi, {0}!', 'developer')); // will output Hi, developer!
Text.addDictionary('test', { module_name: 'Texting Squirrel' });
Text.setDictionary('test');
console.log(Text.get('module_name')); // will output Texting Squirrel
console.log(Text.format('I am using {module_name} module')); // will output I am using Texting Squirrel module
The text module can be set in two modes production
or dev
with setMode
method. In the dev
mode warnings are logged in console and if the text is not found in dictionary the key is returned instead of empty string.
Functions can be registered to the module. The texts can be updated with those functions.
Param | Type | Description |
---|---|---|
index | number |
Index in the args of the format function |
a | string |
Text which will be returned if the value is equal to 1 |
b | string |
Text which will be returned if the value is equal to 2 , 3 or 4 |
c | string |
Text which will be returned if the value is equal to 0 or bigger than 4 |
Param | Type | Description |
---|---|---|
index | number |
Index in the args of the format function |
a | string |
Text which will be returned if the value is equal to male |
b | string |
Text which will be returned if the value is equal to female |
c | string |
Text which will be returned if the value is not defined or something else than male or female |
import Text from 'texting-squirrel';
console.log(Text.format('{count(0, script, scripts, scripts)}', 5)); // will output 5 scripts
console.log(Text.format('{gender(0, He, She, He/She)} codes', 'female')); // will output She codes
Custom functions can be registered and called
Text.addFunction('date', () => new Date().toISOString());
console.log(Text.format('{date()}')); // will output [actual date in iso format]
Checkout the documentation here.
Generated using TypeDoc