npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

satisfier

v5.4.2

Published

A purposely loose comparison tool.

Downloads

1,102

Readme

satisfier

NPM version NPM downloads

GitHub NodeJS Codecov

Semantic Release

Visual Studio Code

A purposely loose comparison tool.

Version 5 breaking changes

  • Exact check on array
  • No spread on array, use has()/some() or every()
  • undefined now checks against undefined instead of a placeholder for anything. Use anything for the previous behavior.

satisfies(actual, expected)

The simplest way to use satisfier.

import { satisfies } from 'satisfier'

satisfies(1, 1)  // true
satisfies({ a: 1 }, { a: v => v === 1}) // true
satisfies([{ a: { b: 'b' }}], [{ a: { b: v => v === 'b' } }]) // true

Code completion is available to help you quickly creating your expectation.

createSatisfier(expectation)

Each property in expectation can be a value, a RegExp, or a predicate function.

test(actual)

Test actual against expectation.

import { createSatisfier } from 'satisfier'

// these returns true
createSatisfier({ a: 1 }).test({ a: 1, b: 2 })
createSatisfier({ a: /foo/ }).test({ a: 'foo', b: 'boo' })
createSatisfier({ a: n => n === 1 }).test({ a: 1, b, 2 })

// these returns false
createSatisfier({ a: 1 }).test({ a: 2 })
createSatisfier({ a: 1, b: 2 }).test({ a: 1 })
createSatisfier({ a: /boo/ }).test({ a: 'foo' })
createSatisfier({ a: () => false }).test({ a: 1 })

exec(actual)

Check actual against expectation and returns the checking result. If actual meets the criteria, returns null.

import { createSatisfier } from 'satisfier'

// returns undefined
createSatisfier({ a: 1 }).exec({ a: 1, b: 2 })
createSatisfier({ a: /foo/ }).exec({ a: 'foo', b: 'boo' })
createSatisfier({ a: n => n === 1 }).exec({ a: 1, b, 2 })

// returns [{ path: ['a'], expected: 1, actual: 2}]
createSatisfier({ a: 1 }).exec({ a: 2 })

// returns [{ path: ['b'], expected: 2, actual: undefined}]
createSatisfier({ a: 1, b: 2 }).exec({ a: 1 })

// returns [{ path: ['a'], expected: /boo/, actual: 'foo'}]
createSatisfier({ a: /boo/ }).exec({ a: 'foo' })

// returns [{ path: ['a'], expected: 'a => a === 1', actual: 2}]
createSatisfier({ a: a => a === 1 }).exec({ a: 2 })

anything

If anything is used in expectation, it will match anything.

import { anything } from 'satisfier'

createSatisfier(anything).test({})
createSatisfier({ a: anything }).test({})
createSatisfier([anything, 1]).test(['x', 1])

Test against array

There are several ways to test against array:

Using array expectation

When you use an array expectation to test against array, each entry in the expectation will be used to test against the corresponding entry in the array.

You can also skip over entries by putting in anything.

import { createSatisfier } from 'satisfier'

// all true
createSatisfier([anything, 1]).test(['...anything...', 1])
createSatisfier([e => e === anything, 1]).test([anything, 1])

Using predicate expectation

You can test against the array using a predicate function. The predicate function will receive the whole array.

This is useful if you want to check the relationship within the array.

import { createSatisfier } from 'satisfier'

createSatisfier(
  a =>
    Array.isArray(a) &&
    a.length === 2 &&
    a[0] === 1 &&
    a[1] === 2)
  .test([1, 2])

Using primitive and object expectation

When the expectation is a primitive value or an object, it will be used to check against each element in the array.

import { createSatisfier } from 'satisfier'

// true
createSatisfier(1).test([1, 1])
createSatisfier(false).test([false, false])
createSatisfier('a').test(['a', 'a'])
createSatisfier({ a: e => typeof e === 'string' })
  .test([{ a: 'a' }, { a: 'b' }]))

Build in predicates

There are a few predicates shipped in the package for convenience. They all support tersify. This means if you use tersify to print the predicate (e.g. for logging purpose), you will get a terse string representing the predicates.

For example:

import { createSatisfier, isInRange } from 'satisfier'

const results = createSatisfier(isInRange(1, 3)).exec(0)

// prints '[1...3]'
results[0].expected.tersify()
// { path: [], expected: [1...3], actual: 0 }
tersify(results[0])

Examples of predicate: every, has, isInInterval, isInRange, isTypeOf, none, some, startsWith

Contribute

# after fork and clone
npm install

# begin making changes
git checkout -b <branch>
npm run watch

# after making change(s)
git commit -m "<commit message>"
git push

# create PR