1.1.0 • Published 2 years ago

texting-squirrel v1.1.0

Weekly downloads
86
License
MIT
Repository
github
Last release
2 years ago

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)

ParamTypeDescription
indexnumberIndex in the args of the format function
astringText which will be returned if the value is equal to 1
bstringText which will be returned if the value is equal to 2, 3 or 4
cstringText which will be returned if the value is equal to 0 or bigger than 4

gender(index, a, b, c)

ParamTypeDescription
indexnumberIndex in the args of the format function
astringText which will be returned if the value is equal to male
bstringText which will be returned if the value is equal to female
cstringText 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.

ParamTypeDescription
keystring | Object.<string, string>Key with which is the dictionary accesible to format texts. If it's an object, the key is default.
dictionaryObject.<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.

ParamTypeDescription
keystringKey of the dictionary.

setDictionary(key) ⇒ Text

Sets the actual dictionary.

Kind: global function Returns: Text - Instance of the Text class.

ParamTypeDescription
keystringKey fo the dictionary.

addFunction(name, fn) ⇒ Text

Adds the function.

Kind: global function Returns: Text - Instance of the Text class.

ParamTypeDescription
namestringName of the function.
fnfunctionFunction 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.

ParamTypeDescription
keystringKey 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.

ParamTypeDescription
textstringText 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
1.1.0

2 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.5

4 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago