Skip to content

Commit

Permalink
Better assertion chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
chuigda committed Sep 20, 2022
1 parent 7d3fcd3 commit fb9f3ad
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
16 changes: 15 additions & 1 deletion test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,23 @@ typeAssert({
]
}, compoundAssertion)

const nonNegativeAssertion = 'number'.chainWith(x => x > 0 || 'no negative numbers')
const nonNegativeAssertion = 'number'
.chainWith(x => x > 0 || 'no negative numbers')
.chainWith(x => x <= 100 || 'no greater than 100')
typeAssert(5, nonNegativeAssertion)
expectFailure(() => typeAssert(-1, nonNegativeAssertion))
expectFailure(() => typeAssert(101, nonNegativeAssertion))

const nonNegativeAssertion2 = 'number?'
.chainWith(x => x > 0 || 'no negative numbers')
.chainWith(x => x <= 100 || 'no greater than 100')

typeAssert(5, nonNegativeAssertion2)
typeAssert(null, nonNegativeAssertion2)
typeAssert(undefined, nonNegativeAssertion2)
expectFailure(() => typeAssert('114514', nonNegativeAssertion2))
expectFailure(() => typeAssert(-1, nonNegativeAssertion2))
expectFailure(() => typeAssert(101, nonNegativeAssertion2))

typeAssert(5, 'number'.assertValue(5))
expectFailure(() => typeAssert(5, 'number'.assertValue(114514)))
Expand Down
16 changes: 15 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,23 @@ typeAssert({
]
}, compoundAssertion)

const nonNegativeAssertion = 'number'.chainWith(x => x > 0 || 'no negative numbers')
const nonNegativeAssertion = 'number'
.chainWith(x => x > 0 || 'no negative numbers')
.chainWith(x => x <= 100 || 'no greater than 100')
typeAssert(5, nonNegativeAssertion)
expectFailure(() => typeAssert(-1, nonNegativeAssertion))
expectFailure(() => typeAssert(101, nonNegativeAssertion))

const nonNegativeAssertion2 = 'number?'
.chainWith(x => x > 0 || 'no negative numbers')
.chainWith(x => x <= 100 || 'no greater than 100')

typeAssert(5, nonNegativeAssertion2)
typeAssert(null, nonNegativeAssertion2)
typeAssert(undefined, nonNegativeAssertion2)
expectFailure(() => typeAssert('114514', nonNegativeAssertion2))
expectFailure(() => typeAssert(-1, nonNegativeAssertion2))
expectFailure(() => typeAssert(101, nonNegativeAssertion2))

typeAssert(5, 'number'.assertValue(5))
expectFailure(() => typeAssert(5, 'number'.assertValue(114514)))
Expand Down
29 changes: 28 additions & 1 deletion typeAssert.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,34 @@ const enableChainAPI = methodNames => {
configurable: false,
writable: false,
value(that) {
return (new ChainType([this]))[chainWithName](that)
let self = this
let nullable = false
if (self.constructor === NullableType.prototype.constructor) {
nullable = true
self = self.origin
return new NullableType(self[chainWithName](that))
}
return (new ChainType([self]))[chainWithName](that)
}
})

Object.defineProperty(String.prototype, chainWithName, {
enumerable: false,
configurable: false,
writable: false,
value(that) {
let nullable = false
let self = `${this}`
if (self.endsWith('?')) {
nullable = true
self = self.slice(0, -1)
}

let ret = (new ChainType([self]))[chainWithName](that)
if (nullable) {
ret = new NullableType(ret)
}
return ret
}
})

Expand Down
29 changes: 28 additions & 1 deletion typeAssert.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,34 @@ export const enableChainAPI = methodNames => {
configurable: false,
writable: false,
value(that) {
return (new ChainType([this]))[chainWithName](that)
let self = this
let nullable = false
if (self.constructor === NullableType.prototype.constructor) {
nullable = true
self = self.origin
return new NullableType(self[chainWithName](that))
}
return (new ChainType([self]))[chainWithName](that)
}
})

Object.defineProperty(String.prototype, chainWithName, {
enumerable: false,
configurable: false,
writable: false,
value(that) {
let nullable = false
let self = `${this}`
if (self.endsWith('?')) {
nullable = true
self = self.slice(0, -1)
}

let ret = (new ChainType([self]))[chainWithName](that)
if (nullable) {
ret = new NullableType(ret)
}
return ret
}
})

Expand Down

0 comments on commit fb9f3ad

Please sign in to comment.