From 2d202e90b7f2c67772c6ea0e82b81ccfcf40d67d Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 12 Jan 2013 19:02:06 -0800 Subject: [PATCH] Add unofficial `_.where` like support for methods like `_.find`. [closes #159] Former-commit-id: c6106035af3f3d676cbd3f0a5c785b2c00ad1e9d --- build.js | 39 ++++++++++++++++++++------------------- lodash.js | 30 +++++++++++++++++------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/build.js b/build.js index 0db0b07c8..be622a684 100755 --- a/build.js +++ b/build.js @@ -76,23 +76,23 @@ 'compact': [], 'compose': [], 'contains': ['indexOf', 'isString'], - 'countBy': ['forEach'], + 'countBy': ['forEach', 'keys'], 'debounce': [], 'defaults': ['isArguments'], 'defer': [], 'delay': [], 'difference': ['indexOf'], 'escape': [], - 'every': ['isArray'], - 'filter': ['isArray'], - 'find': ['forEach'], + 'every': ['isArray', 'keys'], + 'filter': ['isArray', 'keys'], + 'find': ['forEach', 'keys'], 'first': [], 'flatten': ['isArray'], 'forEach': ['identity', 'isArguments', 'isArray', 'isString'], 'forIn': ['identity', 'isArguments'], 'forOwn': ['identity', 'isArguments'], 'functions': ['forIn', 'isFunction'], - 'groupBy': ['forEach'], + 'groupBy': ['forEach', 'keys'], 'has': [], 'identity': [], 'indexOf': ['sortedIndex'], @@ -120,11 +120,11 @@ 'keys': ['forOwn', 'isArguments', 'isObject'], 'last': [], 'lastIndexOf': [], - 'map': ['isArray'], - 'max': ['isArray', 'isString'], + 'map': ['isArray', 'keys'], + 'max': ['isArray', 'isString', 'keys'], 'memoize': [], 'merge': ['forOwn', 'isArray', 'isPlainObject'], - 'min': ['isArray', 'isString'], + 'min': ['isArray', 'isString', 'keys'], 'mixin': ['forEach', 'forOwn', 'functions'], 'noConflict': [], 'object': [], @@ -136,16 +136,16 @@ 'pluck': ['map'], 'random': [], 'range': [], - 'reduce': ['isArray'], + 'reduce': ['isArray', 'keys'], 'reduceRight': ['forEach', 'isString', 'keys'], 'reject': ['filter'], 'rest': [], 'result': ['isFunction'], 'shuffle': ['forEach'], 'size': ['keys'], - 'some': ['isArray'], - 'sortBy': ['forEach'], - 'sortedIndex': ['identity'], + 'some': ['isArray', 'keys'], + 'sortBy': ['forEach', 'keys'], + 'sortedIndex': ['identity', 'keys'], 'tap': ['mixin'], 'template': ['defaults', 'escape', 'keys', 'values'], 'throttle': [], @@ -153,11 +153,11 @@ 'toArray': ['isString', 'values'], 'unescape': [], 'union': ['uniq'], - 'uniq': ['identity', 'indexOf'], + 'uniq': ['identity', 'indexOf', 'keys'], 'uniqueId': [], 'value': ['mixin'], 'values': ['keys'], - 'where': ['filter', 'keys'], + 'where': ['filter'], 'without': ['indexOf'], 'wrap': [], 'zip': ['max', 'pluck'], @@ -1336,6 +1336,12 @@ exposeForOwn = !isUnderscore, exposeIsPlainObject = !isUnderscore; + // flags used to specify export options + var isAMD = exportsOptions.indexOf('amd') > -1, + isCommonJS = exportsOptions.indexOf('commonjs') > -1, + isGlobal = exportsOptions.indexOf('global') > -1, + isNode = exportsOptions.indexOf('node') > -1; + /*------------------------------------------------------------------------*/ // names of methods to include in the build @@ -2004,11 +2010,6 @@ // customize Lo-Dash's export bootstrap (function() { - var isAMD = exportsOptions.indexOf('amd') > -1, - isCommonJS = exportsOptions.indexOf('commonjs') > -1, - isGlobal = exportsOptions.indexOf('global') > -1, - isNode = exportsOptions.indexOf('node') > -1; - if (!isAMD) { source = source.replace(/(?: *\/\/.*\n)*( *)if *\(typeof +define[\s\S]+?else /, '$1'); } diff --git a/lodash.js b/lodash.js index 13e11ce1e..5c8780ba7 100644 --- a/lodash.js +++ b/lodash.js @@ -633,9 +633,23 @@ if (!func) { return identity; } - if (typeof func != 'function') { + var type = typeof func; + if (type != 'function') { + if (type != 'object') { + return function(object) { + return object[func]; + }; + } + var props = keys(func); return function(object) { - return object[func]; + var length = props.length; + while (length--) { + var result = object[props[length]] === func[props[length]]; + if (!result) { + break; + } + } + return !!result; }; } if (typeof thisArg != 'undefined') { @@ -2748,17 +2762,7 @@ * // => [{ 'name': 'moe', 'age': 40 }] */ function where(collection, properties) { - var props = keys(properties); - return filter(collection, function(object) { - var length = props.length; - while (length--) { - var result = object[props[length]] === properties[props[length]]; - if (!result) { - break; - } - } - return !!result; - }); + return filter(collection, properties); } /*--------------------------------------------------------------------------*/