UNPKG

1.3 kBPlain TextView Raw
1import {ListWrapper} from 'angular2/src/facade/collection';
2import {bind, provide, Provider, OpaqueToken} from 'angular2/src/core/di';
3
4import {Validator} from '../validator';
5import {MeasureValues} from '../measure_values';
6
7/**
8 * A validator that waits for the sample to have a certain size.
9 */
10export class SizeValidator extends Validator {
11 // TODO(tbosch): use static values when our transpiler supports them
12 static get BINDINGS(): Provider[] { return _PROVIDERS; }
13 // TODO(tbosch): use static values when our transpiler supports them
14 static get SAMPLE_SIZE() { return _SAMPLE_SIZE; }
15
16 _sampleSize: number;
17
18 constructor(size) {
19 super();
20 this._sampleSize = size;
21 }
22
23 describe(): {[key: string]: any} { return {'sampleSize': this._sampleSize}; }
24
25 validate(completeSample: MeasureValues[]): MeasureValues[] {
26 if (completeSample.length >= this._sampleSize) {
27 return ListWrapper.slice(completeSample, completeSample.length - this._sampleSize,
28 completeSample.length);
29 } else {
30 return null;
31 }
32 }
33}
34
35var _SAMPLE_SIZE = new OpaqueToken('SizeValidator.sampleSize');
36var _PROVIDERS = [
37 bind(SizeValidator)
38 .toFactory((size) => new SizeValidator(size), [_SAMPLE_SIZE]),
39 provide(_SAMPLE_SIZE, {useValue: 10})
40];