Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f28ec3b

Browse filesBrowse files
authored
Adds back delete() and clear() to Record (immutable-js#1157)
This adds back the delete() and clear() methods to Record instances that set values back to the default values, in the process improving the equals() and hashCode() implementations.
1 parent 52f8326 commit f28ec3b
Copy full SHA for f28ec3b

10 files changed

+132
-25
lines changed

‎__tests__/Record.ts

Copy file name to clipboardExpand all lines: __tests__/Record.ts
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ describe('Record', () => {
4848
expect(t2).toBe(t1);
4949
});
5050

51+
it('falls back to default values when deleted or cleared', () => {
52+
const MyType = Record({ a: 1, b: 2, c: 3 });
53+
const t1 = new MyType({ a: 10, b: 20 });
54+
const t2 = new MyType({ b: 20 });
55+
const t3 = t1.delete('a');
56+
const t4 = t3.clear();
57+
58+
expect(t1.get('a')).toBe(10);
59+
expect(t2.get('a')).toBe(1);
60+
expect(t3.get('a')).toBe(1);
61+
expect(t4.get('b')).toBe(2);
62+
63+
expect(t2.equals(t3)).toBe(true);
64+
expect(t2.equals(t4)).toBe(false);
65+
expect(t4.equals(new MyType())).toBe(true);
66+
});
67+
5168
it('is a value type and equals other similar Records', () => {
5269
let MyType = Record({a: 1, b: 2, c: 3});
5370
let t1 = MyType({ a: 10 });

‎__tests__/RecordJS.js

Copy file name to clipboardExpand all lines: __tests__/RecordJS.js
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,20 @@ describe('Record', () => {
4545
expect(t.soup()).toBe(6);
4646
expect(t2.soup()).toBe(204);
4747
});
48+
49+
it('can be cleared', () => {
50+
const MyType = Record({ a: 1, b: 2, c: 3 });
51+
let t = new MyType({ c: 'cats' });
52+
53+
expect(t.c).toBe('cats');
54+
t = t.clear();
55+
expect(t.c).toBe(3);
56+
57+
const MyType2 = Record({ d: 4, e: 5, f: 6 });
58+
let t2 = new MyType2({ d: 'dogs' });
59+
60+
expect(t2.d).toBe('dogs');
61+
t2 = t2.clear();
62+
expect(t2.d).toBe(4);
63+
});
4864
});

‎dist/immutable-nonambient.d.ts

Copy file name to clipboardExpand all lines: dist/immutable-nonambient.d.ts
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,21 @@
21622162
...collections: Array<Partial<T> | Iterable<[string, any]>>
21632163
): this;
21642164

2165+
/**
2166+
* Returns a new instance of this Record type with the value for the
2167+
* specific key set to its default value.
2168+
*
2169+
* @alias remove
2170+
*/
2171+
delete<K extends keyof T>(key: K): this;
2172+
remove<K extends keyof T>(key: K): this;
2173+
2174+
/**
2175+
* Returns a new instance of this Record type with all values set
2176+
* to their default values.
2177+
*/
2178+
clear(): this;
2179+
21652180
// Deep persistent changes
21662181

21672182
setIn(keyPath: Iterable<any>, value: any): this;

‎dist/immutable.d.ts

Copy file name to clipboardExpand all lines: dist/immutable.d.ts
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,21 @@ declare module Immutable {
21622162
...collections: Array<Partial<T> | Iterable<[string, any]>>
21632163
): this;
21642164

2165+
/**
2166+
* Returns a new instance of this Record type with the value for the
2167+
* specific key set to its default value.
2168+
*
2169+
* @alias remove
2170+
*/
2171+
delete<K extends keyof T>(key: K): this;
2172+
remove<K extends keyof T>(key: K): this;
2173+
2174+
/**
2175+
* Returns a new instance of this Record type with all values set
2176+
* to their default values.
2177+
*/
2178+
clear(): this;
2179+
21652180
// Deep persistent changes
21662181

21672182
setIn(keyPath: Iterable<any>, value: any): this;

‎dist/immutable.js

Copy file name to clipboardExpand all lines: dist/immutable.js
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5234,11 +5234,11 @@ Record.prototype.toString = function toString () {
52345234

52355235
Record.prototype.equals = function equals (other) {
52365236
return this === other ||
5237-
(this._keys === other._keys && this._values.equals(other._values));
5237+
(this._keys === other._keys && recordSeq(this).equals(recordSeq(other)));
52385238
};
52395239

52405240
Record.prototype.hashCode = function hashCode () {
5241-
return this._values.hashCode();
5241+
return recordSeq(this).hashCode();
52425242
};
52435243

52445244
// @pragma Access
@@ -5271,6 +5271,15 @@ Record.prototype.set = function set (k, v) {
52715271
return this;
52725272
};
52735273

5274+
Record.prototype.remove = function remove (k) {
5275+
return this.set(k);
5276+
};
5277+
5278+
Record.prototype.clear = function clear () {
5279+
var newValues = this._values.clear().setSize(this._keys.length);
5280+
return this.__ownerID ? this : makeRecord(this, newValues);
5281+
};
5282+
52745283
Record.prototype.wasAltered = function wasAltered () {
52755284
return this._values.wasAltered();
52765285
};
@@ -5308,6 +5317,7 @@ Record.isRecord = isRecord;
53085317
Record.getDescriptiveName = recordName;
53095318
var RecordPrototype = Record.prototype;
53105319
RecordPrototype[IS_RECORD_SENTINEL] = true;
5320+
RecordPrototype[DELETE] = RecordPrototype.remove;
53115321
RecordPrototype.getIn = CollectionPrototype.getIn;
53125322
RecordPrototype.hasIn = CollectionPrototype.hasIn;
53135323
RecordPrototype.merge = MapPrototype.merge;

‎dist/immutable.js.flow

Copy file name to clipboardExpand all lines: dist/immutable.js.flow
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,10 @@ declare class RecordInstance<T: Object> {
10911091
...collections: Array<$Shape<T> | Iterable<[string, any]>>
10921092
): this;
10931093

1094+
delete<K: $Keys<T>>(key: K): this;
1095+
remove<K: $Keys<T>>(key: K): this;
1096+
clear(): this;
1097+
10941098
setIn(keyPath: Iterable<any>, value: any): this;
10951099
updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this;
10961100
mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this;

‎dist/immutable.min.js

Copy file name to clipboardExpand all lines: dist/immutable.min.js
+21-21Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Record.js

Copy file name to clipboardExpand all lines: src/Record.js
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { List } from './List';
1414
import { ITERATOR_SYMBOL } from './Iterator';
1515
import { isRecord, IS_RECORD_SENTINEL } from './Predicates';
1616
import { CollectionPrototype } from './CollectionImpl';
17+
import { DELETE } from './TrieUtils';
1718

1819
import invariant from './utils/invariant';
1920
import quoteString from './utils/quoteString';
@@ -86,11 +87,11 @@ export class Record {
8687

8788
equals(other) {
8889
return this === other ||
89-
(this._keys === other._keys && this._values.equals(other._values));
90+
(this._keys === other._keys && recordSeq(this).equals(recordSeq(other)));
9091
}
9192

9293
hashCode() {
93-
return this._values.hashCode();
94+
return recordSeq(this).hashCode();
9495
}
9596

9697
// @pragma Access
@@ -123,6 +124,15 @@ export class Record {
123124
return this;
124125
}
125126

127+
remove(k) {
128+
return this.set(k);
129+
}
130+
131+
clear() {
132+
const newValues = this._values.clear().setSize(this._keys.length);
133+
return this.__ownerID ? this : makeRecord(this, newValues);
134+
}
135+
126136
wasAltered() {
127137
return this._values.wasAltered();
128138
}
@@ -161,6 +171,7 @@ Record.isRecord = isRecord;
161171
Record.getDescriptiveName = recordName;
162172
const RecordPrototype = Record.prototype;
163173
RecordPrototype[IS_RECORD_SENTINEL] = true;
174+
RecordPrototype[DELETE] = RecordPrototype.remove;
164175
RecordPrototype.getIn = CollectionPrototype.getIn;
165176
RecordPrototype.hasIn = CollectionPrototype.hasIn;
166177
RecordPrototype.merge = MapPrototype.merge;

‎type-definitions/Immutable.d.ts

Copy file name to clipboardExpand all lines: type-definitions/Immutable.d.ts
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,21 @@ declare module Immutable {
21622162
...collections: Array<Partial<T> | Iterable<[string, any]>>
21632163
): this;
21642164

2165+
/**
2166+
* Returns a new instance of this Record type with the value for the
2167+
* specific key set to its default value.
2168+
*
2169+
* @alias remove
2170+
*/
2171+
delete<K extends keyof T>(key: K): this;
2172+
remove<K extends keyof T>(key: K): this;
2173+
2174+
/**
2175+
* Returns a new instance of this Record type with all values set
2176+
* to their default values.
2177+
*/
2178+
clear(): this;
2179+
21652180
// Deep persistent changes
21662181

21672182
setIn(keyPath: Iterable<any>, value: any): this;

‎type-definitions/immutable.js.flow

Copy file name to clipboardExpand all lines: type-definitions/immutable.js.flow
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,10 @@ declare class RecordInstance<T: Object> {
10911091
...collections: Array<$Shape<T> | Iterable<[string, any]>>
10921092
): this;
10931093

1094+
delete<K: $Keys<T>>(key: K): this;
1095+
remove<K: $Keys<T>>(key: K): this;
1096+
clear(): this;
1097+
10941098
setIn(keyPath: Iterable<any>, value: any): this;
10951099
updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this;
10961100
mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.