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

Skip to content

Adds back delete() and clear() to Record #1157

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

Merged
merged 1 commit into from
Mar 13, 2017
Merged
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
17 changes: 17 additions & 0 deletions __tests__/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ describe('Record', () => {
expect(t2).toBe(t1);
});

it('falls back to default values when deleted or cleared', () => {
const MyType = Record({ a: 1, b: 2, c: 3 });
const t1 = new MyType({ a: 10, b: 20 });
const t2 = new MyType({ b: 20 });
const t3 = t1.delete('a');
const t4 = t3.clear();

expect(t1.get('a')).toBe(10);
expect(t2.get('a')).toBe(1);
expect(t3.get('a')).toBe(1);
expect(t4.get('b')).toBe(2);

expect(t2.equals(t3)).toBe(true);
expect(t2.equals(t4)).toBe(false);
expect(t4.equals(new MyType())).toBe(true);
});

it('is a value type and equals other similar Records', () => {
let MyType = Record({a: 1, b: 2, c: 3});
let t1 = MyType({ a: 10 });
Expand Down
16 changes: 16 additions & 0 deletions __tests__/RecordJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ describe('Record', () => {
expect(t.soup()).toBe(6);
expect(t2.soup()).toBe(204);
});

it('can be cleared', () => {
const MyType = Record({ a: 1, b: 2, c: 3 });
let t = new MyType({ c: 'cats' });

expect(t.c).toBe('cats');
t = t.clear();
expect(t.c).toBe(3);

const MyType2 = Record({ d: 4, e: 5, f: 6 });
let t2 = new MyType2({ d: 'dogs' });

expect(t2.d).toBe('dogs');
t2 = t2.clear();
expect(t2.d).toBe(4);
});
});
15 changes: 15 additions & 0 deletions dist/immutable-nonambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,21 @@
...collections: Array<Partial<T> | Iterable<[string, any]>>
): this;

/**
* Returns a new instance of this Record type with the value for the
* specific key set to its default value.
*
* @alias remove
*/
delete<K extends keyof T>(key: K): this;
remove<K extends keyof T>(key: K): this;

/**
* Returns a new instance of this Record type with all values set
* to their default values.
*/
clear(): this;

// Deep persistent changes

setIn(keyPath: Iterable<any>, value: any): this;
Expand Down
15 changes: 15 additions & 0 deletions dist/immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,21 @@ declare module Immutable {
...collections: Array<Partial<T> | Iterable<[string, any]>>
): this;

/**
* Returns a new instance of this Record type with the value for the
* specific key set to its default value.
*
* @alias remove
*/
delete<K extends keyof T>(key: K): this;
remove<K extends keyof T>(key: K): this;

/**
* Returns a new instance of this Record type with all values set
* to their default values.
*/
clear(): this;

// Deep persistent changes

setIn(keyPath: Iterable<any>, value: any): this;
Expand Down
14 changes: 12 additions & 2 deletions dist/immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5234,11 +5234,11 @@ Record.prototype.toString = function toString () {

Record.prototype.equals = function equals (other) {
return this === other ||
(this._keys === other._keys && this._values.equals(other._values));
(this._keys === other._keys && recordSeq(this).equals(recordSeq(other)));
};

Record.prototype.hashCode = function hashCode () {
return this._values.hashCode();
return recordSeq(this).hashCode();
};

// @pragma Access
Expand Down Expand Up @@ -5271,6 +5271,15 @@ Record.prototype.set = function set (k, v) {
return this;
};

Record.prototype.remove = function remove (k) {
return this.set(k);
};

Record.prototype.clear = function clear () {
var newValues = this._values.clear().setSize(this._keys.length);
return this.__ownerID ? this : makeRecord(this, newValues);
};

Record.prototype.wasAltered = function wasAltered () {
return this._values.wasAltered();
};
Expand Down Expand Up @@ -5308,6 +5317,7 @@ Record.isRecord = isRecord;
Record.getDescriptiveName = recordName;
var RecordPrototype = Record.prototype;
RecordPrototype[IS_RECORD_SENTINEL] = true;
RecordPrototype[DELETE] = RecordPrototype.remove;
RecordPrototype.getIn = CollectionPrototype.getIn;
RecordPrototype.hasIn = CollectionPrototype.hasIn;
RecordPrototype.merge = MapPrototype.merge;
Expand Down
4 changes: 4 additions & 0 deletions dist/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,10 @@ declare class RecordInstance<T: Object> {
...collections: Array<$Shape<T> | Iterable<[string, any]>>
): this;

delete<K: $Keys<T>>(key: K): this;
remove<K: $Keys<T>>(key: K): this;
clear(): this;

setIn(keyPath: Iterable<any>, value: any): this;
updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this;
mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
Expand Down
42 changes: 21 additions & 21 deletions dist/immutable.min.js

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions src/Record.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { List } from './List';
import { ITERATOR_SYMBOL } from './Iterator';
import { isRecord, IS_RECORD_SENTINEL } from './Predicates';
import { CollectionPrototype } from './CollectionImpl';
import { DELETE } from './TrieUtils';

import invariant from './utils/invariant';
import quoteString from './utils/quoteString';
Expand Down Expand Up @@ -86,11 +87,11 @@ export class Record {

equals(other) {
return this === other ||
(this._keys === other._keys && this._values.equals(other._values));
(this._keys === other._keys && recordSeq(this).equals(recordSeq(other)));
}

hashCode() {
return this._values.hashCode();
return recordSeq(this).hashCode();
}

// @pragma Access
Expand Down Expand Up @@ -123,6 +124,15 @@ export class Record {
return this;
}

remove(k) {
return this.set(k);
}

clear() {
const newValues = this._values.clear().setSize(this._keys.length);
return this.__ownerID ? this : makeRecord(this, newValues);
}

wasAltered() {
return this._values.wasAltered();
}
Expand Down Expand Up @@ -161,6 +171,7 @@ Record.isRecord = isRecord;
Record.getDescriptiveName = recordName;
const RecordPrototype = Record.prototype;
RecordPrototype[IS_RECORD_SENTINEL] = true;
RecordPrototype[DELETE] = RecordPrototype.remove;
RecordPrototype.getIn = CollectionPrototype.getIn;
RecordPrototype.hasIn = CollectionPrototype.hasIn;
RecordPrototype.merge = MapPrototype.merge;
Expand Down
15 changes: 15 additions & 0 deletions type-definitions/Immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,21 @@ declare module Immutable {
...collections: Array<Partial<T> | Iterable<[string, any]>>
): this;

/**
* Returns a new instance of this Record type with the value for the
* specific key set to its default value.
*
* @alias remove
*/
delete<K extends keyof T>(key: K): this;
remove<K extends keyof T>(key: K): this;

/**
* Returns a new instance of this Record type with all values set
* to their default values.
*/
clear(): this;

// Deep persistent changes

setIn(keyPath: Iterable<any>, value: any): this;
Expand Down
4 changes: 4 additions & 0 deletions type-definitions/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,10 @@ declare class RecordInstance<T: Object> {
...collections: Array<$Shape<T> | Iterable<[string, any]>>
): this;

delete<K: $Keys<T>>(key: K): this;
remove<K: $Keys<T>>(key: K): this;
clear(): this;

setIn(keyPath: Iterable<any>, value: any): this;
updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this;
mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
Expand Down