maybe-chai
Maybe モナド用の Chai プラグイン。任意のモナドライブラリに適応できます。
インストール
npm install maybe-chai --save-dev
// Before all the tests (use --require in mocha)
import chai from 'chai'
import maybeChai from 'maybe-chai'
chai.use( maybeChai() )
Maybe ライブラリへの適応
既定では Maybe-chai は true-myth
ライブラリで動作します。別のライブラリを使用する場合はアダプタを渡す必要があります (当社には プリコンパイルされたアダプタ があります)。
次のシグニチャに従うオブジェクトを maybeChai()
に渡してアダプタを構成できます
maybeChai( {
match: (maybe: Maybe<T>, cases: MatchCases<T, U>) => U,
isMaybe: (maybe: Maybe<T>) => Boolean,
} )
type MatchCases<T, U> = {
Just: (value: T) => U,
Nothing: () => U,
}
型は厳密には適用されませんが、テストが適切に機能するか確認する必要があります。
ただし、このライブラリの目的は、人気のあるモナドライブラリのそれぞれにアダプタを提供することです。
用途
just
指定の match
関数を使用して、ターゲットが Maybe.Just のインスタンスであることをアサートします。
expect( Maybe.just(5) ).to.be.a.just() // OK!
expect( Maybe.just(5) ).to.be.just(5) // OK!
expect( Maybe.nothing() ).to.be.just(5) // fails
expect( Maybe.nothing() ).to.not.be.just(5) // OK!
expect( 'string' ).to.be.just(5) // fails
expect( 'string' ).to.not.be.just(5) // OK!
.just
はまた、モナドの内容を深く評価します。
expect( Maybe.just({a: 'test'}) ).to.be.just({a: 'test'})
さらに、.just
は、チェーンの後のアサーションのターゲットを、元の Just オブジェクト内の値に変更します。
expect( Maybe.just( { status: 200 } ) ).to.be.a.just()
.and.to.have.property( 'status', 200 ) // OK
nothing
指定の match
関数を使用して、ターゲットが Maybe.Nothing のインスタンスであることをアサートします。
expect( Maybe.nothing() ).to.be.nothing() // OK!
expect( Maybe.just(5) ).to.be.nothing() // fails
expect( Maybe.just(5) ).to.not.be.nothing() // OK!
expect( 'string' ).to.be.nothing() // fails
expect( 'string' ).to.not.be.nothing() // OK!
サポートされるライブラリ
このライブラリを使用すると任意のライブラリにアダプタを提供できますが、ライブラリの目的は、JavaScript の人気のあるモナドライブラリのそれぞれに ここで レシピを提供することです。
- True Myth
- Sanctuary
- TSMonad
- Folktale
- Monet
- Funfix
- Crocks
- KudoJS
- Purify
対象のライブラリがここにない場合、リクエストを発行してください。PR を歓迎します!