Skip to content

Commit

Permalink
Merge pull request #172 from madnetter/add-toArray-options
Browse files Browse the repository at this point in the history
Added an optional options parameter to the toArray method of the data series
  • Loading branch information
ashleydavis authored Mar 13, 2024
2 parents 7a061a8 + 3f3fa22 commit 99555c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export interface ISeries<IndexT = number, ValueT = any> extends Iterable<ValueT>
* const values = series.toArray();
* </pre>
*/
toArray (): ValueT[];
toArray (options?: { includeNulls?: boolean }): ValueT[];

/**
* Retreive the index, values pairs from the series as an array.
Expand Down Expand Up @@ -2913,10 +2913,12 @@ export class Series<IndexT = number, ValueT = any> implements ISeries<IndexT, Va
* const values = series.toArray();
* </pre>
*/
toArray (): any[] {
toArray (options?: { includeNulls?: boolean }): any[] {
const values = [];
for (const value of this.getContent().values) {
if (value !== undefined && value !== null) {
if (options && options.includeNulls && value !== undefined) {
values.push(value);
} else if (value !== undefined && value !== null) {
values.push(value);
}
}
Expand Down Expand Up @@ -6691,4 +6693,4 @@ class OrderedSeries<IndexT = number, ValueT = any, SortT = any>
parent: this
});
}
}
}
23 changes: 23 additions & 0 deletions src/test/series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ describe('Series', () => {
expect(series.toArray()).to.eql([10, 20]);
});

it('Series.toArray({includeNulls: true}) does not strip null values', () => {

var series = new Series([10, null, 20, null]);
expect(series.toArray({ includeNulls: true })).to.eql([10, null, 20, null]);
});

it('Series.toArray({includeNulls: true}) strips undefined values', () => {

var series = new Series([10, undefined, 20, undefined]);
expect(series.toArray({ includeNulls: true })).to.eql([10, 20]);
});

it('Series.toArray({includeNulls: false}) strips null values', () => {

var series = new Series([10, null, 20, null]);
expect(series.toArray({ includeNulls: false })).to.eql([10, 20]);
});

it('Series.toArray({includeNulls: false}) strips undefined values', () => {

var series = new Series([10, undefined, 20, undefined]);
expect(series.toArray({ includeNulls: false })).to.eql([10, 20]);
});

it('Series.toPairs strips undefined values', () => {

Expand Down

0 comments on commit 99555c5

Please sign in to comment.