append-if

Append a string, conditionally

Usage no npm install needed!

<script type="module">
  import appendIf from 'https://cdn.skypack.dev/append-if';
</script>

README

append-if

NPM version Build Status Coverage Status

Code Climate Dependencies DevDependencies

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