8000 Merge pull request #365 from tcoopman/list_methods · githubsheng/immutable-js@64eb617 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>

Commit 64eb617

Browse files
committed
Merge pull request immutable-js#365 from tcoopman/list_methods
added List methods push, pop, unshift, shift to List IndexedCursor
2 parents 577c9a0 + 6186342 commit 64eb617

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

contrib/cursor/__tests__/Cursor.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,59 @@ describe('Cursor', () => {
186186
expect(cursor.map((x: number) => x * x)).toEqual(Immutable.Map({a: 1, b: 4, c: 9}));
187187
});
188188

189+
it('can push values on a List', () => {
190+
var onChange = jest.genMockFunction();
191+
var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
192+
var cursor = Cursor.from(data, ['a', 'b'], onChange);
193+
194+
expect(cursor.push(3,4)).toEqual(Immutable.List([0, 1, 2, 3, 4]));
195+
expect(onChange).lastCalledWith(
196+
Immutable.fromJS({a: {b: [0, 1, 2, 3, 4]}}),
197+
data,
198+
['a', 'b']
199+
);
200+
});
201+
202+
it('can pop values of a List', () => {
203+
var onChange = jest.genMockFunction();
204+
var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
205+
var cursor = Cursor.from(data, ['a', 'b'], onChange);
206+
207+
expect(cursor.pop()).toEqual(Immutable 8000 .List([0, 1]));
208+
expect(onChange).lastCalledWith(
209+
Immutable.fromJS({a: {b: [0, 1]}}),
210+
data,
211+
['a', 'b']
212+
);
213+
});
214+
215+
it('can unshift values on a List', () => {
216+
var onChange = jest.genMockFunction();
217+
var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
218+
var cursor = Cursor.from(data, ['a', 'b'], onChange);
219+
220+
expect(cursor.unshift(-2, -1)).toEqual(Immutable.List([-2, -1, 0, 1, 2]));
221+
expect(onChange).lastCalledWith(
222+
Immutable.fromJS({a: {b: [-2, -1, 0, 1, 2]}}),
223+
data,
224+
['a', 'b']
225+
);
226+
});
227+
228+
it('can shift values of a List', () => {
229+
var onChange = jest.genMockFunction();
230+
var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
231+
var cursor = Cursor.from(data, ['a', 'b'], onChange);
232+
233+
expect(cursor.shift()).toEqual(Immutable.List([1, 2]));
234+
expect(onChange).lastCalledWith(
235+
Immutable.fromJS({a: {b: [1, 2]}}),
236+
data,
237+
['a', 'b']
238+
);
239+
});
240+
241+
189242
it('returns wrapped values for sequence API', () => {
190243
var data = Immutable.fromJS({a: {v: 1}, b: {v: 2}, c: {v: 3}});
191244
var onChange = jest.genMockFunction();

contrib/cursor/index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,29 @@ declare module 'immutable/contrib/cursor' {
164164
setIn(keyPath: Array<any>, value: any): Cursor;
165165
setIn(keyPath: Immutable.Iterable<any, any>, value: any): Cursor;
166166

167+
/**
168+
* Returns a new Cursor with provided `values` appended
169+
*/
170+
push(...values: Array<any>): Cursor;
171+
172+
/**
173+
* Returns a new Cursor with a size ones less than this Cursor,
174+
* excluding the last index in this Cursor.
175+
*/
176+
pop(): Cursor;
177+
178+
/**
179+
* Returns a new Cursor with the provided `values` prepended,
180+
* shifting other values ahead to higher indices.
181+
*/
182+
unshift(...values: Array<any>): Cursor;
183+
184+
/**
185+
* Returns a new Cursor with a size ones less than this Cursor, excluding
186+
* the first index in this Cursor, shifting all other values to a lower index.
187+
*/
188+
shift(): Cursor;
189+
167190
/**
168191
* Returns a new Cursor having removed the value at this `keyPath`.
169192
*

contrib/cursor/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ KeyedCursorPrototype.set = function(key, value) {
8888
return updateCursor(this, function (m) { return m.set(key, value); }, [key]);
8989
}
9090

91+
IndexedCursorPrototype.push = function(/* values */) {
92+
var args = arguments;
93+
return updateCursor(this, function (m) {
94+
return m.push.apply(m, args);
95+
});
96+
}
97+
98+
IndexedCursorPrototype.pop = function() {
99+
return updateCursor(this, function (m) {
100+
return m.pop();
101+
});
102+
}
103+
104+
IndexedCursorPrototype.unshift = function(/* values */) {
105+
var args = arguments;
106+
return updateCursor(this, function (m) {
107+
return m.unshift.apply(m, args);
108+
});
109+
}
110+
111+
IndexedCursorPrototype.shift = function() {
112+
return updateCursor(this, function (m) {
113+
return m.shift();
114+
});
115+
}
116+
91117
IndexedCursorPrototype.setIn =
92118
KeyedCursorPrototype.setIn = Map.prototype.setIn;
93119

0 commit comments

Comments
 (0)
0