Added limitedArray.ts and tests
This commit is contained in:
parent
cc31e18f82
commit
10f19796de
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@rakh/utils",
|
"name": "@rakh/utils",
|
||||||
"version": "2.0.4",
|
"version": "2.0.5",
|
||||||
"main": "dist/commonjs/index.js",
|
"main": "dist/commonjs/index.js",
|
||||||
"module": "dist/es/index.js",
|
"module": "dist/es/index.js",
|
||||||
"jsnext:main": "dist/es/index.js",
|
"jsnext:main": "dist/es/index.js",
|
||||||
|
@ -9,6 +9,7 @@ export * from './hasOwn';
|
|||||||
export * from './hourFloor';
|
export * from './hourFloor';
|
||||||
export * from './isEmpty';
|
export * from './isEmpty';
|
||||||
export * from './latLong';
|
export * from './latLong';
|
||||||
|
export * from './limitedArray';
|
||||||
export * from './maybePluralize';
|
export * from './maybePluralize';
|
||||||
export * from './once';
|
export * from './once';
|
||||||
export * from './partOfDay';
|
export * from './partOfDay';
|
||||||
|
68
ts-src/limitedArray.test.ts
Normal file
68
ts-src/limitedArray.test.ts
Normal file
@ -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]);
|
||||||
|
});*/
|
67
ts-src/limitedArray.ts
Normal file
67
ts-src/limitedArray.ts
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user