From 819d4abaa25a4cb2b4a8cc3e831d73ee30bfe9ef Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 18 Nov 2012 21:50:41 -0800 Subject: [PATCH] Add `_#toString` and `_#valueOf`. Former-commit-id: adb194b6270fc72f794c69343891a2e891b90051 --- build.js | 3 +++ lodash.js | 18 ++++++++++++++++++ test/test-build.js | 6 +++++- test/test.js | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/build.js b/build.js index b3117fb8a..f66c4db0d 100755 --- a/build.js +++ b/build.js @@ -1413,6 +1413,9 @@ // remove conditional `charCodeCallback` use from `_.max` and `_.min` source = source.replace(/!callback *&& *isString\(collection\)[\s\S]+?: */g, ''); + // remove `lodash.prototype.toString` and `lodash.prototype.valueOf` assignments + source = source.replace(/ *lodash\.prototype\.(?:toString|valueOf) *=.+\n/g, ''); + // remove unused features from `createBound` if (buildMethods.indexOf('partial') == -1) { source = source.replace(matchFunction(source, 'createBound'), function(match) { diff --git a/lodash.js b/lodash.js index 6f573caf7..16306da7a 100644 --- a/lodash.js +++ b/lodash.js @@ -4043,6 +4043,22 @@ return this; } + /** + * Produces the `toString` result of the wrapped value. + * + * @name toString + * @memberOf _ + * @category Chaining + * @returns {String} Returns the string result. + * @example + * + * _([1, 2, 3]).toString(); + * // => '1,2,3' + */ + function wrapperToString() { + return String(this.__wrapped__); + } + /** * Extracts the wrapped value. * @@ -4196,7 +4212,9 @@ // add `lodash.prototype.chain` after calling `mixin()` to avoid overwriting // it with the wrapped `lodash.chain` lodash.prototype.chain = wrapperChain; + lodash.prototype.toString = wrapperToString; lodash.prototype.value = wrapperValue; + lodash.prototype.valueOf = wrapperValue; // add all mutator Array functions to the wrapper. forEach(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) { diff --git a/test/test-build.js b/test/test-build.js index 4a85bf62a..6439d092a 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -694,7 +694,11 @@ equal(lodash.some([false, true, false]), true, '_.some: ' + basename); equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename); - equal(lodash(1).clone() instanceof lodash, false, '_(...) wrapped values are not chainable by default: ' + basename); + + object = lodash(1); + equal(object.clone() instanceof lodash, false, '_(...) wrapped values are not chainable by default: ' + basename); + equal(String(object) === '1', false, '_.prototype should not implement its own `toString` method: ' + basename); + equal(Number(object) === 1 , false, '_.prototype should not implement its own `valueOf` method: ' + basename); start(); }); diff --git a/test/test.js b/test/test.js index a1f90440c..3986e8d6d 100644 --- a/test/test.js +++ b/test/test.js @@ -1922,6 +1922,28 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash(...).toString'); + + (function() { + test('should return the `toString` result of the wrapped value', function() { + var wrapped = _([1, 2, 3]); + equal(String(wrapped), '1,2,3'); + }); + }()); + + /*--------------------------------------------------------------------------*/ + + QUnit.module('lodash(...).valueOf'); + + (function() { + test('should return the `valueOf` result of the wrapped value', function() { + var wrapped = _(123); + equal(Number(wrapped), 123); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash methods'); (function() {