Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dustinspecker/prepend-if
Prepend a string, conditionally
https://github.com/dustinspecker/prepend-if
Last synced: 2 days ago
JSON representation
Prepend a string, conditionally
- Host: GitHub
- URL: https://github.com/dustinspecker/prepend-if
- Owner: dustinspecker
- License: mit
- Created: 2015-09-09T02:37:20.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-31T21:40:46.000Z (over 7 years ago)
- Last Synced: 2024-09-24T11:06:50.505Z (about 1 month ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# prepend-if
[![NPM version](https://badge.fury.io/js/prepend-if.svg)](https://badge.fury.io/js/prepend-if) [![Build Status](https://travis-ci.org/dustinspecker/prepend-if.svg)](https://travis-ci.org/dustinspecker/prepend-if) [![Coverage Status](https://img.shields.io/coveralls/dustinspecker/prepend-if.svg)](https://coveralls.io/r/dustinspecker/prepend-if?branch=master)[![Code Climate](https://codeclimate.com/github/dustinspecker/prepend-if/badges/gpa.svg)](https://codeclimate.com/github/dustinspecker/prepend-if) [![Dependencies](https://david-dm.org/dustinspecker/prepend-if.svg)](https://david-dm.org/dustinspecker/prepend-if/#info=dependencies&view=table) [![DevDependencies](https://david-dm.org/dustinspecker/prepend-if/dev-status.svg)](https://david-dm.org/dustinspecker/prepend-if/#info=devDependencies&view=table)
> Prepend a string, conditionally
## Install
```
npm install --save prepend-if
```## Usage
### ES2015
```javascript
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
```javascript
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
- [append-if](https://github.com/dustinspecker/append-if)## LICENSE
MIT © [Dustin Specker](https://github.com/dustinspecker)