prepend-if
v0.2.1
Published
Prepend a string, conditionally
Downloads
1
Maintainers
Readme
prepend-if
Prepend a string, conditionally
Install
npm install --save prepend-if
Usage
ES2015
import prependIf from 'prepend-if';
prependIf('world', 'hello ');
// => 'hello world'
prependIf('hello', 'he');
// => 'hello'
prependIf('world', 'hello ', true);
// => 'hello world'
prependIf('world', 'hello ', false);
// => 'world'
function customCondition(string, prependString) {
return string.length > prependString.length;
}
prependIf('longer', 'short', customCondition);
// => 'shortlonger'
prependIf('short', 'longer', customCondition);
// => 'short'
ES5
var prependIf = require('prepend-if');
prependIf('world', 'hello ');
// => 'hello world'
prependIf('hello', 'he');
// => 'hello'
prependIf('world', 'hello ', true);
// => 'hello world'
prependIf('world', 'hello ', false);
// => 'world'
function customCondition(string, prependString) {
return string.length > prependString.length;
}
prependIf('longer', 'short', customCondition);
// => 'shortlonger'
prependIf('short', 'longer', customCondition);
// => 'short'
API
prependIf(string, prependString[, condition])
string
Type: string
String to prepend if condition is true.
prependString
Type: string
String to prepend to string if condition is true.
condition
Type: function
|| boolean
Function to evaluate to determine if string should be prepended with prependString. condition is given string and prependString as parameters. Default
condition checks if string starts with prependString. If not, prependString is prepended to string.
Condition may also be a boolean
. If true
, string is prepended with prependString, otherwise it is not.
Related
LICENSE
MIT © Dustin Specker