arozwalak
3/9/2017 - 10:14 PM

ES6: Modules

ES6: Modules

/// <reference path="Validation.ts" />
module Validation {
  var numberRegexp = /^[0-9]+$/;
  export class ZipCodeValidator implements StringValidator {
    isAcceptable(s:string) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
}
module Validation {
  export interface StringValidator {
    isAcceptable(s: string): boolean;
  }
}
/// <reference path="Validation.ts" />
/// <reference path="LettersOnlyValidator.ts" />
/// <reference path="ZipCodeValidator.ts" />

// samples to try
var strings = ['Hello', '90210', '101'];
// validators to use
var validators: { [s: string]: Validation.StringValidator } = {};
validators['ZIP code'] = new Validation.ZipCodeValidator();
validators['Letters only'] = new Validation.LettersOnlyValidator();
// show whether each string passed each validator
strings.forEach(s => {
  for (var name in validators) {
    console.log('"' + s + '" ' + (validators[name].isAcceptable(s) ? ' matches ' : ' does not match ') + name);
  }
});

/// <reference path="Validation.ts" />
module Validation {
  var lettersRegexp = /^[A-Za-z]+$/;
  export class LettersOnlyValidator implements StringValidator {
    isAcceptable(s:string) {
      return lettersRegexp.test(s);
    }
  }
}