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

@quanxiaoxiao/compare

v0.2.2

Published

Use expressions similar to MongoDB query statements to detect whether the data type of an `object` matches the expression.

Downloads

12

Readme

Use expressions similar to MongoDB query statements to detect whether the data type of an object matches the expression.

Install

npm install @quanxiaoxiao/compare

Quick Start

import compare from '@quanxiaoxiao/compare';

compare({ name: 'quan' })({ name: 'quan', age: 22 }) === true
compare({ age: { $gt: 20 } })({ name: 'quan', age: 22 }) === true
compare({ good: false })({ name: 'quan', age: 22, good: false }) === true
compare({ big: null })({ name: 'quan', age: 22, good: false }) === true
compare({ 'obj.name': 'quan' })({ obj: { name: 'quan' } }) === true

Expression Type

  • Object
  • Array

Expression Operators

Number Comparison Expression Operators

| Name | Type | | ---- | ------------------------------------- | | $eq | string, null, boolean, number | | $ne | string, null, boolean, number | | $gt | number | | $gte | number | | $lt | number | | $lte | number |

$eq

compare({ name: { $eq: 'quan' } })({ name: 'quan' }) === true
compare({ age: { $eq: null } })({ name: 'quan' }) === true
compare({ 'obj.name': { $eq: 'quan' } })({ obj: { name: 'quan' } }) === true

$ne

compare({ name: { $ne: 'quan' } })({ name: 'rice' }) === true
compare({ age: { $ne: null } })({ name: 'quan', age: 33 }) === true

$gt

compare({ age: { $gt: 22 } })({ age: 23 }) === true

$gte

compare({ age: { $gte: 22 } })({ age: 22 }) === true
compare({ age: { $gte: 22 } })({ age: 23 }) === true

$lt

compare({ age: { $lt: 22 } })({ age: 21 }) === true

$lte

compare({ age: { $lte: 22 } })({ age: 21 }) === true
compare({ age: { $lte: 22 } })({ age: 22 }) === true

Array Expression Operators

| Name | Type | | ---- | -------------------------------------------- | | $in | Array<string, null, boolean, number> | | $nin | Array<string, null, boolean, number> |

$in

compare({ age: { $in: [22, 23, 28] } })({ age: 22 }) === true
compare({ name: { $in: ['', null] } })({ age: 22 }) === true
compare({ name: { $in: ['', null] } })({ age: 22, name: '' }) === true

$nin

compare({ age: { $nin: [22, 23, 28] } })({ age: 24 }) === true
compare({ name: { $nin: ['', null] } })({ age: 22, name: 'aaa' }) === true

Eegexp Expression Operators

| Name | Type | | ------ | ------------------------- | | $regex | string, Array<string> |

$regex

compare({ name: { $regex: '^a(1|3|z)c' } })({ name: 'a3c' }) === true
compare({ name: { $regex: '^a(1|3|z)c' } })({ name: 'azc' }) === true
compare({ name: { $regex: ['^a(1|3|z)c', 'i'] } })({ name: 'Azc' }) === true

Boolean Expression Operators

| Name | Type | | ---- | ------------------------------------------------------------------------------ | | $not | Array<Number comparison Expression, Eegexp Expression, Array Expression> | | $and | Array<Number comparison Expression, Eegexp Expression, Array Expression> | | $or | Array<Number comparison Expression, Eegexp Expression, Array Expression> | | $nor | Array<Number comparison Expression, Eegexp Expression, Array Expression> |

$not

express = {
  $not: [
     { $eq: 33 },
     { $eq: 44 },
  ]
}; // age !== 33 && age !== 44

compare(express)({ age: 34 }) === true
express = {
  $not: [
     { $lt: 33 },
     { $gt: 44 },
  ]
};  // !(age < 33) && !(age > 44)

compare(express)({ age: 34 }) === true

$and

express = {
  $and: [
     { $gt: 33 },
     { $lt: 44 },
  ]
};  // age > 33 && age < 44

compare(express)({ age: 34 }) === true

$or

express = {
  $or: [
     { $lt: 33 },
     { $gt: 44 },
  ]
}; // age < 33 || age > 44

compare(express)({ age: 32 }) === true
compare(express)({ age: 45 }) === true 

Other

const express = [
  { name: 'quan' },
  { age: { $gt: 22 } },
]; // name === 'quan' || age > 22

compare(express)({ age: 32 }) === true
compare(express)({ name: 'quan' }) === true
const express = {
  method: 'GET',
  'headers.host': {
    $or: [
      {
        $regex: '\\baaa\\.com\\b',
      },
      {
        $regex: '\\bccc\\.net\\b',
      },
    ],
  },
};

compare(express)({ method: 'GET', headers: { host: 'www.aaa.com' } }) === true
compare({ 'obj1.age': ['obj2.age', '$gt'] })({
  obj1: { age: 22 },
  obj2: { age: 33 },
}) === true  // obj2.age > obj1.age