mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Add fp.convert.
This commit is contained in:
@@ -115,6 +115,10 @@ function baseConvert(util, name, func, options) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var convertLib = function(options) {
|
||||||
|
return _.runInContext.convert(options)();
|
||||||
|
};
|
||||||
|
|
||||||
var createCloner = function(func) {
|
var createCloner = function(func) {
|
||||||
return function(object) {
|
return function(object) {
|
||||||
return func({}, object);
|
return func({}, object);
|
||||||
@@ -230,7 +234,7 @@ function baseConvert(util, name, func, options) {
|
|||||||
name = mapping.aliasToReal[name] || name;
|
name = mapping.aliasToReal[name] || name;
|
||||||
var wrapper = wrappers[name];
|
var wrapper = wrappers[name];
|
||||||
|
|
||||||
var convert = function(options) {
|
var convertMethod = function(options) {
|
||||||
var newUtil = isLib ? pristine : helpers,
|
var newUtil = isLib ? pristine : helpers,
|
||||||
newFunc = isLib ? pristine[name] : func,
|
newFunc = isLib ? pristine[name] : func,
|
||||||
newOptions = assign(assign({}, config), options);
|
newOptions = assign(assign({}, config), options);
|
||||||
@@ -240,7 +244,7 @@ function baseConvert(util, name, func, options) {
|
|||||||
|
|
||||||
if (wrapper) {
|
if (wrapper) {
|
||||||
var result = wrapper(func);
|
var result = wrapper(func);
|
||||||
result.convert = convert;
|
result.convert = convertMethod;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
var wrapped = func;
|
var wrapped = func;
|
||||||
@@ -293,7 +297,7 @@ function baseConvert(util, name, func, options) {
|
|||||||
return func.apply(this, arguments);
|
return func.apply(this, arguments);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
result.convert = convert;
|
result.convert = convertMethod;
|
||||||
if (mapping.placeholder[name]) {
|
if (mapping.placeholder[name]) {
|
||||||
setPlaceholder = true;
|
setPlaceholder = true;
|
||||||
result.placeholder = func.placeholder = placeholder;
|
result.placeholder = func.placeholder = placeholder;
|
||||||
@@ -322,10 +326,11 @@ function baseConvert(util, name, func, options) {
|
|||||||
_[pair[0]] = pair[1];
|
_[pair[0]] = pair[1];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_.convert = convertLib;
|
||||||
if (setPlaceholder) {
|
if (setPlaceholder) {
|
||||||
_.placeholder = placeholder;
|
_.placeholder = placeholder;
|
||||||
}
|
}
|
||||||
// Wrap the lodash method and its aliases.
|
// Reassign aliases.
|
||||||
each(keys(_), function(key) {
|
each(keys(_), function(key) {
|
||||||
each(mapping.realToAlias[key] || [], function(alias) {
|
each(mapping.realToAlias[key] || [], function(alias) {
|
||||||
_[alias] = _[key];
|
_[alias] = _[key];
|
||||||
|
|||||||
@@ -274,7 +274,7 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('convert method');
|
QUnit.module('convert on methods');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
QUnit.test('should work when given an object', function(assert) {
|
QUnit.test('should work when given an object', function(assert) {
|
||||||
@@ -309,6 +309,41 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('fp.convert');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
QUnit.test('should work when given an object', function(assert) {
|
||||||
|
assert.expect(3);
|
||||||
|
|
||||||
|
var array = [1, 2, 3, 4],
|
||||||
|
lodash = fp.convert(allFalseOptions);
|
||||||
|
|
||||||
|
var actual = lodash.remove(array, function(n, index) {
|
||||||
|
return index % 2 == 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(array, [2, 4]);
|
||||||
|
assert.deepEqual(actual, [1, 3]);
|
||||||
|
assert.deepEqual(lodash.remove(), []);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should extend existing configs', function(assert) {
|
||||||
|
assert.expect(2);
|
||||||
|
|
||||||
|
var array = [1, 2, 3, 4],
|
||||||
|
lodash = fp.convert({ 'cap': false }).convert({ 'rearg': false });
|
||||||
|
|
||||||
|
var actual = lodash.remove(array)(function(n, index) {
|
||||||
|
return index % 2 == 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(array, [1, 2, 3, 4]);
|
||||||
|
assert.deepEqual(actual, [2, 4]);
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('method arity checks');
|
QUnit.module('method arity checks');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user