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

@unly/conditions-matcher

v2.0.1

Published

Compares a given context with a filter (a set of conditions) and resolves whether the context checks the filter. Strongly inspired by [GraphQL filters](https://www.prisma.io/docs/reference/prisma-api/queries-ahwee4zaey#filtering-by-field).

Downloads

11

Readme

Maintainability Test Coverage DeepScan grade Build Status

Conditions-matcher

Compares a given context with a filter (a set of conditions) and resolves whether the context checks the filter. Strongly inspired by GraphQL filters.

Use cases are:

  • Compare a given context with a set or conditions, to see if it matches those conditions
  • We use it at Unly to resolve which financing solutions apply to a student, depending on that student's situation

The biggest strengths of this plugin are:

  • Flexibility: You compose your own filter, by defining conditions within it that depends on your business logic.
    • You have access to a wide range of conditional operators (contains, endsWith, greaterThan, etc.).
    • You have access to the same logical operators as those available in GraphQL (AND, OR, NOT).
    • You can nest those operators within themselves, without limit.
  • Robustness: There is an important test suite (unit tests) written to make sure to detect regressions and only ship well-tested and quality software. We also follow Semantic Versioning.

This package was developed after opening a StackOverflow question, we were disappointed not to find an existing implementation and therefore built our own.

Getting started

Installation

Using npm :

npm i @unly/conditions-matcher

Using yarn :

yarn add @unly/conditions-matcher

Usage

Import in you project

ES5

const contextMatcher = require("@unly/conditions-matcher");

ES6

import contextMatcher from "@unly/conditions-matcher";

See the examples for more details, clone the project to play around with those. Then please check the conditional operators documentation here

Function prototype

const result: boolean = contextMatcher(filter, context, options);

filter: Contains all the conditions that constitute the said filter. For a complete list of conditions and operators, see the documentation. Example :

const filter = {
  AND: [
    { "school_gpa__lessThan": 3 },
    { "school_name__contains": "business" },
    { "foo__eq": "bar" },
  ],
};

context: Is an object containing the data (context) to match against the filter, to know whether it passes the conditions or not. Example :

const context = {
  'school': {
      name: 'Awesome business school',
      gpa: 2.5
  },
};

options: Optional configuration object.

This example will return the following in result:

{
  status: true, // Means the check passed, the context therefore passes the filter's conditions
  ignoredConditions: [ // Contains conditions that were ignored due to a lack of data in `context`
    {
      status: null, // "null" because no check could be performed (lack of data)
      rule: 'foo__eq',
      conditionalOperator: 'eq',
      path: 'foo',
      valueInContext: undefined,
      reason: 'Error: path: foo is not defined in context' // Human friendly error message, easier to reason about
    }
  ],
}

Documentation

Options

See defaultOptions.

| Option name | Default value | Description | |--------------|---------------|--------------| | strictMatch | false | This represents the behavior in case of non-existent context corresponds to the filter. Set to true if you want to return false in case of a value in the filter doesn't exist. |


Contributing

We gladly accept PRs, but please open an issue first so we can discuss it beforehand.

Working locally

yarn start # Shortcut - Runs linter + build + tests in concurrent mode (watch mode)

OR run each process separately for finer control

yarn lint
yarn build
yarn test

Test

yarn test # Run all tests, interactive and watch mode
yarn test:once # Used for CI/CD
yarn test:coverage # Generate coverage report

Versions

SemVer

We use Semantic Versioning for this project: https://semver.org/. (vMAJOR.MINOR.PATCH: v1.0.1)

  • Major version: Must be changed when Breaking Changes are made (public API isn't backward compatible).
    • A function has been renamed/removed from the public API
    • Something has changed that will cause the app to behave differently with the same configuration
  • Minor version: Must be changed when a new feature is added or updated (without breaking change nor behavioral change)
  • Patch version: Must be changed when any change is made that isn't either Major nor Minor. (Misc, doc, etc.)

Release a new version

Note: You should write the CHANGELOG.md doc before releasing the version. This way, it'll be included in the same commit as the built files and version update

Then, release a new version:

  • yarn run release

This command will prompt you for the version to update to, create a git tag, build the files and commit/push everything automatically.

Don't forget we are using SemVer, please follow our SemVer rules.

Pro hint: use beta tag if you're in a work-in-progress (or unsure) to avoid releasing WIP versions that looks legit

Releasing and publishing

yarn releaseAndPublish # Shortcut - Will prompt for bump version, commit, create git tag, push commit/tag and publish to NPM

yarn release # Will prompt for bump version, commit, create git tag, push commit/tag
npm publish # Will publish to NPM

License

MIT