CARVIEW |
Select Language
HTTP/2 200
date: Wed, 23 Jul 2025 13:53:48 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
x-robots-tag: none
etag: W/"670a6475c4e2c0841cfcefd7eaf52db5"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=4IY1Kbiw%2BIlWDeCsPq8HtqoMhFdGlC9TpeVc8TPKud5sHgh3fZXFA6n%2F5jpqHimRq7t6rLXpF9zwlH3SaHzyXWjRZgVGks3RRQRYcymgHAmVWg%2BqHZnlTbVO0Rrr4rXPsVMMCS2Dpok5KBACk1uCfCNy42pt%2FJJqzYYOL%2FG9fkPHZKqodWi2osfq47EA2sAtPbmHI%2BPgSolt%2BPQLXfNUlATWM2NU1ppyaUoTqgnRrQ%2FhRMZrZBi%2FOmEW91vXqpQC759H8UM%2FihR%2FQuXrt08Zow%3D%3D--vBEbG%2BeQWFhMg%2BOi--F2gT4%2B0K%2BSTJ7eLP2DV%2BVA%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.291398349.1753278828; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 13:53:48 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 13:53:48 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: A0E4:124FF4:CAC5F7:F07027:6880E96C
Some Examples · shuckster/match-iz Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 2
Some Examples
Conan edited this page Oct 11, 2022
·
6 revisions
In addition to the examples below, I've used match-iz
myself in the following libraries: sift-r and viddy.
match(props)(
when({ loading: defined }, <Loading />),
when({ error: defined }, <Error {...props} />),
when({ data: defined }, <Page {...props} />),
otherwise(<Logout />)
)
// <Loading />
Full example
import * as matchiz from 'match-iz'
const { match, when, otherwise } = matchiz
const { spread, defined } = matchiz
function AccountPage(props) {
const { loading, error, data } = spread(defined)
return match(props)(
when({ loading }, <Loading />),
when({ error }, <Error {...props} />),
when({ data }, <Page {...props} />),
otherwise(<Logout />)
)
}
What is spread()?
match(action)(
when({ type: 'add-todo', payload: pluck(isString) }, (payload) => ({
...state,
todos: [...state.todos, { text: payload, completed: false }],
})),
otherwise(state)
)
What is pluck()?
Full example
import { match, when, otherwise, pluck as $ } from 'match-iz'
const todosReducer = (state, action) =>
match(action)(
when({ type: 'set-visibility-filter', payload: $() }, (visFilter) => ({
...state,
visFilter,
})),
when({ type: 'add-todo', payload: $() }, (text) => ({
...state,
todos: [...state.todos, { text, completed: false }],
})),
when({ type: 'toggle-todo', payload: $() }, (index) => ({
...state,
todos: state.todos.map((todo, i) =>
match(i)(
when(index)({ ...todo, completed: !todo.completed }),
otherwise(todo)
)
),
})),
otherwise(state)
)
What is pluck()?
match(new Date())(
when(allOf(nthSun(-1), isMar), (dateObj) => {
return 'Last Sunday of March: Clocks go forward'
}),
when(anyOf(isSat, isSun), () => {
return 'Ladies and Gentlemen; The Weekend'
}),
otherwise('The clock is ticking')
)
Full example
import { match, when, otherwise, allOf, inRange } from 'match-iz'
import { nthSun, isMar, isOct, isDay } from 'match-iz/dates'
match(new Date())(
when(allOf(nthSun(-1), isMar), (dateObj) => {
return 'Last Sunday of March: Clocks go forward'
}),
when(allOf(nthSun(-1), isOct), (dateObj) => {
return 'Last Sunday of October: Clocks go back'
}),
when(isDay(1), (dateObj) => {
return 'Pinch punch, first day of the month!'
}),
when(isDay(inRange(30, 31)), (dateObj) => {
return "It's probably not February..."
}),
otherwise('The clock is ticking')
)
match(vector)(
when({ x, y, z }, ({ x, y, z }) => Math.hypot(x, y, z)),
when({ x, y }, ({ x, y }) => Math.hypot(x, y)),
otherwise((vector) => vector.length)
)
// 3.14
Full example
import * as matchiz from 'match-iz'
const { match, when, otherwise } = matchiz
const { spread, defined } = matchiz
function getLength(vector) {
const { x, y, z } = spread(defined)
return match(vector)(
when({ x, y, z }, ({ x, y, z }) => Math.hypot(x, y, z)),
when({ x, y }, ({ x, y }) => Math.hypot(x, y)),
otherwise((vector) => vector.length)
)
}
What is spread()?
match(['', '2', undefined])(
when(['1', _, _], 'one'),
when([_, '2', _, _], 'two, with four items'),
when([_, '2', _], 'two'),
otherwise('nope')
)
// "two"
Full example
import * as matchiz from 'match-iz'
const { match, when, otherwise } = matchiz
const { empty: _ } = matchiz
match(['', '2', undefined])(
when(['1', _, _], 'one'),
when([_, '2', _, _], 'two, with four items'),
when([_, '2', _], 'two'),
otherwise('nope')
)
function* gen() {
yield 1
yield 2
yield 3
}
let res = match(gen())(
// use array-matchers with iterables
// to test emitted values
when([3, 2, 1], 'back'),
when([1, 2, 3], 'forward'),
otherwise('no match')
)
// "forward"
Full example
import * as matchiz from 'match-iz'
const { match, when, otherwise, lastOf } = matchiz
function* range(start, end) {
for (let i = start; i <= end; i++) {
yield i
}
}
let res = match(range(0, 100))(
when([1, 2, 3], () => {
return 'iterable emitted only 3 values: 1, 2, and 3'
}),
when(lastOf(50, 51), (captured) => {
console.log('everything iterated over to get here:', captured)
return captured
}),
otherwise('no match')
)
// [0, 1, 2, ..., 50, 51]
let john = { name: 'John Doe' }
let lily = { name: 'Lily Bush' }
let peter = { name: 'Peter Drucker' }
let userRolesMap = new Map([
[john, 'admin'],
[lily, 'editor'],
[peter, 'subscriber'],
])
match(userRolesMap)(
// Map: when({ key, value }, result/handler )
when(
{
key: { name: includes('Bush') },
value: isString,
},
({ value: role }) => {
return role
}
),
otherwise('sorry!')
)
// "editor"
let userSet = new Set([john, lily, peter])
match(userSet)(
// Set: when( value, result/handler )
when({ name: pluck(includes('Doe')) }, (value) => {
return value
}),
otherwise('sorry!')
)
// "John Doe"
Full example
import * as matchiz from 'match-iz'
const { match, when, otherwise, includes, pluck } = matchiz
let john = { name: 'John Doe' }
let lily = { name: 'Lily Bush' }
let peter = { name: 'Peter Drucker' }
let userRolesMap = new Map([
[john, 'admin'],
[lily, 'editor'],
[peter, 'subscriber'],
])
match(userRolesMap)(
// Map: when({ key, value }, result/handler )
when(
{
key: { name: includes('Bush') },
value: isString,
},
({ value: role }) => {
return role
}
),
otherwise('sorry!')
)
// "editor"
let userSet = new Set([john, lily, peter])
match(userSet)(
// Set: when( value, result/handler )
when({ name: pluck(includes('Doe')) }, (value) => {
return value
}),
otherwise('sorry!')
)
// "John Doe"
lines.filter(
against(
when(/remove-this-one/, false),
when(/and-this-one-too/, false),
when(endsWith('-and-another'), false),
otherwise(true)
)
)
See a few more:
import { against, when, otherwise, lte } from 'match-iz'
function memoize(fn, cache = new Map()) {
return (x) => (cache.has(x) ? cache.get(x) : cache.set(x, fn(x)).get(x))
}
// Fibonnacci
const fib = memoize(
against(
when(lte(0), 0),
when(1, 1),
otherwise((x) => fib(x - 1) + fib(x - 2))
)
)
fib(35)
// Font sizes
const fontSize = memoize(
against(
when([100, 200], 'Super Thin'),
when([300], 'Thin'),
when([400, 500], 'Normal'),
when([600, 700, 800], 'Bold'),
when([900], 'Heavy'),
otherwise('Not valid')
)
)
;[100, 200, 300, 400, 500, 600, 700, 800, 900, 901].forEach((size) => {
console.log(`${size} = `, fontSize(size))
})
import { against, when, otherwise } from 'match-iz'
// Sorting
numbers.sort(
nargs(
against(
when(([a, b]) => a < b, -1),
when(([a, b]) => a === b, 0),
when(([a, b]) => a > b, 1)
)
)
)
function nargs() {
return (fn) =>
(...args) =>
fn(args)
}
match-iz
🔥 | on npm | docs home | top of page
Clone this wiki locally
You can’t perform that action at this time.