Skip to content

Commit

Permalink
indexedSeq.interpose()
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Oct 12, 2014
1 parent f79a64a commit 5e55148
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 22 deletions.
34 changes: 34 additions & 0 deletions __tests__/interpose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
///<reference path='../resources/jest.d.ts'/>
///<reference path='../dist/Immutable.d.ts'/>

jest.autoMockOff();

import I = require('immutable');

describe('interpose', () => {

it('separates with a value', () => {
var range = I.Range(10, 15);
var interposed = range.interpose(0);
expect(interposed.toArray()).toEqual(
[ 10, 0, 11, 0, 12, 0, 13, 0, 14 ]
);
})

it('can be iterated', () => {
var range = I.Range(10, 15);
var interposed = range.interpose(0);
var values = interposed.values();
expect(values.next()).toEqual({ value: 10, done: false });
expect(values.next()).toEqual({ value: 0, done: false });
expect(values.next()).toEqual({ value: 11, done: false });
expect(values.next()).toEqual({ value: 0, done: false });
expect(values.next()).toEqual({ value: 12, done: false });
expect(values.next()).toEqual({ value: 0, done: false });
expect(values.next()).toEqual({ value: 13, done: false });
expect(values.next()).toEqual({ value: 0, done: false });
expect(values.next()).toEqual({ value: 14, done: false });
expect(values.next()).toEqual({ value: undefined, done: true });
})

})
6 changes: 6 additions & 0 deletions dist/Immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,12 @@ declare module 'immutable' {
context?: any
): Sequence<G, any/*IndexedSequence<T>*/>; // Bug: exposing this causes the type checker to implode.

/**
* Returns a lazy sequence with `separator` between each item in this
* sequence.
*/
interpose(separator: T): IndexedSequence<T>;

/**
* Mapper takes IndexedSequence.
* @override
Expand Down
30 changes: 30 additions & 0 deletions dist/Immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,9 @@ var $IndexedSequence = IndexedSequence;
index = wrapIndex(this, index);
return index >= 0 && (this.length != null ? this.length === Infinity || index < this.length : this.indexOf(index) !== -1);
},
interpose: function(separator) {
return interposeFactory(this, separator);
},
last: function() {
return this.get(this.length ? this.length - 1 : 0);
},
Expand Down Expand Up @@ -1485,6 +1488,33 @@ function flattenFactory(sequence, useKeys) {
};
return flatSequence;
}
function interposeFactory(sequence, separator) {
var interposedSequence = sequence.__makeSequence();
interposedSequence.length = sequence.length && sequence.length * 2 - 1;
interposedSequence.__iterateUncached = function(fn, reverse) {
var $__0 = this;
var iterations = 0;
sequence.__iterate((function(v, k) {
return (!iterations || fn(separator, iterations++, $__0) !== false) && fn(v, iterations++, $__0) !== false;
}), reverse);
return iterations;
};
interposedSequence.__iteratorUncached = function(type, reverse) {
var iterator = sequence.__iterator(ITERATE_VALUES, reverse);
var iterations = 0;
var step;
return new Iterator((function() {
if (!step || iterations % 2) {
step = iterator.next();
if (step.done) {
return step;
}
}
return iterations % 2 ? iteratorValue(type, iterations++, separator) : iteratorValue(type, iterations++, step.value, step);
}));
};
return interposedSequence;
}
var Cursor = function Cursor(rootData, keyPath, onChange, value) {
value = value ? value : rootData.getIn(keyPath);
this.length = value instanceof Sequence ? value.length : null;
Expand Down
Loading

0 comments on commit 5e55148

Please sign in to comment.