@need-some/i18n v0.1.0
need-some-i18n.js
need-some is a collection of small yet useful functions. The i18n package provides string formatting extensions
Installation
Simply install as dependency
npm install @need-some/i18n --save
String replacemant
The string replacer is a tool that recursively applies parameters to variables in string inputs.
import { StringReplacer } from '../module/stringreplacer';
const replacer = new StringReplacer();
console.log(replacer.replace('hello')); // the most simple test outputs the input string 'hello'
console.log(replacer.replace('hello ${var}', {var: 'world'})); // a plain parameter is replaced to 'hello world'
console.log(replacer.replace('hello ${var} from ${who}', {var: 'world', who: 'need-some'})); // multiple parameters as well to 'hello world from need-some'
variable replacement
The variable replacement allows formatting and recursive calls.
Formatters need to be registered before use
import { StringReplacer } from '../module/stringreplacer';
import { marshal } from '@need-some/basic/converter/converters';
const replacer = new StringReplacer();
replacer.registerFormatter('upper', marshal(s => s.toUpperCase()));
replacer.registerFormatter('constant', marshal(s => 'hello world'));
replacer.registerFormatter('name', marshal(s => s.firstname + ' ' + s.lastname));
Default formatters can be registered for tested types
replacer.registerResolver('constant', value => value === undefined);
replacer.registerResolver('name', value => value && value.firstname);
Note that the formatters need to accept values of the variable type if they are called without parenthesis, but an object of type {value:any, params:any[]} if called with parenthesis
replacer.registerFormatter('concat', marshal(s => s.value + s.params[0]));
If both should be acceptable, the formatter needs to distinguish
replacer.registerFormatter('concat', marshal(s => s.params === undefined ? s : (s.value + s.params[0])));
This way even complex object can be simply inserted
console.log(replacer.replace('hello ${var}', {var: {firstname: 'The', lastname: 'Doctor'}}));
rule | input | parameters | output |
---|---|---|---|
lookup variable | ${param} | {param: 'booga'} | booga |
lookup child variable | ${param.x} | {param: {x: 'booga'}} | booga |
recursive lookup | ${${param}} | { param: 'booga', booga: 'loo' } | loo |
recursive cyclic lookup | ${${${param}}} | { param: 'booga', booga: 'param' } | booga |
recursive lookup of complex string | ${x${param}.${booga}} | { param: 'booga', booga: 'loo', xbooga:{loo: 'loo2' }} | loo2 |
pipe processing | ${param.x | upper} | {param: {x: 'booga'}} | BOOGA |
pipe with parameters | ${var | concat(1)} | {var: 'variant', two:'2'} | variant1 |
pipe with variable parameters | ${var | concat(${two})} | {var: 'variant', two:'2'} | variant2 |
pipe with parameter replacement | ${var | concat(${three|upper})} | {var: 'variant', three:'three'} | variantTHREE |
multiple pipes | ${param.x | upper | concat(${param.x})} | {param: {x: 'booga'}} | BOOGAbooga |
methods
replace
Replace a string using the given parameters. The name can be used for a resolver or in a pipe call
- input The string that should be formatted.
- params The parameters to use in the replacement variables
returns a formatted string
replace(input: string, params?: anyParam): string
registerFormatter
Register a formatter with a given name. The name can be used for a resolver or in a pipe call.
Note that the same marshaller can be registered using different names with different default params
- name The name to register
- formatter The formatter function
defaultParams params that are applied, if the name is used without own parameters or in a resolver
registerFormatter(name: string, formatter: Marshaller<anyObject, anyObject>, defaultParams?: string[]): void
registerResolver
Register an automated formatter for defined objects. The resolvers are checked in the order they are registered.
Note that the resolvers are only used if no pipe is given at all.
- name The registered formatter to be used as default
check Function to check if a given object can be formatted
registerResolver(name: string, check: (object: anyObject) => boolean): void