Skip to content

Commit

Permalink
add object_of tool (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
isatiso authored Jan 5, 2024
1 parent 2b24412 commit cff57ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 9 additions & 0 deletions packages/judge/src/matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ describe('matcher.ts', function() {
})
})

describe('#object_of()', function() {
it('should check value if the target type match the given', function() {
expect(Jtl.object_of({ a: Jtl.string, b: Jtl.number }).mismatch({ a: 1, b: 2 })).to.have.property('rule')
expect(Jtl.object_of({ a: Jtl.string, b: Jtl.number }).mismatch({ a: '1', b: 2 })).to.be.undefined
expect(Jtl.object_of({ a: Jtl.string, b: /^abc/ }).mismatch({ a: '1', b: 'ab' })).to.have.property('rule')
expect(Jtl.object_of({ a: Jtl.string, b: /^abc/ }).mismatch({ a: '1', b: 'abcd' })).to.be.undefined
})
})

describe('#object', function() {
it('should check value if is a object', function() {
expect(Jtl.object.mismatch({})).to.be.undefined
Expand Down
14 changes: 10 additions & 4 deletions packages/judge/src/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* found in the LICENSE file at source root.
*/


export type MismatchDescription = {
rule: string,
}
Expand Down Expand Up @@ -59,12 +58,12 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (

export namespace Jtl {

export function some<T extends (Matcher<T> | RegExp)[]>(...rules: T): Matcher<MatcherInferType<T[number]>> {
export function some<T extends (Matcher<any> | RegExp)[]>(...rules: T): Matcher<MatcherInferType<T[number]>> {
return new Matcher(`match at least one rule of [${rules.map(r => `"${get_rule(r)}"`).join('|')}]`,
(target: any) => rules.some(rule => !Matcher.mismatch(target, rule)))
}

export function every<T extends (Matcher<T> | RegExp)[]>(...rules: T): Matcher<UnionToIntersection<MatcherInferType<T[number]>>> {
export function every<T extends (Matcher<any> | RegExp)[]>(...rules: T): Matcher<UnionToIntersection<MatcherInferType<T[number]>>> {
return new Matcher(`match every rule of [${rules.map(r => `"${get_rule(r)}"`).join('|')}]`,
(target: any) => rules.every(rule => !Matcher.mismatch(target, rule)))
}
Expand Down Expand Up @@ -123,11 +122,18 @@ export namespace Jtl {
return new Matcher(`be multiple of factor ${factor}`, (target: any) => typeof target === 'number' && target % factor === 0)
}

export function array_of<T extends Matcher<T> | RegExp>(matcher: T): Matcher<MatcherInferType<T>[]> {
export function array_of<T extends Matcher<any> | RegExp>(matcher: T): Matcher<MatcherInferType<T>[]> {
return new Matcher(`an array containing elements of [${get_rule(matcher)}]`,
(target: any) => target.every((item: any) => !Matcher.mismatch(item, matcher)))
}

export function object_of<T extends {
[key: string]: Matcher<any> | RegExp
}>(matcher: T): Matcher<{ [key in keyof T]: MatcherInferType<T[key]> }> {
return new Matcher(`an object containing properties of [${Object.entries(matcher).map(([k, v]) => `"${k}": ${get_rule(v)}`).join(', ')}]`,
(target: any) => Object.entries(matcher).every(([k, v]) => !Matcher.mismatch(target[k], v)))
}

export function property<P extends string, T extends Matcher<T> | RegExp>(prop: P, matcher: T): Matcher<{ [key in P]: MatcherInferType<T> }> {
return new Matcher(`has property "${prop}" of [${get_rule(matcher)}]`,
(target: any) => target[prop] !== undefined && !Matcher.mismatch(target[prop], matcher))
Expand Down

0 comments on commit cff57ac

Please sign in to comment.