js-stdlib-extensions / Exports / StringExtensions / String
StringExtensions.String
String
▸ format(...args
): string
Returns a formatted string using printf-like tokens, %s, for string substitution.
Name | Type | Description |
---|---|---|
...args |
any [] |
Any amount of arguments to substitute for the same amount of %s in the originating string. |
string
Example
// returns 'Hello world, today is Monday.'
'Hello %s, today is %s.'.format('world', 'Monday')
▸ isEmpty(): boolean
Returns true if the length of the string is 0.
boolean
Example
// returns true
''.isEmpty()
▸ toObject(options?
): Object
Returns a one-dimensional (1-D) object from a (multi-line) template literal string that uses a separator, =, to distinguish key from value, while using new lines to separate key-value pairs. A callback function can also be provided to transform the keys and/or values before they are returned.
Name | Type | Description |
---|---|---|
options? |
Object |
- |
options.callbackFn? |
(value : [k: string, v: any]) => [k: string, v: any] |
If provided, the function will be used to transform the key-value pairs before output. |
options.separator? |
string |
(Default: '=') If provided, the character(s) will be used to demarcate key-value pairs on a single line. |
Object
Example
// returns { name: 'Jack', age: 42, single: false }
`name=Jack
age=42
single=false`.toObject()
Example
// returns { name: 'Mary', age: 25, single: true }
`NAME Mary
AGE 25
SINGLE true`.toObject({separator: ' ', callbackFn: ([k, v]) => [k.toLowerCase(), v]})