add LimitedArray

This commit is contained in:
Philipp Kunz 2017-09-21 22:54:22 +02:00
parent 38884ff59c
commit e59205490b
12 changed files with 118 additions and 6 deletions

1
dist/index.d.ts vendored
View File

@ -2,3 +2,4 @@ export * from './lik.looptracker';
export * from './lik.objectmap'; export * from './lik.objectmap';
export * from './lik.observablemap'; export * from './lik.observablemap';
export * from './lik.stringmap'; export * from './lik.stringmap';
export * from './lik.limitedarray';

3
dist/index.js vendored
View File

@ -8,4 +8,5 @@ __export(require("./lik.looptracker"));
__export(require("./lik.objectmap")); __export(require("./lik.objectmap"));
__export(require("./lik.observablemap")); __export(require("./lik.observablemap"));
__export(require("./lik.stringmap")); __export(require("./lik.stringmap"));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUVBLGlCQUFpQjtBQUVqQix1Q0FBaUM7QUFDakMscUNBQStCO0FBQy9CLHlDQUFtQztBQUNuQyxxQ0FBK0IifQ== __export(require("./lik.limitedarray"));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUVBLGlCQUFpQjtBQUVqQix1Q0FBaUM7QUFDakMscUNBQStCO0FBQy9CLHlDQUFtQztBQUNuQyxxQ0FBK0I7QUFDL0Isd0NBQWtDIn0=

9
dist/lik.limitedarray.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
export declare class LimitedArray<T> {
array: T[];
arrayLimit: number;
constructor(limitArg: number);
addOne(objectArg: T): void;
addMany(objectArrayArg: T[]): void;
setLimit(limitArg: number): void;
getAverage(): number;
}

40
dist/lik.limitedarray.js vendored Normal file
View File

@ -0,0 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class LimitedArray {
constructor(limitArg) {
this.array = [];
this.arrayLimit = limitArg;
}
addOne(objectArg) {
this.array.unshift(objectArg);
if (this.array.length > this.arrayLimit) {
this.array.length = this.arrayLimit;
}
}
addMany(objectArrayArg) {
for (let objectArg of objectArrayArg) {
this.addOne(objectArg);
}
}
setLimit(limitArg) {
this.arrayLimit = limitArg;
if (this.array.length > this.arrayLimit) {
this.array.length = this.arrayLimit;
}
}
getAverage() {
if (typeof this.array[0] === 'number') {
let sum = 0;
for (let localNumber of this.array) {
let localNumberAny = localNumber;
sum = sum + localNumberAny;
}
return sum / this.array.length;
}
else {
return null;
}
}
}
exports.LimitedArray = LimitedArray;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibGlrLmxpbWl0ZWRhcnJheS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL2xpay5saW1pdGVkYXJyYXkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFFQTtJQUdFLFlBQVksUUFBZ0I7UUFGNUIsVUFBSyxHQUFRLEVBQUUsQ0FBQTtRQUdiLElBQUksQ0FBQyxVQUFVLEdBQUcsUUFBUSxDQUFBO0lBQzVCLENBQUM7SUFFRCxNQUFNLENBQUUsU0FBWTtRQUNsQixJQUFJLENBQUMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxTQUFTLENBQUMsQ0FBQTtRQUM3QixFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsS0FBSyxDQUFDLE1BQU0sR0FBRyxJQUFJLENBQUMsVUFBVSxDQUFDLENBQUMsQ0FBQztZQUN4QyxJQUFJLENBQUMsS0FBSyxDQUFDLE1BQU0sR0FBRyxJQUFJLENBQUMsVUFBVSxDQUFBO1FBQ3JDLENBQUM7SUFDSCxDQUFDO0lBRUQsT0FBTyxDQUFFLGNBQW1CO1F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expect } from 'tapbundle'
import { LimitedArray } from '../ts/index'
let testLimitedArray: LimitedArray<string>
tap.test('should create a LimitedArray', async () => {
testLimitedArray = new LimitedArray(6)
expect(testLimitedArray).to.be.instanceof(LimitedArray)
})
tap.test('should never be longer than the set length', async () => {
testLimitedArray.addMany(['hi','this','is','quite','a','long','string',':)'])
expect(testLimitedArray.array.length).to.be.lessThan(7)
})
tap.start()

View File

@ -5,7 +5,7 @@ import * as rx from 'rxjs/Rx'
import * as smartq from 'smartq' import * as smartq from 'smartq'
// import the module // import the module
import lik = require('../ts/index') import * as lik from '../ts/index'
let object1 = {} let object1 = {}
let object2 = {} let object2 = {}

View File

@ -5,7 +5,7 @@ import * as rx from 'rxjs/Rx'
import * as smartq from 'smartq' import * as smartq from 'smartq'
// import the module // import the module
import lik = require('../ts/index') import * as lik from '../ts/index'
// Objectmap // Objectmap
interface ITestObject { interface ITestObject {

View File

@ -5,7 +5,7 @@ import * as rx from 'rxjs/Rx'
import * as smartq from 'smartq' import * as smartq from 'smartq'
// import the module // import the module
import lik = require('../ts/index') import * as lik from '../ts/index'
let testObservablemap: lik.Observablemap let testObservablemap: lik.Observablemap
let testObservable1: rx.Observable<any> let testObservable1: rx.Observable<any>

View File

@ -5,7 +5,7 @@ import * as rx from 'rxjs/Rx'
import * as smartq from 'smartq' import * as smartq from 'smartq'
// import the module // import the module
import lik = require('../ts/index') import * as lik from '../ts/index'
// testData // testData
let testStringmap: lik.Stringmap let testStringmap: lik.Stringmap

View File

@ -6,3 +6,4 @@ export * from './lik.looptracker'
export * from './lik.objectmap' export * from './lik.objectmap'
export * from './lik.observablemap' export * from './lik.observablemap'
export * from './lik.stringmap' export * from './lik.stringmap'
export * from './lik.limitedarray'

43
ts/lik.limitedarray.ts Normal file
View File

@ -0,0 +1,43 @@
import * as plugins from './lik.plugins'
export class LimitedArray<T> {
array: T[] = []
arrayLimit: number
constructor(limitArg: number) {
this.arrayLimit = limitArg
}
addOne (objectArg: T) {
this.array.unshift(objectArg)
if (this.array.length > this.arrayLimit) {
this.array.length = this.arrayLimit
}
}
addMany (objectArrayArg: T[]) {
for (let objectArg of objectArrayArg) {
this.addOne(objectArg)
}
}
setLimit (limitArg: number) {
this.arrayLimit = limitArg
if (this.array.length > this.arrayLimit) {
this.array.length = this.arrayLimit
}
}
getAverage ():number {
if (typeof this.array[0] === 'number') {
let sum = 0
for (let localNumber of this.array) {
let localNumberAny: any = localNumber
sum = sum + localNumberAny
}
return sum / this.array.length
} else {
return null
}
}
}