https://github.com/dustinspecker/append-if
Append a string, conditionally
https://github.com/dustinspecker/append-if
Last synced: 3 months ago
JSON representation
Append a string, conditionally
- Host: GitHub
- URL: https://github.com/dustinspecker/append-if
- Owner: dustinspecker
- License: mit
- Created: 2015-09-10T17:00:43.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-05-31T21:40:48.000Z (about 8 years ago)
- Last Synced: 2025-03-16T15:52:08.522Z (4 months ago)
- Language: JavaScript
- Size: 19.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# append-if
[](https://badge.fury.io/js/append-if) [](https://travis-ci.org/dustinspecker/append-if) [](https://coveralls.io/r/dustinspecker/append-if?branch=master)[](https://codeclimate.com/github/dustinspecker/append-if) [](https://david-dm.org/dustinspecker/append-if/#info=dependencies&view=table) [](https://david-dm.org/dustinspecker/append-if/#info=devDependencies&view=table)
[](https://github.com/semantic-release/semantic-release)
> Append a string, conditionally
## Install
```
npm install --save append-if
```## Usage
### ES2015
```javascript
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
```javascript
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
- [prepend-if](https://github.com/dustinspecker/prepend-if)## LICENSE
MIT © [Dustin Specker](https://github.com/dustinspecker)