mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Simplify fp map.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
var mapping = require('./mapping.js'),
|
var mapping = require('./mapping.js'),
|
||||||
mutateMap = mapping.mutateMap;
|
mutateMap = mapping.mutate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `convert` which accepts a `util` object of methods
|
* The base implementation of `convert` which accepts a `util` object of methods
|
||||||
@@ -158,13 +158,13 @@ function baseConvert(util, name, func) {
|
|||||||
}
|
}
|
||||||
var result;
|
var result;
|
||||||
each(mapping.caps, function(cap) {
|
each(mapping.caps, function(cap) {
|
||||||
each(mapping.aryMethodMap[cap], function(otherName) {
|
each(mapping.aryMethod[cap], function(otherName) {
|
||||||
if (name == otherName) {
|
if (name == otherName) {
|
||||||
result = ary(func, cap);
|
result = ary(func, cap);
|
||||||
if (cap > 1 && !mapping.skipReargMap[name]) {
|
if (cap > 1 && !mapping.skipRearg[name]) {
|
||||||
result = rearg(result, mapping.methodReargMap[name] || mapping.aryReargMap[cap]);
|
result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[cap]);
|
||||||
}
|
}
|
||||||
var n = !isLib && mapping.aryIterateeMap[name];
|
var n = !isLib && mapping.aryIteratee[name];
|
||||||
if (n) {
|
if (n) {
|
||||||
result = iterateeAry(result, n);
|
result = iterateeAry(result, n);
|
||||||
}
|
}
|
||||||
@@ -185,8 +185,8 @@ function baseConvert(util, name, func) {
|
|||||||
// Iterate over methods for the current ary cap.
|
// Iterate over methods for the current ary cap.
|
||||||
var pairs = [];
|
var pairs = [];
|
||||||
each(mapping.caps, function(cap) {
|
each(mapping.caps, function(cap) {
|
||||||
each(mapping.aryMethodMap[cap], function(key) {
|
each(mapping.aryMethod[cap], function(key) {
|
||||||
var func = _[mapping.keyMap[key] || key];
|
var func = _[mapping.key[key] || key];
|
||||||
if (func) {
|
if (func) {
|
||||||
pairs.push([key, wrap(key, func)]);
|
pairs.push([key, wrap(key, func)]);
|
||||||
}
|
}
|
||||||
@@ -200,7 +200,7 @@ function baseConvert(util, name, func) {
|
|||||||
|
|
||||||
// Wrap the lodash method and its aliases.
|
// Wrap the lodash method and its aliases.
|
||||||
each(keys(_), function(key) {
|
each(keys(_), function(key) {
|
||||||
each(mapping.aliasMap[key] || [], function(alias) {
|
each(mapping.alias[key] || [], function(alias) {
|
||||||
_[alias] = _[key];
|
_[alias] = _[key];
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
/** Used to map method names to their aliases. */
|
/** Used to map method names to their aliases. */
|
||||||
'aliasMap': {
|
'alias': {
|
||||||
'ary': ['nAry'],
|
'ary': ['nAry'],
|
||||||
'overEvery': ['allPass'],
|
'overEvery': ['allPass'],
|
||||||
'overSome': ['somePass'],
|
'overSome': ['somePass'],
|
||||||
@@ -31,7 +31,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/** Used to map method names to their iteratee ary. */
|
/** Used to map method names to their iteratee ary. */
|
||||||
'aryIterateeMap': {
|
'aryIteratee': {
|
||||||
'assignWith': 2,
|
'assignWith': 2,
|
||||||
'cloneDeepWith': 1,
|
'cloneDeepWith': 1,
|
||||||
'cloneWith': 1,
|
'cloneWith': 1,
|
||||||
@@ -71,7 +71,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/** Used to map ary to method names. */
|
/** Used to map ary to method names. */
|
||||||
'aryMethodMap': {
|
'aryMethod': {
|
||||||
1: (
|
1: (
|
||||||
'attempt,ceil,create,curry,curryRight,floor,fromPairs,iteratee,invert,over,' +
|
'attempt,ceil,create,curry,curryRight,floor,fromPairs,iteratee,invert,over,' +
|
||||||
'overEvery,overSome,memoize,method,methodOf,mixin,rest,reverse,round,' +
|
'overEvery,overSome,memoize,method,methodOf,mixin,rest,reverse,round,' +
|
||||||
@@ -98,14 +98,14 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/** Used to map ary to rearg configs by method ary. */
|
/** Used to map ary to rearg configs by method ary. */
|
||||||
'aryReargMap': {
|
'aryRearg': {
|
||||||
2: [1, 0],
|
2: [1, 0],
|
||||||
3: [2, 1, 0],
|
3: [2, 1, 0],
|
||||||
4: [3, 2, 0, 1]
|
4: [3, 2, 0, 1]
|
||||||
},
|
},
|
||||||
|
|
||||||
/** Used to map ary to rearg configs by method names. */
|
/** Used to map ary to rearg configs by method names. */
|
||||||
'methodReargMap': {
|
'methodRearg': {
|
||||||
'clamp': [2, 0, 1],
|
'clamp': [2, 0, 1],
|
||||||
'reduce': [2, 0, 1],
|
'reduce': [2, 0, 1],
|
||||||
'reduceRight': [2, 0, 1],
|
'reduceRight': [2, 0, 1],
|
||||||
@@ -119,14 +119,14 @@ module.exports = {
|
|||||||
'caps': [1, 2, 3, 4],
|
'caps': [1, 2, 3, 4],
|
||||||
|
|
||||||
/** Used to map keys to other keys. */
|
/** Used to map keys to other keys. */
|
||||||
'keyMap': {
|
'key': {
|
||||||
'curryN': 'curry',
|
'curryN': 'curry',
|
||||||
'curryRightN': 'curryRight',
|
'curryRightN': 'curryRight',
|
||||||
'getOr': 'get'
|
'getOr': 'get'
|
||||||
},
|
},
|
||||||
|
|
||||||
/** Used to identify methods which mutate arrays or objects. */
|
/** Used to identify methods which mutate arrays or objects. */
|
||||||
'mutateMap': {
|
'mutate': {
|
||||||
'array': {
|
'array': {
|
||||||
'fill': true,
|
'fill': true,
|
||||||
'pull': true,
|
'pull': true,
|
||||||
@@ -153,7 +153,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/** Used to track methods that skip `_.rearg`. */
|
/** Used to track methods that skip `_.rearg`. */
|
||||||
'skipReargMap': {
|
'skipRearg': {
|
||||||
'assign': true,
|
'assign': true,
|
||||||
'assignIn': true,
|
'assignIn': true,
|
||||||
'defaults': true,
|
'defaults': true,
|
||||||
|
|||||||
@@ -71,7 +71,7 @@
|
|||||||
QUnit.test('should have correct aliases', function(assert) {
|
QUnit.test('should have correct aliases', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var actual = _.transform(mapping.aliasMap, function(result, aliases, methodName) {
|
var actual = _.transform(mapping.alias, function(result, aliases, methodName) {
|
||||||
var func = fp[methodName];
|
var func = fp[methodName];
|
||||||
_.each(aliases, function(alias) {
|
_.each(aliases, function(alias) {
|
||||||
result.push([alias, fp[alias] === func]);
|
result.push([alias, fp[alias] === func]);
|
||||||
@@ -92,13 +92,13 @@
|
|||||||
|
|
||||||
var funcMethods = [
|
var funcMethods = [
|
||||||
'curry', 'iteratee', 'memoize', 'over', 'overEvery', 'overSome',
|
'curry', 'iteratee', 'memoize', 'over', 'overEvery', 'overSome',
|
||||||
'method', 'methodOf', 'restParam', 'runInContext'
|
'method', 'methodOf', 'rest', 'runInContext'
|
||||||
];
|
];
|
||||||
|
|
||||||
var exceptions = funcMethods.concat('mixin', 'template'),
|
var exceptions = funcMethods.concat('mixin', 'template'),
|
||||||
expected = _.map(mapping.aryMethodMap[1], _.constant(true));
|
expected = _.map(mapping.aryMethod[1], _.constant(true));
|
||||||
|
|
||||||
var actual = _.map(mapping.aryMethodMap[1], function(methodName) {
|
var actual = _.map(mapping.aryMethod[1], function(methodName) {
|
||||||
var arg = _.includes(funcMethods, methodName) ? _.noop : 1,
|
var arg = _.includes(funcMethods, methodName) ? _.noop : 1,
|
||||||
result = _.attempt(function() { return fp[methodName](arg); });
|
result = _.attempt(function() { return fp[methodName](arg); });
|
||||||
|
|
||||||
@@ -124,9 +124,9 @@
|
|||||||
];
|
];
|
||||||
|
|
||||||
var exceptions = _.difference(funcMethods.concat('matchesProperty'), ['cloneDeepWith', 'cloneWith', 'delay']),
|
var exceptions = _.difference(funcMethods.concat('matchesProperty'), ['cloneDeepWith', 'cloneWith', 'delay']),
|
||||||
expected = _.map(mapping.aryMethodMap[2], _.constant(true));
|
expected = _.map(mapping.aryMethod[2], _.constant(true));
|
||||||
|
|
||||||
var actual = _.map(mapping.aryMethodMap[2], function(methodName) {
|
var actual = _.map(mapping.aryMethod[2], function(methodName) {
|
||||||
var args = _.includes(funcMethods, methodName) ? [methodName == 'curryN' ? 1 : _.noop, _.noop] : [1, []],
|
var args = _.includes(funcMethods, methodName) ? [methodName == 'curryN' ? 1 : _.noop, _.noop] : [1, []],
|
||||||
result = _.attempt(function() { return fp[methodName](args[0])(args[1]); });
|
result = _.attempt(function() { return fp[methodName](args[0])(args[1]); });
|
||||||
|
|
||||||
@@ -151,9 +151,9 @@
|
|||||||
'pickBy', 'reduce', 'reduceRight', 'transform', 'zipWith'
|
'pickBy', 'reduce', 'reduceRight', 'transform', 'zipWith'
|
||||||
];
|
];
|
||||||
|
|
||||||
var expected = _.map(mapping.aryMethodMap[3], _.constant(true));
|
var expected = _.map(mapping.aryMethod[3], _.constant(true));
|
||||||
|
|
||||||
var actual = _.map(mapping.aryMethodMap[3], function(methodName) {
|
var actual = _.map(mapping.aryMethod[3], function(methodName) {
|
||||||
var args = _.includes(funcMethods, methodName) ? [_.noop, 0, 1] : [0, 1, []],
|
var args = _.includes(funcMethods, methodName) ? [_.noop, 0, 1] : [0, 1, []],
|
||||||
result = _.attempt(function() { return fp[methodName](args[0])(args[1])(args[2]); });
|
result = _.attempt(function() { return fp[methodName](args[0])(args[1])(args[2]); });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user