3 ms·
I wish it had actual proper examples. I've no idea how to use that.
by lloydatkinson 5y ago
I wish it had actual proper examples. I've no idea how to use that.
- uryga 5y agofrom a look at the readme, you combine those `$.TYPE` things to build a validation function that checks if its argument matches some pattern (and throws an exception if it doesn't). import * as $ from '@appliedblockchain/assert-combinators' const validateFooBar = ( $.object({ foo: $.string, bar: $.boolean }) ) // probably roughly equivalent to /* const validateFooBar = (x) => { console.assert( typeof x === 'object' && typeof x.foo === 'string' && typeof x.bar === 'boolean' ) return x } */ const test1 = { foo: "abc", bar: false } const test2 = { foo: 0, quux: true } const { foo, bar } = validateFooBar(test1) // ok const oops = validateFooBar(test2) // throws error the source is pretty readable too if you want to get an idea how it works. https://github.com/appliedblockchain/assert-combinators/blob/master/boolean.ts https://github.com/appliedblockchain/assert-combinators/blob... https://github.com/appliedblockchain/assert-combinators/blob/master/object.ts https://github.com/appliedblockchain/assert-combinators/blob...
- mirekrusin 5y agoYes, the difference between console.assert and assert-combinators is that assert combinators return well typed ts result (and are more terse/minimal).
- hermanradtke 5y agoCheck out https://github.com/gcanti/io-ts/blob/master/index.md https://github.com/gcanti/io-ts/blob/master/index.md instead. I find it more composable and you can define a codec and get a native type from it so you are only defining things once.
- mirekrusin 5y agoAssert combinators are composable, light, terse (very little verbosity), types can be defined in single place, instead of type/interface definition you can use return type of assert function. They don’t go into deep category theory, you won’t find monads and friends, they are first level, straightforward any typescript developer can pick up in minutes - this is by design. It stops at combinators in typescript to solve very specific problem and nothing more. Haskell in ts is not the goal of this npm.
- mirekrusin 5y agoThank you, you are right. I’ll add examples and ping here.