8000 add isCursor and unCursor · robertpeter07/immutable-js@7ab779e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ab779e

Browse files
committed
add isCursor and unCursor
1 parent eceb992 commit 7ab779e

File tree

7 files changed

+616
-563
lines changed

7 files changed

+616
-563
lines changed

__tests__/Cursor.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
jest.autoMockOff();
55

66
import Immutable = require('immutable');
7+
import Map = Immutable.Map;
78

89
jasmine.getEnv().addEqualityTester((a, b) =>
910
a instanceof Immutable.Sequence && b instanceof Immutable.Sequence ?
@@ -19,7 +20,7 @@ describe('Cursor', () => {
1920
var data = Immutable.fromJS(json);
2021
var cursor = data.cursor();
2122

22-
expect(cursor.deref()).toBe(data);
23+
expect(Immutable.unCursor(cursor)).toBe(data);
2324

2425
var deepCursor = cursor.cursor(['a', 'b']);
2526
expect(deepCursor.toJS()).toEqual(json.a.b);
@@ -30,8 +31,8 @@ describe('Cursor', () => {
3031
expect(leafCursor).toBe(1);
3132

3233
var missCursor = deepCursor.cursor('d');
33-
expect(Immutable.is(missCursor, undefined)).toBe(true);
34-
expect(missCursor.deref()).toBe(undefined);
34+
expect(Immutable.is(missCursor, Map.empty())).toBe(true);
35+
expect(Immutable.unCursor(missCursor)).toEqual(Map.empty());
3536
});
3637

3738
it('appears to be the type it points to', () => {
@@ -48,11 +49,17 @@ describe('Cursor', () => {
4849
).toBe(true);
4950
});
5051

52+
it('can detect cursors', () => {
53+
var data = Immutable.fromJS(json);
54+
expect(Immutable.isCursor(data.get('a'))).toBe(false);
55+
expect(Immutable.isCursor(data.cursor('a'))).toBe(true);
56+
});
57+
5158
it('gets return new cursors', () => {
5259
var data = Immutable.fromJS(json);
5360
var cursor = data.cursor();
5461
var deepCursor = cursor.getIn(['a', 'b']);
55-
expect(deepCursor.deref()).toBe(data.getIn(['a', 'b']));
62+
expect(Immutable.unCursor(deepCursor)).toBe(data.getIn(['a', 'b']));
5663
});
5764

5865
it('can be treated as a value', () => {
@@ -135,9 +142,9 @@ describe('Cursor', () => {
135142
it('creates maps as necessary', () => {
136143
var data = Immutable.Map();
137144
var cursor = data.cursor(['a', 'b', 'c']);
138-
expect(cursor.deref()).toBe(undefined);
145+
expect(cursor).toEqual(< 8000 /span>Map.empty());
139146
cursor = cursor.set('d', 3);
140-
expect(cursor.deref()).toEqual(Immutable.Map({d: 3}));
147+
expect(cursor).toEqual(Immutable.Map({d: 3}));
141148
});
142149

143150
it('has the sequence API', () => {
@@ -151,7 +158,7 @@ describe('Cursor', () => {
151158
var onChange = jest.genMockFunction();
152159
var cursor = data.cursor(onChange);
153160
var found = cursor.find(map => map.get('v') === 2);
154-
expect(typeof found.deref).toBe('function'); // is a cursor!
161+
expect(Immutable.isCursor(found)).toBe(true);
155162
found = found.set('v', 20);
156163
expect(onChange).lastCalledWith(
157164
Immutable.fromJS({a: {v: 1}, b: {v: 20}, c: {v: 3}}),
@@ -167,8 +174,8 @@ describe('Cursor', () => {
167174
var c1 = data.cursor(onChange);
168175
var c2 = c1.withMutations(m => m.set('b', 2).set('c', 3).set('d', 4));
169176

170-
expect(c1.deref().toObject()).toEqual({'a': 1});
171-
expect(c2.deref().toObject()).toEqual({'a': 1, 'b': 2, 'c': 3, 'd': 4});
177+
expect(c1.toObject()).toEqual({'a': 1});
178+
expect(c2.toObject()).toEqual({'a': 1, 'b': 2, 'c': 3, 'd': 4});
172179
expect(onChange.mock.calls.length).toBe(1);
173180
});
174181

@@ -179,8 +186,8 @@ describe('Cursor', () => {
179186
var c1 = data.cursor(['a', 'b', 'c'], onChange);
180187
var c2 = c1.withMutations(m => m.set('x', 1).set('y', 2).set('z', 3));
181188

182-
expect(c1.deref()).toEqual(undefined);
183-
expect(c2.deref()).toEqual(Immutable.fromJS(
189+
expect(c1).toEqual(Map.empty());
190+
expect(c2).toEqual(Immutable.fromJS(
184191
{ x: 1, y: 2, z: 3 }
185192
));
186193
expect(onChange.mock.calls.length).toBe(1);

dist/Immutable.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,24 @@ declare module 'immutable' {
19211921

19221922

19231923

1924+
/**
1925+
* Returns true if the collection provided is actually a Cursor to a
1926+
* collection.
1927+
*
1928+
* @see Map#cursor
1929+
*/
1930+
export function isCursor(maybeCursor: any): boolean;
1931+
1932+
/**
1933+
* If a Cursor, returns the collection referenced, otherwise returns the
1934+
* non-cursor value provided.
1935+
*
1936+
* @see Map#cursor
1937+
*/
1938+
export function unCursor(maybeCursor: any): any;
1939+
1940+
1941+
19241942
// ES6 Iterator
19251943
export interface Iterator<T> {
19261944
next(): { value: T; done: boolean; }

0 commit comments

Comments
 (0)
0