diff --git a/package.json b/package.json index 61bf450..b3b5511 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rakh/utils", - "version": "2.0.4", + "version": "2.0.5", "main": "dist/commonjs/index.js", "module": "dist/es/index.js", "jsnext:main": "dist/es/index.js", diff --git a/ts-src/index.ts b/ts-src/index.ts index c91653e..8054e78 100644 --- a/ts-src/index.ts +++ b/ts-src/index.ts @@ -9,6 +9,7 @@ export * from './hasOwn'; export * from './hourFloor'; export * from './isEmpty'; export * from './latLong'; +export * from './limitedArray'; export * from './maybePluralize'; export * from './once'; export * from './partOfDay'; diff --git a/ts-src/limitedArray.test.ts b/ts-src/limitedArray.test.ts new file mode 100644 index 0000000..710d72a --- /dev/null +++ b/ts-src/limitedArray.test.ts @@ -0,0 +1,68 @@ +import { limitedArray } from './limitedArray'; + +test('should initialize an empty array', function () { + let lArray = new limitedArray(); + + expect(lArray.length()).toEqual(0); +}); + +test('Add an item to the array', function () { + let lArray = new limitedArray(); + + lArray.push('bob'); + + expect(lArray.length()).toEqual(1); +}); + +test('Add items to the array', function () { + let lArray = new limitedArray(); + + lArray.add([1, 2, 3, 4, 5]); + + expect(lArray.length()).toEqual(5); +}); + +test('Add too many items', function () { + let lArray = new limitedArray(5); + lArray.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + + expect(lArray.length()).toEqual(5); +}); + +test('Change limit', function () { + let lArray = new limitedArray(); + + lArray.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + lArray.limit(5); + + expect(lArray.length()).toEqual(5); +}); + +test('Get items', function () { + let lArray = new limitedArray(); + + lArray.push('bob'); + expect(lArray.get()).toEqual(['bob']); +}); + +test('Add too many items and get the results', function () { + let lArray = new limitedArray(5); + lArray.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + + expect(lArray.get()).toEqual([6, 7, 8, 9, 10]); +}); + +test('Add one more', function () { + let lArray = new limitedArray(5); + lArray.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + lArray.push(11); + + expect(lArray.get()).toEqual([7, 8, 9, 10, 11]); +}); + + +/*test('Push many', function () { + let lArray = new limitedArray(5); + lArray.push(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + expect(lArray.get()).toEqual([6, 7, 8, 9, 10]); +});*/ diff --git a/ts-src/limitedArray.ts b/ts-src/limitedArray.ts new file mode 100644 index 0000000..bdd6c21 --- /dev/null +++ b/ts-src/limitedArray.ts @@ -0,0 +1,67 @@ +export class limitedArray { + _array: any[]; + _limit: number; + + /** + * Used to construct the LimitedArray + * Defaults to 100 when no size is passed in + * @param size + */ + constructor(size = 100) { + this._array = []; + this._limit = size; + } + + /** + * Add items to the end of an array + * @param item + */ + + push(item: any): void { + this._array.push(item); + if (this._array.length > this._limit) this._array.shift(); + } + + /** + * Return the items within the array + * @returns {[]} + */ + get(): any[] { + return this._array; + } + + /** + * Limits the size of the array + * @param size + */ + limit(size: number): void { + this._limit = size; + if (this._array.length > this._limit) this._array = this._array.slice(this._array.length - this._limit); + } + + /** + * Returns the length of the array + * @returns {number} + */ + length(): number { + return this._array.length; + } + + /** + * Bulk add an array to the array + * @param items + */ + add(items: any[]): void { + this._array = this._array.concat(items); + + if (this._array.length > this._limit) this._array = this._array.slice(this._array.length - this._limit); + } + + /** + * Remove an item from the beginning of an array + * @returns {*} + */ + shift(): any { + return this._array.shift(); + } +}