From b78f72105a891ac4d3e8ec6817395e5273a8d1ad Mon Sep 17 00:00:00 2001 From: Bas Huis Date: Tue, 23 Nov 2021 15:49:32 +0100 Subject: [PATCH] Document valid and less usual patterns --- README.mz | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.mz b/README.mz index d16f7f5..1f19f3f 100644 --- a/README.mz +++ b/README.mz @@ -189,6 +189,35 @@ patroon([], 'is array')([]) patroon(Array, 'is array')([]) ``` +A less intuitive case: + +```js ./tape-test > /dev/null +patroon({}, 'is object')([]) +patroon([], 'is array')({}) +``` + +Patroon allows this because Arrays can have properties defined. + +```js ./tape-test > /dev/null +const array = [] +array.prop = 42 + +patroon({prop: _}, 'has prop')(array) +``` + +The other way around is also allowed even if it seems weird. + +```js ./tape-test > /dev/null +const object = {0: 'wow'} +patroon(['wow'], 'has 0th')(object) +``` + +If you do not desire this loose behavior you can use a predicate to make sure +something is an array or object. + +```js ./tape-test > /dev/null +patroon(Array.isArray, 'is array')([]) +``` ### Reference