8000 Make `_.contains` work with strings similar ES6 draft `String#contains`. · lodash/lodash@cbdc9c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbdc9c0

Browse files
committed
Make _.contains work with strings similar ES6 draft String#contains.
Former-commit-id: 3cfffdcddec3e1e8175da95043ec86ac3c6a85fe
1 parent b9bade8 commit cbdc9c0

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

build/post-compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/*--------------------------------------------------------------------------*/
1616

1717
/**
18-
* Post-process a given minified JavaScript `source`, preparing it for
18+
* Post-process a given minified Lo-Dash `source`, preparing it for
1919
* deployment.
2020
*
2121
* @param {String} source The source to process.

build/pre-compile.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
/**
199199
* Pre-process a given Lo-Dash source, preparing it for minification.
200200
*
201-
* @param {String} source The Lo-Dash source to process.
201+
* @param {String} source The source to process.
202202
* @returns {String} Returns the processed source.
203203
*/
204204
function preprocess(source) {
@@ -218,6 +218,9 @@
218218
// remove brackets from `_.escape()` in `_.template`
219219
source = source.replace(/__e *= *_\['escape']/, '__e=_.escape');
220220

221+
// remove brackets from `collection.indexOf` in `_.contains`
222+
source = source.replace("collection['indexOf'](target)", 'collection.indexOf(target)');
223+
221224
// remove brackets from `result[length].value` in `_.sortBy`
222225
source = source.replace("result[length]['value']", 'result[length].value');
223226

lodash.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,12 @@
514514
data.firstArg = firstArg;
515515
data.hasDontEnumBug = hasDontEnumBug;
516516
data.isKeysFast = isKeysFast;
517-
data.noCharByIndex = noCharByIndex;
518517
data.shadowed = shadowed;
519518
data.useHas = data.useHas !== false;
520519

520+
if (!('noCharByIndex' in data)) {
521+
data.noCharByIndex = noCharByIndex;
522+
}
521523
if (!data.exit) {
522524
data.exit = 'if (!' + firstArg + ') return result';
523525
}
@@ -711,11 +713,21 @@
711713
*
712714
* _.contains([1, 2, 3], 3);
713715
* // => true
716+
*
717+
* _.contains({ 'name': 'moe', 'age': 40 }, 'moe');
718+
* // => true
719+
*
720+
* _.contains('curly', 'ur');
721+
* // => true
714722
*/
715723
var contains = createIterator({
716724
'args': 'collection, target',
717725
'init': 'false',
718-
'inLoop': 'if (iteratee[index] === target) return true'
726+
'noCharByIndex': false,
727+
'beforeLoop': {
728+
'array': 'if (toString.call(iteratee) == stringClass) return collection.indexOf(target) > -1'
729+
},
730+
'inLoop': 'if (iteratee[index] === target) return true',
719731
});
720732

721733
/**
@@ -2636,6 +2648,9 @@
26362648
*
26372649
* _.isEmpty({});
26382650
* // => true
2651+
*
2652+
* _.isEmpty('');
2653+
* // => true
26392654
*/
26402655
var isEmpty = createIterator({
26412656
'args': 'value',

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@
132132

133133
/*--------------------------------------------------------------------------*/
134134

135+
QUnit.module('lodash.contains');
136+
137+
(function() {
138+
_.each([
139+
{ 'kind': 'literal', 'value': 'abc' },
140+
{ 'kind': 'object', 'value': Object('abc') }
141+
],
142+
function(data) {
143+
test('should work with a string ' + data.kind + ' for `collection`', function() {
144+
equal(_.contains(data.value, 'bc'), true);
145+
equal(_.contains(data.value, 'd'), false);
146+
});
147+
});
148+
}());
149+
150+
/*--------------------------------------------------------------------------*/
151+
135152
QUnit.module('lodash.debounce');
136153

137154
(function() {

0 commit comments

Comments
 (0)
0