Object Pattern Matching
npm install ptnmptnm> Object Pattern Matching
- zero production dependencies
- tiny code footprint
- production battle-tested in Pear
ptnm matches a pattern object against a message object.
A match succeeds when all enumerable own properties in the pattern exist in the message
and are strictly equal, recursively.
Extra properties in the message are ignored.
- Non-coercive matching (===)
- Own properties only (non-enumerable/inherited pattern keys ignored)
- No cycle detection by design - do not pass circular objects
``js`
const match = require('ptnm')
`js`
import match from 'ptnm'
`js`
match({ a: 1, b: 2 }, { a: 1 }) // true
`js`
match(
{ a: { b: 1, c: 2 } },
{ a: { b: 1 } }
)
// true
`js
match(
{ a: 1 },
{ a: 2 }
)
// false
match(
{},
{ a: 1 }
)
// false
``
Apache-2.0