texting-squirrel v1.1.0
Texting Squirrel
Module to format texts with variables and functions. It supports multiple dictionaries.
Installation
$ npm install texting-squirrel
Usage
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
Text functions
Functions can be registered to the module. The texts can be updated with those funtions
count(index, a, b, c)
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 |
gender(index, a, b, c)
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
Custom fuctions can be registered and called
Text.addFunction('date', () => new Date().toISOString());
console.log(Text.format('{date()}')); // will output [actual date in iso format]
TODO
- passing JSON in the text -> every text in braces is cleared
Functions
addDictionary(key, dictionary) ⇒ Text
Adds the new dictionary.
Kind: global function Returns: Text - Instance of the Text class.
Param | Type | Description |
---|---|---|
key | string | Object.<string, string> | Key with which is the dictionary accesible to format texts. If it's an object, the key is default. |
dictionary | Object.<string, string> | Key-value object for mapping the texts. |
getDictionary(key) ⇒ Dictionary
Gets the dictionary by the key.
Kind: global function Returns: Dictionary - Dictionary instance or null.
Param | Type | Description |
---|---|---|
key | string | Key of the dictionary. |
setDictionary(key) ⇒ Text
Sets the actual dictionary.
Kind: global function Returns: Text - Instance of the Text class.
Param | Type | Description |
---|---|---|
key | string | Key fo the dictionary. |
addFunction(name, fn) ⇒ Text
Adds the function.
Kind: global function Returns: Text - Instance of the Text class.
Param | Type | Description |
---|---|---|
name | string | Name of the function. |
fn | function | Function to execute. |
get(key, ...args) ⇒ string
Formats the text from the set dictionary. The text is searched as a value in the dictionary by defined key. If the key is not found in set dictionary it tries to find the text in default dictionary.
Kind: global function Returns: string - Formatted text.
Param | Type | Description |
---|---|---|
key | string | Key of the text in the dictionary. |
...args | * | Arguments to pass in format method. |
format(text, ...args) ⇒ string
If there's a number in braces the value to replace is searched in the args by orIf it's a function in the braces the function is called. First parameter acceptsIt it's a string in the braces the value to replace is searched in the set dictionary.
Kind: global function Returns: string - Formatted text.
Param | Type | Description |
---|---|---|
text | string | Text to format. |
...args | * | Arguments to pass in the text by index. |
Example (Passing variables)
Text.format('{0}', 'one'); // returns one
Text.format('{0} {1} {2}', 'one', 'two', 'three'); // returns one two three
Example (Count function)
Text.format('{count(0, script, scripts, scripts)}', 5); // returns 5 scripts
Example (Gender function)
Text.format('{gender(0, He, She, He/She)} codes', 'female'); //returns She codes