Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 77d8a86

Browse files
danielfsousaKent C. Dodds
authored andcommitted
feat(endsWith): add endsWith function (#90) (#91)
add a function that checks if a string ends with a given input Closes #90
1 parent 8ab3382 commit 77d8a86

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/endsWith.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default endsWith
2+
3+
/**
4+
* Original source: http://stackoverflow.com/questions/280634/endswith-in-javascript
5+
*
6+
* Checks if a string ends with a given input
7+
*
8+
* @param {String} str - The string to validate against
9+
* @param {String} suffix - The input to match
10+
* @return {Boolean} - True if 'str' ends with 'suffix', otherwise false
11+
*/
12+
function endsWith(str, suffix) {
13+
return str.indexOf(suffix, str.length - suffix.length) !== -1
14+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import toPower from './to-power'
2929
import mod from './mod'
3030
import shallowEqual from './shallow-equal'
3131
import swapCase from './swap-case'
32+
import endsWith from './endsWith'
3233

3334
export {
3435
initArray,
@@ -62,4 +63,5 @@ export {
6263
mod,
6364
shallowEqual,
6465
swapCase,
66+
endsWith,
6567
}

test/endsWith.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava'
2+
import {endsWith} from '../src'
3+
4+
test('ends with input', t => {
5+
const string = 'Brasilia'
6+
const input = 'lia'
7+
const result = endsWith(string, input)
8+
t.true(result)
9+
})
10+
11+
test('does not end with input', t => {
12+
const string = 'Brazil'
13+
const input = 'USA'
14+
const result = endsWith(string, input)
15+
t.false(result)
16+
})

0 commit comments

Comments
 (0)