1 | import {ListWrapper} from 'angular2/src/facade/collection';
|
2 | import {bind, provide, Provider, OpaqueToken} from 'angular2/src/core/di';
|
3 |
|
4 | import {Validator} from '../validator';
|
5 | import {MeasureValues} from '../measure_values';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export class SizeValidator extends Validator {
|
11 |
|
12 | static get BINDINGS(): Provider[] { return _PROVIDERS; }
|
13 |
|
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 |
|
35 | var _SAMPLE_SIZE = new OpaqueToken('SizeValidator.sampleSize');
|
36 | var _PROVIDERS = [
|
37 | bind(SizeValidator)
|
38 | .toFactory((size) => new SizeValidator(size), [_SAMPLE_SIZE]),
|
39 | provide(_SAMPLE_SIZE, {useValue: 10})
|
40 | ];
|