QuickString
A quick an easy string formatter / library.
Installation
Install with npm i quickstring
Usage
Define a set of strings as a JSON object, and pass to the constructor (could also be in a separate file, and imported)
import StringFormatter from 'quickstring';
const strings = {
myString: 'Hello, world!'
};
const {getString} = new StringFormatter(strings);
Retrieve strings by name with the getString
method
getString('myString'); // "Hello, world!"
Strings can be nested and retrieved with dot notation
const strings = {
data: {
myString: 'Hello, nested!'
}
};
...
getString('data.myString'); // "Hello, nested!"
Strings can be given placeholders, allowing you to interpolate data into them
const strings = {
myString: 'Hello, {name}!'
};
...
getString('myString', {name: 'John'}); // "Hello, John!"
getString('myString', {name: 'world'}); // "Hello, world!"
In the event you want a random string choice, an array can be added to the JSON and selected using the [?]
syntax.
const strings = {
random: [
'String A',
'String B'
]
};
...
getString('random[?]'); // "String A" or "String B"
Nested objects can be stored in arrays, but be aware that this can cause unexpected behavior if the nested schema is not consistent.
const string = {
random: [
{
data: 'Some data'
},
{
data: 'Some more data'
}
]
};
...
getString('random[?].data'); // "Some data" or "Some more data"
Running tests
Run tests simply with npm test
Code style enfored by XO
Test runner of choice is Ava
Contributing
Feel free to pick up any open issues and submit PRs!
License: MIT
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.