@jswork/match-first
v1.0.2
Published
Return the first value whose condition evaluates to true.
Readme
installation
yarn add @jswork/match-firstusage
import matchFirst from '@jswork/match-first';
// Basic example: return first matching value
const result = matchFirst(
[
{ condition: isHotfix, value: `hotfix ${id}` },
{ condition: taskType === 'bug', value: `bugfix ${id}` }
],
`start ${id}`
);
// Conditional branching with fallback
const size = matchFirst(
[
{ condition: x > 10, value: 'large' },
{ condition: x > 5, value: 'medium' }
],
'small'
);
// Different value types
const config = matchFirst(
[
{ condition: env === 'prod', value: { apiUrl: 'https://api.prod.com' } },
{ condition: env === 'staging', value: { apiUrl: 'https://api.staging.com' } }
],
{ apiUrl: 'http://localhost:3000' }
);license
Code released under the MIT license.
