Skip to content

Core Pattern helpers

Conan edited this page Jun 23, 2022 · 9 revisions

match-iz 🔥 docs

Core Pattern-helpers

match-iz provides a number of composable matchers you can use to build patterns:

Numbers Strings Strings/Arrays Arrays Truthiness Primitives Negation Combinators
gt(n) startsWith('s') includes(o) some(...) empty isArray not(...) allOf(...)
lt(n) endsWith('s') - every(...) falsy isDate - anyOf(...)
gte(n) - - - defined isFunction - firstOf(...)
lte(n) - - - truthy isNumber - lastOf(...)
inRange(x,y) - - - eq(obj) isPojo - includedIn(...)
- - - - deepEp(obj) isRegExp - hasOwn(...)
- - - - - isString - isStrictly(x)
- - - - - instanceOf - -

Just import them from match-iz as you do the core library:

import { gt, lt, ...etc } from 'match-iz'

Numbers

Matchers Meaning
gt / lt / gte / lte / inRange number comparisons
match(5)(
  when(3, 'Exactly 3'),
  when(gt(0), 'Greater than 0'),
  when(gte(4), 'Greater than or equal to 4'),
  when(lt(10), 'Less than 10'),
  when(lte(9), 'Less than or equal to 9'),
  when(inRange(5, 10), 'Between 5 and 10 inclusive')
)

Strings

Matchers Meaning
startsWith / endsWith string comparisons
match('lorem ipsum')(
  when('lipsum', 'Exactly "lipsum"'),
  when(startsWith('ip'), 'Starts with "ip"'),
  when(endsWith('um'), 'Ends with "um"')
)

Strings + Arrays

Helper Meaning
includes string/array comparisons
match('lorem ipsum')(
  when(includes('em'), 'Got "em"'),
  when(includes('zap'), 'Found "zap"')
)

match([1, 2, 3, 4])(
  when(includes(5), 'Array has a 5'),
  when([1, 2, 3, gt(3)], 'Array is [1, 2, 3, >3]')
)

Arrays

Matchers Meaning
some / every array item comparisons
match(array)(
  when(every(isNumber), 'all items are numbers'),
  when(some(isNumber), 'some items are numbers'),
  when(every({ id: isNumber }), 'all items have an id property')
)

Truthiness

Matchers Meaning
empty / defined / falsy / truthy bottom-value comparisons
match('')(
  when(empty, () => {
    return "It's '', {}, [], null, undefined, or NaN"
  }),
  when(defined, 'Opposite of empty'),
  when(falsy, "It's falsy"),
  when(truthy, "It's truthy")
)
Matchers Meaning
eq / deepEq "full" object-equality comparison
match({ one: '1', two: '2', three: '3' })(
  when({ one: '1', two: '2' }, 'partial match')
)
// 'partial match'

match({ one: '1', two: '2', three: '3' })(
  when(eq({ one: '1', two: '2' }), 'shallow match')
)
// undefined

match({ one: '1', two: '2', three: { four: '4', five: '5' } })(
  when(
    eq({ one: '1', two: '2', three: eq({ four: '4' }) }),
    'deep match using nested eq()'
  )
)
// undefined

match({ one: '1', two: '2', three: { four: '4', five: '5' } })(
  when(
    deepEq({ one: '1', two: '2', three: { four: '4' } }),
    'deep match using deepEq()'
  )
)
// undefined

Primitives

Matchers Meaning
isArray / isDate / isFunction / isNumber / isPojo / isRegExp / isString / instanceOf primitive comparisons
match([1, 2, 3])(
  when(isArray, 'Looks like an array, eh?'),
  when(instanceOf(Component), 'A nice component')
)

Negation

Helper Meaning
not negation
match(5)(
  when(not(5), 'Not a 5'),
  when(not(gte(4)), 'Less than 4'),
  when(not(inRange(100, 0)), () => {
    return 'Less than 0 or greater than 100'
  })
)

Combinators

Matchers Meaning
allOf / anyOf / firstOf / lastOf / includedIn / hasOwn / isStrictly combinators
match({ one: 1, two: 2, three: [1, 2, 'a'] })(
  when(allOf(isPojo, hasOwn('one')), () => {
    return 'Has "one"'
  }),
  when({ one: anyOf(1, 2, 3) }, () => {
    return 'Has "one" of 1, 2, or 3'
  }),
  when({ two: includedIn(1, gt(3)) }, () => {
    return 'Has "two" with a value of 1 or >3'
  }),
  when({ three: lastOf(isString) }, () => {
    return 'Has "three" with a string value in the last position'
  })
)

const obj = { one: 1, two: 2 }

match([1, 'a', 3, 4, 5, 6, obj])(
  when(lastOf(isNumber, isString), () => {
    return 'last two items are a number and a string'
  }),
  when(firstOf(isNumber, isString), () => {
    return 'first two items are a number and a string'
  }),
  when(lastOf(isStrictly(obj)), () => {
    return 'last item is strictly equal to the object'
  })
)
Clone this wiki locally