diff --git a/src/lib/series.ts b/src/lib/series.ts index b8ebc55..5d0691d 100644 --- a/src/lib/series.ts +++ b/src/lib/series.ts @@ -394,7 +394,7 @@ export interface ISeries extends Iterable * const values = series.toArray(); * */ - toArray (): ValueT[]; + toArray (options?: { includeNulls?: boolean }): ValueT[]; /** * Retreive the index, values pairs from the series as an array. @@ -2913,10 +2913,12 @@ export class Series implements ISeries */ - 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); } } @@ -6691,4 +6693,4 @@ class OrderedSeries parent: this }); } -} +} \ No newline at end of file diff --git a/src/test/series.test.ts b/src/test/series.test.ts index f5067a9..a8b0f9d 100644 --- a/src/test/series.test.ts +++ b/src/test/series.test.ts @@ -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', () => {