diff options
Diffstat (limited to 'src/test/fixtures/namespaced-validators.ts')
-rw-r--r-- | src/test/fixtures/namespaced-validators.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/test/fixtures/namespaced-validators.ts b/src/test/fixtures/namespaced-validators.ts new file mode 100644 index 000000000..fcafcb139 --- /dev/null +++ b/src/test/fixtures/namespaced-validators.ts @@ -0,0 +1,71 @@ +enum AllTheWays { + you = 1, + can = "can", + enum = 2, + really, + for, + namespace, + let, + of, + const, + var, + while, + true = 8, +} + +enum LogLevel { + ERROR, + WARN, + INFO, + DEBUG, +} + +/** + * This is equivalent to: + * type LogLevelStrings = 'ERROR' | 'WARN' | 'INFO' | 'DEBUG'; + */ +type LogLevelStrings = keyof typeof LogLevel; + +function printImportant(key: LogLevelStrings, message: string) { + const num = LogLevel[key]; + if (num <= LogLevel.WARN) { + console.log("Log level key is:", key); + console.log("Log level value is:", num); + console.log("Log level message is:", message); + } +} +printImportant("WARN", "This is a message"); + +namespace Validation { + export interface StringValidator { + isAcceptable(s: string): boolean; + } + const lettersRegexp = /^[A-Za-z]+$/; + const numberRegexp = /^[0-9]+$/; + export class LettersOnlyValidator implements StringValidator { + isAcceptable(s: string) { + return lettersRegexp.test(s); + } + } + export class ZipCodeValidator implements StringValidator { + isAcceptable(s: string) { + return s.length === 5 && numberRegexp.test(s); + } + } +} +// Some samples to try +let strings = ["Hello", "98052", "101"]; +// Validators to use +let validators: { [s: string]: Validation.StringValidator } = {}; +validators["ZIP code"] = new Validation.ZipCodeValidator(); +validators["Letters only"] = new Validation.LettersOnlyValidator(); +// Show whether each string passed each validator +for (let s of strings) { + for (let name in validators) { + console.log( + `"${s}" - ${ + validators[name].isAcceptable(s) ? "matches" : "does not match" + } ${name}` + ); + } +} |