All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
- Adds types export to pkg.json#exports. Thanks to @olmesm.
- Fix lint command
index.d.ts
: Updated to parse patterns into types when the input is sent into yourwhen
handlers:
Some examples:
when({ one: 1 as const, two: isNumber }, (res) => {
// res will be typed as = { one: 1, two: number }
})
when({ name: isString, age: isNumber }, (res) => {
// res will be typed as = { name: string, age: number }
})
You can have your own predicates conform by using the is
keyword:
function isString(value: any): value is string {
return typeof value === 'string';
}
index.d.ts
: PassInput
intoTPattern
so we can get autocomplete inwhen()
s.
- 'months' was broken for
inTheNext/Future/Past
because the minutes RegExp got there first, makinginTheNext(6, 'months')
assume 6 minutes.
matchiz
now includes a runtime exhaustiveness check. If no patterns match and nootherwise
was specified, an error will be thrown.4.0.0
is otherwise identical to3.10.0
.
- New date pattern-helpers:
inDay/s
,inMonth/s
,inYear/s
Usage:
let date = new Date(2021, 0, 1, 12, 0, 0)
match(date)(
when(inDay(new Date(2021, 0, 1)), 'January 1st 2021'),
when(inDay(new Date(2021, 0, 2)), 'January 2nd 2021')
)
Use a 1-based month index by passing an array:
let date = new Date(2021, 0, 1, 12, 0, 0)
match(date)(
when(inDay([2021, 1, 1]), 'January 1st 2021'),
when(inDay([2021, 1, 2]), 'January 2nd 2021')
)
The plural helpers accept arrays of date-objects and/or 1-based month-index arrays:
let date = new Date(2021, 0, 1, 12, 0, 0)
match(date)(
when(inDays([[2021, 1, 1], new Date(2021, 0, 2)]), 'January 1st or 2nd 2021')
)
- Test for browser build exports
- Fix browser build
- Fix browser build
- Typedefs + JSDoc for
getIterationLimit
andsetIterationLimit
- Fix browser build. The following now exist on the
matchiz
object:firstOf
,lastOf
,some
,every
,isStrictly
.
-
Now matches on
arguments
keyword -
Now matches
Map
,Set
, and iterators. See PR for more details.
- Fix browser build
eq
+deepEq
for 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
isStrictly
typedef
isStrictly
exact-match pattern-helper
- Implement basic generics into the typedefs
- Remove deprecated form of
anyOf()
from JSDoc - Use uncurried form of
when()
in JSDoc
when()
typedef overloads were not working properly
- Simplify README, link to Wiki for further docs
isBefore()
/isAfter()
date matchers:
match(new Date())(
when(isBefore([2001, 1, 1]))('Before midnight January 1st, 2001'),
when(isAfter([1970, 5]))('After May 1970 (ie; June onwards)'),
when(isBefore([1999]))('Before 1999 (ie; up to and including 1998)'),
when(isAfter(new Date(2001, 0, 1)))('After midnight January 1st, 2001')
)
- Add missing typedefs
- Add missing exports to the browser-build
every()
/some()
for matching patterns in array haystacks:
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')
)
- Remove
browser
setting frompackage.json
; seems to cause issues with CodeSandbox build process, and isn't really necessary since the browser-build is something that's interacted with manually.
-
inTheFuture
alias forinTheNext
-
inTheFuture
+inThePast
can be used without arguments now
- README tweaks, typos
firstOf()
andlastOf()
matchers:
match([1, 'a', 3, 4, 5, 6])(
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'
})
)
- Support
when
guards, which are essentially just consecutive patterns:
match(haystack)(
when(isNumber, gt(5), lte(10), num => {
return 'a number between 6 and 10'
}),
when(isString, startsWith('a'), str => {
return 'a string starting with "a"'
}),
when(isArray, lastOf(isNumber, isNumber), arr => {
return 'the last two elements of the array are numbers'
})
)
-
isTime
for testing milliseconds since Unix epoch -
New
match-iz/dates
methods for testing time-frames relative to now:
match(date)(
// Test if a date is in the past
when(inThePast(1, 'day'))(...),
when(inThePast(2, 'days'))(...),
// Test if a date is in the future
when(inTheNext(30, 'minutes'))(...),
)
- Old
isDate
/isTime
typedefs were left behind
-
Remove
isDate
/isTime
.isDate
conflicted with the same function intypes
, and could not have matchers passed into it in the same way as the individual helpers. ie;isDate(inRange(), anyOf(), ...)
was not possible. -
Removed deprecated
when
OR syntax that abuses arrays. UseanyOf
matcher instead.
// Deprecated syntax:
when([allOf(isFri, isHour(gte(17)), isSat, isSun)])
// ^__ _ _ _ _ __^
// Refactored using `anyOf`:
when(anyOf(allOf(isFri, isHour(gte(17))), isSat, isSun))
// ^^^^^^__ _ _ _ _ __^
when
now accepts a result/handler as its second argument:
// New syntax:
when(literalOrPattern, resultOrHandler)
match(new Date())(
when(allOf(nthSun(-1), isMar), dateObj => {
return 'Last Sunday of March: Clocks go forward'
}),
when(anyOf(allOf(isFri, isHour(gte(17))), isSat, isSun), () => {
return 'Ladies and Gentlemen; The Weekend'
}),
otherwise('The clock is ticking')
)
// Existing syntax (still works):
when(literalOrPattern)(resultOrHandler)
match(new Date())(
when(allOf(nthSun(-1), isMar))(dateObj => {
return 'Last Sunday of March: Clocks go forward'
}),
when(anyOf(allOf(isFri, isHour(gte(17))), isSat, isSun))(() => {
return 'Ladies and Gentlemen; The Weekend'
}),
otherwise('The clock is ticking')
)
- README typos, fixes
- Simple date matchers now available in
match-iz/dates
andmatch-iz/dates/utc
- Permit use of anyOf() as standalone function instead of just match-iz pattern, eg:
// Before 2.2.0
const isStringOrRegExp = anyOf(isString, isRegExp)
// [isString, isRegExp]
// After 2.2.0
const isStringOrRegExp = anyOf(isString, isRegExp)
// <Function>
isStringOrRegExp('foo') // true
isStringOrRegExp(/foo/) // true
- inRange() will work with min/max swapped
- README typo
- README cleanup
- README tweaks, update deps
- Include package.json for sub-modules.
- Another run at fixing import vs. require.
- Fix .d.ts for allOf, anyOf, includedIn, hasOwn
- Added isDate() to .d.ts
- If specified, named RegExp-groups will be passed-in to
when
handlers as the first argument, and the full-match the second.
Before v2:
when(/(?<foo>.*)/)(fullMatch => {
const foo = fullMatch.groups.foo
console.log(foo)
})
After v2:
when(/(?<foo>.*)/)(({ foo }, fullMatch) => {
console.log(foo)
})
- RegExps without named-groups work as before, with the full-match being the first and only argument to the handler.
- cata() helper for integration with ADTs/monads
- Fix esbuild errors by re-ordering package.json exports
- esbuild output was different after deps-update for the browser-build
- isDate
- Fix WebPack error: "Default condition should be last one"
- Above-the-fold README tweaks
- Whoops, README example condition for >=500 will not run
- README example tweaks
- pluck(predicate?) helper to extract a part of the haystack
- instanceOf(constructor) helper
- hasOwn(prop1, prop2...) helper
- isNumber(NaN) should return false
- Tweak README
- Mild code shrinking
- allOf(), anyOf(), includedIn()
- Minor cleanup
not()
type definition
not()
can be used with literals as well as functions
not()
will negate the output of a function
- README: Compact full examples into details/summary tags
- We're exposing CJS as well as ESM, so use .mjs instead of "type": "module" for build/source files. Fixes CJS imports
- Use ESM for source/build files
- Fix ESM build (import { ... } from 'match-iz' was not working right)
- Will do the right thing with
NaN
- Can handle sub-arrays as well as sub-pojos
- When matching array-to-array, ensure exact lengths before comparing contents to make matches more predictable. Comparing variable length arrays is done better via a custom when-predicate
- Can now match against contents of an array
- Add runtime type checkers to index.d.ts
- Export runtime type checkers, why not
- NPM tags, README tweaks, no functional changes
- Remove @params / @returns from JSDoc comments - fixes duplicate Intellisense suggestions
index.d.ts
against() example
- Basic
index.d.ts
for type hints and examples
- otherwise() was returning all falsy values as
undefined
- Updated test for nested patterns
- Remove superfluous coercion
- Support nested objects for patterns
- unpkg example in README
- Literals already cover use-case for isTrue/isFalse
- README: Above the fold examples
- inRange() should check that value isNumber, not min/max
- empty() should not include 'false'
- Provide truthy/falsy/isTrue/isFalse
- Update README to include correct empty/defined usage
- defined, empty, spead()
- More complete empty()
- Number.isNumber() is not a thing
- RegExps guard for strings
- spread() should not mutate its argument
- Browser-build global now
matchiz
, notmatches
- match-iz :)