From 88270a7b6674da869357ac65793812ba5de559c3 Mon Sep 17 00:00:00 2001 From: jdalton Date: Sun, 31 May 2015 13:27:35 -0700 Subject: [PATCH] Add built-in `Date.now` fork back. --- lodash.src.js | 5 +++-- test/index.html | 9 +++++++++ test/test.js | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 57527ef9b..da1978453 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -751,6 +751,7 @@ nativeKeys = getNative(Object, 'keys'), nativeMax = Math.max, nativeMin = Math.min, + nativeNow = getNative(Date, 'now'), nativeParseInt = context.parseInt, nativeRandom = Math.random; @@ -7280,9 +7281,9 @@ * }, _.now()); * // => logs the number of milliseconds it took for the deferred function to be invoked */ - function now() { + var now = nativeNow || function() { return new Date().getTime(); - } + }; /*------------------------------------------------------------------------*/ diff --git a/test/index.html b/test/index.html index b3ce68690..7d52d3249 100644 --- a/test/index.html +++ b/test/index.html @@ -85,6 +85,9 @@ setProperty(Array, '_isArray', Array.isArray); setProperty(Array, 'isArray', noop); + setProperty(Date, '_now', Date.now); + setProperty(Date, 'now', noop); + setProperty(Object, '_keys', Object.keys); setProperty(Object, 'keys', noop); @@ -124,6 +127,11 @@ } else { delete Array.isArray; } + if (Date._now) { + setProperty(Date, 'now', Date._now); + } else { + delete Date.now; + } if (Object._keys) { setProperty(Object, 'keys', Object._keys); } else { @@ -148,6 +156,7 @@ setProperty(objectProto, 'propertyIsEnumerable', objectProto._propertyIsEnumerable); delete Array._isArray; + delete Date._now; delete Object._keys; delete funcProto._method; delete objectProto._propertyIsEnumerable; diff --git a/test/test.js b/test/test.js index 678b8c4b7..1d78c6c28 100644 --- a/test/test.js +++ b/test/test.js @@ -419,6 +419,9 @@ var _isArray = Array.isArray; setProperty(Array, 'isArray', _.noop); + var _now = Date.now; + setProperty(Date, 'now', _.noop); + var _keys = Object.keys; setProperty(Object, 'keys', _.noop); @@ -447,6 +450,7 @@ // Restore built-in methods. setProperty(Array, 'isArray', _isArray); + setProperty(Date, 'now', _now); setProperty(Object, 'keys', _keys); setProperty(objectProto, 'propertyIsEnumerable', _propertyIsEnumerable); @@ -626,7 +630,7 @@ } }); - test('should avoid overwritten native methods', 6, function() { + test('should avoid overwritten native methods', 7, function() { function Foo() {} function message(lodashMethod, nativeMethod) { @@ -645,6 +649,13 @@ } deepEqual(actual, [true, false], message('_.isArray', 'Array.isArray')); + try { + actual = lodashBizarro.now(); + } catch(e) { + actual = null; + } + ok(typeof actual == 'number', message('_.now', 'Date.now')); + try { actual = [lodashBizarro.keys(object), lodashBizarro.keys()]; } catch(e) { @@ -680,7 +691,7 @@ } } else { - skipTest(6); + skipTest(7); } }); }());