0.2.1 • Published 9 years ago

prepend-if v0.2.1

Weekly downloads
5
License
MIT
Repository
github
Last release
9 years ago

prepend-if

NPM version Build Status Coverage Status

Code Climate Dependencies DevDependencies

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