append-if
v1.0.0
Published
Append a string, conditionally
Downloads
10
Maintainers
Readme
append-if
Append a string, conditionally
Install
npm install --save append-if
Usage
ES2015
import appendIf from 'append-if';
appendIf('hello', ' world');
// => 'hello world'
appendIf('hello', 'lo');
// => 'hello'
appendIf('hello', ' world', true);
// => 'hello world'
appendIf('hello', ' world', false);
// => 'hello'
function customCondition(string, appendString) {
return string.length > appendString.length;
}
appendIf('longer', 'short', customCondition);
// => 'longershort'
appendIf('short', 'longer', customCondition);
// => 'short'
ES5
import appendIf from 'append-if';
appendIf('hello', ' world');
// => 'hello world'
appendIf('hello', 'lo');
// => 'hello'
appendIf('hello', ' world', true);
// => 'hello world'
appendIf('hello', ' world', false);
// => 'hello'
function customCondition(string, appendString) {
return string.length > appendString.length;
}
appendIf('longer', 'short', customCondition);
// => 'longershort'
appendIf('short', 'longer', customCondition);
// => 'short'
API
appendIf(string, appendString[, condition])
string
Type: string
String to append if condition is true.
appendString
Type: string
String to append to string if condition is true.
condition
Type: function
|| boolean
Function to evaluate to determine if string should be appended with appendString. condition is given string and appendString as parameters. Default
condition checks if string ends with appendString. If not, appendString is appended to string.
Condition may also be a boolean
. If true
, string is appended with appendString, otherwise it is not.
Related
LICENSE
MIT © Dustin Specker