Skip to content

Commit 2722857

Browse files
committed
Removing callbacks on Object.keys and Object.values
Former-commit-id: a16d4cad89bb7fa4a095e6ae0ebe90c3d408db19 [formerly 078d21323f34d0b6878f53faa154745dc5e69666] Former-commit-id: 9fcef62223446579a2241ff2c8770c418bcf7671
1 parent af4ede1 commit 2722857

File tree

2 files changed

+22
-101
lines changed

2 files changed

+22
-101
lines changed

lib/object.js

+22-79
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
*
1010
***/
1111

12-
// Flag allowing Object.keys to be enhanced
13-
var OBJECT_ENHANCEMENTS_FLAG = 'enhanceObject';
14-
1512
// Matches bracket-style query strings like user[name]
1613
var DEEP_QUERY_STRING_REG = /^(.+?)(\[.*\])$/;
1714

@@ -25,9 +22,6 @@ var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
2522
// Internal reference to check if an object can be serialized.
2623
var internalToString = Object.prototype.toString;
2724

28-
// Reference to native Object.keys.
29-
var nativeObjectKeys = Object.keys;
30-
3125
// Query Strings | Creating
3226

3327
function toQueryStringWithOptions(obj, opts) {
@@ -353,32 +347,17 @@ function clone(source, deep) {
353347
// Keys/Values
354348

355349
function objectSize(obj) {
356-
return keysWithObjectCoercion(obj).length;
350+
return getKeysWithObjectCoercion(obj).length;
357351
}
358352

359-
function keysWithObjectCoercion(obj) {
353+
function getKeysWithObjectCoercion(obj) {
360354
return getKeys(coercePrimitiveToObject(obj));
361355
}
362356

363-
function getKeysWithCallback(obj, fn) {
364-
if (isFunction(fn)) {
365-
var keys = [];
366-
forEachProperty(obj, function(key) {
367-
fn.call(obj, key, obj);
368-
keys.push(key);
369-
});
370-
return keys;
371-
}
372-
return nativeObjectKeys(obj);
373-
}
374-
375-
function getValuesWithCallback(obj, fn) {
357+
function getValues(obj, fn) {
376358
var values = [];
377359
forEachProperty(obj, function(key, val) {
378360
values.push(val);
379-
if (isFunction(fn)) {
380-
fn.call(obj, val, obj);
381-
}
382361
});
383362
return values;
384363
}
@@ -496,34 +475,6 @@ function buildClassCheckMethods() {
496475
});
497476
}
498477

499-
defineInstanceAndStatic(sugarObject, {
500-
501-
/***
502-
* @method keys(<obj>, [fn])
503-
* @returns Array
504-
* @polyfill ES5
505-
* @short Returns an array containing the keys in <obj>.
506-
* @extra Sugar enhances this method to allow an optional function that is
507-
* called for each key. Note that the order of returned keys is not
508-
* guaranteed.
509-
*
510-
* @callback fn
511-
*
512-
* key The key of the current iteration.
513-
* obj A reference to the object.
514-
*
515-
* @example
516-
*
517-
* Object.keys({ broken: 'wear' }) -> ['broken']
518-
* Object.keys({ broken: 'wear' }, function(key) {
519-
* // Called once for each key.
520-
* });
521-
*
522-
***/
523-
'keys': fixArgumentLength(getKeysWithCallback)
524-
525-
}, [ENHANCEMENTS_FLAG, OBJECT_ENHANCEMENTS_FLAG]);
526-
527478
defineStatic(sugarObject, {
528479

529480
/***
@@ -573,10 +524,10 @@ defineStatic(sugarObject, {
573524
defineInstanceAndStatic(sugarObject, {
574525

575526
/***
576-
* @method has(<obj>, <key>, [allowPrototype] = false)
527+
* @method has(<obj>, <key>, [inherited] = false)
577528
* @returns Boolean
578529
* @short Checks if <obj> has property <key>.
579-
* @extra Supports `deep properties`. If [allowPrototype] is `true`,
530+
* @extra Supports `deep properties`. If [inherited] is `true`,
580531
* properties defined in the prototype chain will also return `true`.
581532
* The default of `false` for this argument makes this method suited
582533
* to working with objects as data stores by default.
@@ -594,10 +545,10 @@ defineInstanceAndStatic(sugarObject, {
594545
},
595546

596547
/***
597-
* @method get(<obj>, <key>, [allowPrototype] = false)
548+
* @method get(<obj>, <key>, [inherited] = false)
598549
* @returns Mixed
599550
* @short Gets a property of <obj>.
600-
* @extra Supports `deep properties`. If [allowPrototype] is `true`,
551+
* @extra Supports `deep properties`. If [inherited] is `true`,
601552
* properties defined in the prototype chain will also be returned.
602553
* The default of `false` for this argument makes this method suited
603554
* to working with objects as data stores by default.
@@ -802,7 +753,7 @@ defineInstanceAndStatic(sugarObject, {
802753
* @method add(<obj1>, <obj2>, [options])
803754
* @returns Object
804755
* @short Merges properties in <obj1> and <obj2> and returns a new object.
805-
* @extra See `merge` for options.
756+
* @extra This method will not modify either object. See `merge` for options.
806757
*
807758
* @example
808759
*
@@ -855,7 +806,7 @@ defineInstanceAndStatic(sugarObject, {
855806
* @returns Merged object
856807
* @short Merges properties from one or multiple <sources> into <target> while
857808
* preserving <target>'s properties.
858-
* @extra See `merge` for options.
809+
* @extra This method modifies <target>! See `merge` for options.
859810
*
860811
* @example
861812
*
@@ -884,27 +835,19 @@ defineInstanceAndStatic(sugarObject, {
884835
},
885836

886837
/***
887-
* @method values(<obj>, [fn])
838+
* @method values(<obj>)
888839
* @returns Array
889-
* @short Returns an array containing the values in <obj>. Optionally calls
890-
* [fn] for each value.
891-
* @extra Values are in no particular order.
892-
*
893-
* @callback fn
894-
*
895-
* val The value of the current iteration.
896-
* obj A reference to the object.
840+
* @short Returns an array containing the values in <obj>.
841+
* @extra Values are in no particular order. Does not include inherited or
842+
* non-enumerable properties.
897843
*
898844
* @example
899845
*
900-
* Object.values({ broken: 'wear' }) -> ['wear']
901-
* Object.values(usersByName, function(user) {
902-
* // Called once for each user.
903-
* });
846+
* Object.values({a:'a',b:'b'}) -> ['a','b']
904847
*
905848
***/
906-
'values': function(obj, fn) {
907-
return getValuesWithCallback(obj, fn);
849+
'values': function(obj) {
850+
return getValues(obj);
908851
},
909852

910853
/***
@@ -975,9 +918,9 @@ defineInstanceAndStatic(sugarObject, {
975918
/***
976919
* @method isObject(<obj>)
977920
* @returns Boolean
978-
* @short Returns true if <obj> is a plain object.
979-
* @extra Does not include instances of classes or "host" objects, such as
980-
* Elements, Events, etc.
921+
* @short Returns true if <obj> is a "plain" object.
922+
* @extra Plain objects do not include instances of classes or "host" objects,
923+
* such as Elements, Events, etc.
981924
*
982925
* @example
983926
*
@@ -992,7 +935,7 @@ defineInstanceAndStatic(sugarObject, {
992935
* @method remove(<obj>, <search>)
993936
* @returns Object
994937
* @short Deletes all properties in <obj> matching <search>.
995-
* @extra This method implements `enhanced matching`.
938+
* @extra This method will modify <obj>!. Implements `enhanced matching`.
996939
*
997940
* @callback search
998941
*
@@ -1014,8 +957,8 @@ defineInstanceAndStatic(sugarObject, {
1014957
* @method exclude(<obj>, <search>)
1015958
* @returns Object
1016959
* @short Returns a new object with all properties matching <search> removed.
1017-
* @extra This is a non-destructive version of `remove`. This method
1018-
* implements `enhanced matching`.
960+
* @extra This is a non-destructive version of `remove` and will not modify
961+
* <obj>. Implements `enhanced matching`.
1019962
*
1020963
* @callback search
1021964
*

test/tests/object.js

-22
Original file line numberDiff line numberDiff line change
@@ -770,25 +770,6 @@ namespace('Object', function () {
770770

771771
});
772772

773-
method('keys', function() {
774-
775-
var called = false;
776-
var fn = function(key, o) {
777-
equal(key, 'foo', 'First argument should be key');
778-
equal(o, obj, 'Second argument should be the object');
779-
called = true;
780-
}
781-
782-
var obj = {foo:'bar'};
783-
run(obj, 'keys', [fn]);
784-
equal(called, true, 'Callback should have been called');
785-
786-
// Issue #525
787-
var result = [{foo:'foo'},{bar:'bar'}].map(Sugar.Object.keys);
788-
equal(result, [['foo'],['bar']], 'non-function argument should not be called');
789-
790-
});
791-
792773
method('values', function() {
793774

794775
test({foo:'bar'}, ['bar'], 'Values should be received');
@@ -799,9 +780,6 @@ namespace('Object', function () {
799780
equal(o, obj, 'Second argument should be the object');
800781
called = true;
801782
}
802-
var obj = {foo:'bar'};
803-
var result = run(obj, 'values', [fn]);
804-
equal(called, true, 'Callback should have been called');
805783

806784
// Issue #525
807785
var result = [{foo:'foo'},{bar:'bar'}].map(Sugar.Object.values);

0 commit comments

Comments
 (0)