Skip to content

Commit

Permalink
add _.endsWith and add tests for both endsWith and startsWith (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
tombiju authored and cht8687 committed Oct 23, 2019
1 parent 89e0d7b commit 056549f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,12 @@ then Lodash/Underscore is the better option.*

**[String](#string)**

1. [_.endsWith](#_endsWith)
1. [_.padStart and _.padEnd](#_padstart-and-_padend)
1. [_.repeat](#_repeat)
1. [_.replace](#_replace)
1. [_.split](#_split)
1. [_.startsWith](#startsWith)
1. [_.startsWith](#_startsWith)
1. [_.template](#_template)
1. [_.toLower](#_tolower)
1. [_.toUpper](#_toupper)
Expand Down Expand Up @@ -2250,6 +2251,40 @@ Retrieves all the given object's own enumerable property values.
## String
### _.endsWith
:heavy_exclamation_mark:`Not in Underscore.js`
Checks if string ends with the given target string.
```js
// Lodash
_.endsWith('abc', 'c');
// => true

_.endsWith('abc', 'b');
// => false

_.endsWith('abc', 'b', 2);
// => true

// Native
'abc'.endsWith('c');
// => true

'abc'endsWith('b');
// => false

'abc'.endsWith('b', 2);
// => true
```
#### Browser Support for `String.prototype.endsWith()`
![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image]
:-: | :-: | :-: | :-: | :-: | :-: |
41.0 ✔ | ✔ | 17.0 ✔ | ✖ | 28.0 ✔ | 9.0 ✔ |
**[⬆ back to top](#quick-links)**
### _.padStart and _.padEnd
:heavy_exclamation_mark:`Not in Underscore.js`
Pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.
Expand Down
6 changes: 6 additions & 0 deletions lib/rules/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,15 @@
"alternative": "String.prototype.split()"
},
"startsWith": {
"ruleName": "starts-with",
"compatible": true,
"alternative": "String.prototype.startsWith()"
},
"endsWith": {
"ruleName": "ends-with",
"compatible": true,
"alternative": "String.prototype.endsWith()"
},
"toLower": {
"compatible": true,
"alternative": "String.prototype.toLowerCase()"
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,47 @@ ruleTester.run('_.flatten', rules['flatten'], {
}]
});

ruleTester.run('_.isUndefined', rules['is-undefined'], {
valid: [
'2 === undefined'
],
invalid: [{
code: '_.isUndefined(2)',
errors: ['Consider using the native value === undefined']
},{
code: '_(2).isUndefined()',
errors: ['Consider using the native value === undefined']

}]
});

ruleTester.run('_.startsWith', rules['starts-with'], {
valid: [
'"abc".startsWith("a")',
'"abc".startsWith("b", 1)'
],
invalid: [{
code: '_.startsWith("abc", "a")',
errors: ['Consider using the native String.prototype.startsWith()']
},{
code: '_.startsWith("abc", "b", 1)',
errors: ['Consider using the native String.prototype.startsWith()']

}]
});

ruleTester.run('_.endsWith', rules['ends-with'], {
valid: [
'"abc".endsWith("c")',
'"abc".endsWith("b", 1)'
],
invalid: [{
code: '_.endsWith("abc", "c")',
errors: ['Consider using the native String.prototype.endsWith()']
},{
code: '_.endsWith("abc", "b", 1)',
errors: ['Consider using the native String.prototype.endsWith()']

}]
});

43 changes: 41 additions & 2 deletions tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,6 @@ describe('code snippet example', () => {
});

assert.deepEqual(lodashOutput,nativeOutput);

});

it('_.forEach(object)', () => {
Expand All @@ -686,8 +685,48 @@ describe('code snippet example', () => {
});

assert.deepEqual(lodashOutput,nativeOutput);
});
});

describe('startsWith', () => {
it(`_.startsWith('abc', 'a')`, () => {
assert.deepEqual(
_.startsWith('abc', 'a'),
'abc'.startsWith('a')
);
});
it(`_.startsWith('abc', 'b')`, () => {
assert.deepEqual(
_.startsWith('abc', 'b'),
'abc'.startsWith('b')
);
});
it(`_.startsWith('abc', 'b', 1)`, () => {
assert.deepEqual(
_.startsWith('abc', 'b', 1),
'abc'.startsWith('b', 1)
);
});
});

})
describe('endsWith', () => {
it(`_.endsWith('abc', 'c')`, () => {
assert.deepEqual(
_.endsWith('abc', 'c'),
'abc'.endsWith('c')
);
});
it(`_.endsWith('abc', 'b')`, () => {
assert.deepEqual(
_.endsWith('abc', 'b'),
'abc'.endsWith('b')
);
});
it(`_.endsWith('abc', 'b', 2)`, () => {
assert.deepEqual(
_.endsWith('abc', 'b', 2),
'abc'.endsWith('b', 2)
);
});
});
});

0 comments on commit 056549f

Please sign in to comment.