Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Deprecate the use of map/fiter/reduce (see #505) #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions __tests__/RecordJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ jest.autoMockOff();

var Immutable = require('immutable');
var Record = Immutable.Record;
var sinon = require('sinon');
var spy = sinon.spy;

describe('Record', () => {

Expand Down Expand Up @@ -64,4 +66,26 @@ describe('Record', () => {
t2 = t2.clear();
expect(t2.d).toBe(4);
});

it('should warn when using map/filter/reduce', () => {

// Temporarily overwrite console.warn to avoid logging during tests
var _warn = console.warn;
console.warn = () => {};

var MyType = Record({a:1, b:2, c:3});
var t = new MyType({c:'cats'});

var mapFunctionSpy = spy();
var warnSpy = spy(console, 'warn');

t.map(mapFunctionSpy);
expect(mapFunctionSpy.firstCall.args[0]).toBe(1);
expect(mapFunctionSpy.secondCall.args[0]).toBe(2);
expect(mapFunctionSpy.thirdCall.args[0]).toBe('cats');

expect(warnSpy.called).toBe(true);

console.warn = _warn;
});
});
13 changes: 13 additions & 0 deletions dist/immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3756,6 +3756,19 @@
RecordPrototype.asMutable = MapPrototype.asMutable;
RecordPrototype.asImmutable = MapPrototype.asImmutable;

// Some functions from Collection should not be called on a Record, since their behavior is not the one expected
// https://github.com/facebook/immutable-js/issues/505
function deprecateFunction(RecordPrototype, functionName) {
RecordPrototype[functionName] = function() {var $D$0;var S_ITER$0 = typeof Symbol!=='undefined'&&Symbol&&Symbol.iterator||'@@iterator';var S_MARK$0 = typeof Symbol!=='undefined'&&Symbol&&Symbol["__setObjectSetter__"];function ITER$0(v,f){if(v){if(Array.isArray(v))return f?v.slice():v;var i,r;if(S_MARK$0)S_MARK$0(v);if(typeof v==='object'&&typeof (f=v[S_ITER$0])==='function'){i=f.call(v);r=[];}else if((v+'')==='[object Generator]'){i=v;r=[];};if(S_MARK$0)S_MARK$0(void 0);if(r) {while((f=i['next']()),f['done']!==true)r.push(f['value']);return r;}}throw new Error(v+' is not iterable')};var params = SLICE$0.call(arguments, 0);
console.warn((("Record" + functionName) + " has been deprecated. See https://github.com/facebook/immutable-js/issues/505 for more details"));
return ($D$0 = KeyedCollection.prototype[functionName]).call.apply($D$0, [this].concat(ITER$0(params)));
}
}

deprecateFunction(RecordPrototype, 'map');
deprecateFunction(RecordPrototype, 'filter');
deprecateFunction(RecordPrototype, 'reduce');


function makeRecord(likeRecord, map, ownerID) {
var record = Object.create(Object.getPrototypeOf(likeRecord));
Expand Down
Loading