From 2fda7e47c4a4fd4c8522fbff489ef065743ecd86 Mon Sep 17 00:00:00 2001 From: Matt Bishop Date: Tue, 2 Jan 2018 10:48:17 -1000 Subject: [PATCH] js.spec: add missing spec functions (#22240) * add missing spec.predicate function * Added another missing method problemStr() --- types/js.spec/index.d.ts | 17 +++++++++++++++++ types/js.spec/js.spec-tests.ts | 6 +++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/types/js.spec/index.d.ts b/types/js.spec/index.d.ts index 764bfa845e..e7b3dfe753 100644 --- a/types/js.spec/index.d.ts +++ b/types/js.spec/index.d.ts @@ -103,6 +103,12 @@ export function explain(spec: spec.SpecInput, value: any): void; */ export function explainStr(spec: spec.SpecInput, value: any): string; +/** + * Returns a string with the problem statement from the given Problem. + * @param problem the problem + */ +export function problemStr(problem: Problem): string; + /** * Tests if a value conforms to a spec, and if not, throws an Error. * @param spec the spec to test with @@ -110,6 +116,9 @@ export function explainStr(spec: spec.SpecInput, value: any): string; */ export function assert(spec: spec.SpecInput, value: any): void; +/** + * Symbols used + */ export namespace symbol { /** * Returned by conform() to indicate a value does not conform to a spec. @@ -210,6 +219,14 @@ export namespace spec { */ function oneOf(name: string, ...values: any[]): Spec; + /** + * Used to define a predicate function as a Spec. + * @param name the name of the spec + * @param predicate the predicate function + * @returns the constructed spec + */ + function predicate(name: string, predicate: PredFn): Spec; + // Predicates /** * Returns true if data is an integer. diff --git a/types/js.spec/js.spec-tests.ts b/types/js.spec/js.spec-tests.ts index e1d7703f76..17e49cfea6 100644 --- a/types/js.spec/js.spec-tests.ts +++ b/types/js.spec/js.spec-tests.ts @@ -17,7 +17,9 @@ S.explainData((value) => true, "a value"); const {path, via, value, predicate}: {path: string[], via: string[], value: any, predicate: S.Predicate} = problems[0]; -const problemStr: string = S.explainStr(S.spec.even, 3); +const problemStr: string = S.problemStr(problems[0]); + +const explainStr: string = S.explainStr(S.spec.even, 3); S.explainStr((value) => true, "a value"); // $ExpectType void @@ -46,6 +48,8 @@ const mapSpec: S.Spec = S.spec.map("map test", { email: S.spec.string, [S.symbol const oneOfSpec: S.Spec = S.spec.oneOf("oneOf test", "a", "b", "c"); +const predicateSpec: S.Spec = S.spec.predicate("predicate test", (value) => true); + // Predicates const intPred: S.Predicate = S.spec.int;