Skip to content

Commit 2de676f

Browse files
committed
Ensure placeholder properties are set for fp.convert() results. [closes #3440]
1 parent 278c6dd commit 2de676f

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

fp/_baseConvert.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ function wrapImmutable(func, cloner) {
136136
* @returns {Function|Object} Returns the converted function or object.
137137
*/
138138
function baseConvert(util, name, func, options) {
139-
var setPlaceholder,
140-
isLib = typeof name == 'function',
139+
var isLib = typeof name == 'function',
141140
isObj = name === Object(name);
142141

143142
if (isObj) {
@@ -158,10 +157,10 @@ function baseConvert(util, name, func, options) {
158157
'rearg': 'rearg' in options ? options.rearg : true
159158
};
160159

161-
var forceCurry = ('curry' in options) && options.curry,
160+
var defaultHolder = isLib ? func : fallbackHolder,
161+
forceCurry = ('curry' in options) && options.curry,
162162
forceFixed = ('fixed' in options) && options.fixed,
163163
forceRearg = ('rearg' in options) && options.rearg,
164-
placeholder = isLib ? func : fallbackHolder,
165164
pristine = isLib ? func.runInContext() : undefined;
166165

167166
var helpers = isLib ? func : {
@@ -466,7 +465,7 @@ function baseConvert(util, name, func, options) {
466465
* @param {Function} func The function to wrap.
467466
* @returns {Function} Returns the converted function.
468467
*/
469-
function wrap(name, func) {
468+
function wrap(name, func, placeholder) {
470469
var result,
471470
realName = mapping.aliasToReal[name] || name,
472471
wrapped = func,
@@ -511,17 +510,15 @@ function baseConvert(util, name, func, options) {
511510
};
512511
}
513512
result.convert = createConverter(realName, func);
514-
if (mapping.placeholder[realName]) {
515-
setPlaceholder = true;
516-
result.placeholder = func.placeholder = placeholder;
517-
}
513+
result.placeholder = func.placeholder = placeholder;
514+
518515
return result;
519516
}
520517

521518
/*--------------------------------------------------------------------------*/
522519

523520
if (!isObj) {
524-
return wrap(name, func);
521+
return wrap(name, func, defaultHolder);
525522
}
526523
var _ = func;
527524

@@ -531,7 +528,8 @@ function baseConvert(util, name, func, options) {
531528
each(mapping.aryMethod[aryKey], function(key) {
532529
var func = _[mapping.remap[key] || key];
533530
if (func) {
534-
pairs.push([key, wrap(key, func)]);
531+
532+
pairs.push([key, wrap(key, func, _)]);
535533
}
536534
});
537535
});
@@ -557,9 +555,8 @@ function baseConvert(util, name, func, options) {
557555
});
558556

559557
_.convert = convertLib;
560-
if (setPlaceholder) {
561-
_.placeholder = placeholder;
562-
}
558+
_.placeholder = _;
559+
563560
// Assign aliases.
564561
each(keys(_), function(key) {
565562
each(mapping.realToAlias[key] || [], function(alias) {

fp/_mapping.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,6 @@ exports.mutate = {
261261
}
262262
};
263263

264-
/** Used to track methods with placeholder support */
265-
exports.placeholder = {
266-
'bind': true,
267-
'bindKey': true,
268-
'curry': true,
269-
'curryRight': true,
270-
'partial': true,
271-
'partialRight': true
272-
};
273-
274264
/** Used to map real names to their aliases. */
275265
exports.realToAlias = (function() {
276266
var hasOwnProperty = Object.prototype.hasOwnProperty,

test/test-fp.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,14 @@
233233
assert.strictEqual(add('2')('1'), '12');
234234
});
235235

236-
QUnit.test('should only add a `placeholder` property if needed', function(assert) {
236+
QUnit.test('should add a `placeholder` property', function(assert) {
237237
assert.expect(2);
238238

239239
if (!document) {
240-
var methodNames = _.keys(mapping.placeholder),
241-
expected = _.map(methodNames, _.constant(true));
242-
243-
var actual = _.map(methodNames, function(methodName) {
244-
var object = {};
245-
object[methodName] = _[methodName];
246-
247-
var lodash = convert(object);
248-
return methodName in lodash;
249-
});
250-
251-
assert.deepEqual(actual, expected);
252-
253240
var lodash = convert({ 'add': _.add });
254-
assert.notOk('placeholder' in lodash);
241+
242+
assert.strictEqual(lodash.placeholder, lodash);
243+
assert.strictEqual(lodash.add.placeholder, lodash)
255244
}
256245
else {
257246
skipAssert(assert, 2);
@@ -645,7 +634,16 @@
645634
});
646635
});
647636

648-
_.forOwn(mapping.placeholder, function(truthy, methodName) {
637+
var methodNames = [
638+
'bind',
639+
'bindKey',
640+
'curry',
641+
'curryRight',
642+
'partial',
643+
'partialRight'
644+
]
645+
646+
_.each(methodNames, function(methodName) {
649647
var func = fp[methodName];
650648

651649
QUnit.test('fp.' + methodName + '` should have a `placeholder` property', function(assert) {

0 commit comments

Comments
 (0)