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

wildcard-utils

v2.2.3

Published

A small, flow-covered, zero-dependency package to work with wildcard (*) values by matching, searching, and filtering values that match a given wildcard pattern. (Works in Browser)

Downloads

3,908

Readme

wildcard-utils

A small, flow-covered, zero-dependency package to work with wildcard (*) values by matching, searching, and filtering values that match a given wildcard pattern.

Values can be string, Array<string>, Set<string>, { [key: string]: any }. Support for Map is also planned in the future.

Reverse filtering is also available to search the above types as patterns instead of as matches (see examples).

BROWSER COMPATIBLE!


Installation

yarn add wildcard-utils

or

npm install --save wildcard-utils

Flow Coverage

Proudly built with 100% Flow Coverage and exported .flow.js files so your flow projects will benefit!

We strongly recommend you look over the types in the source. This will give you an idea of how the various pieces of the package work.


Examples

This package provides extreme flexibility for searching values for wildcard matches. While the example below is simple, you are encouraged to take a look at the examples folders for examples of the more advanced functionality that is available.

Simple String Example

A simple example using simple string matching against a given wildcard pattern.

import { Wildcard } from 'wildcard-utils';

const system_wildcard = new Wildcard().case(false).pattern('system*');

const isSystemType = type => system_wildcard.match(type);

isSystemType('SYSTEM_OFFLINE');
// true

isSystemType('NETWORK_OFFLINE');
// false

More Examples

More examples can be seen and tested by checking out the examples folders


Exports

There are two ways you can use this package. If you simply wish to use the simplistic pattern generator that is used to build the RegExp values, you can import from wildcard-utils/to-pattern. For the full-featured version, import the Wildcard class directly.

Common Flow Types

export type Wildcard$RegExpFlags = $CharSet<'gimsuy'>;

export type Wildcard$ToPatternTypes =
  | string
  | Array<string>
  | Set<string>
  | { [key: string]: * };

export type Wildcard$Config = {
  logic: 'and' | 'or',
  flags: Wildcard$RegExpFlags,
};

RegExp Generator

declare function toWildcardPattern(
  patterns: Wildcard$ToPatternTypes,
  config?: $Shape<Wildcard$Config>,
): RegExp;
import toWildcardPattern from 'wildcard-utils/to-pattern';

const pattern = toWildcardPattern(['ONE*TWO*THREE', 'FOUR*FIVE*SIX'], {
  logic: 'or',
  flags: 'i',
});

Wildcard Class

import Wildcard from 'wildcard-utils';
const WC = new Wildcard();

WC.case(false)
  .logic('or')
  .pattern(['foo*', 'bar*', 'baz*qux']);

.pattern()

(pattern: Wildcard$ToPatternTypes, force?: boolean) => Wildcard;

.match()

(data: Wildcard$ToPatternTypes, pattern?: RegExp | Wildcard$ToPatternTypes) =>
  boolean;

.filter()

(data: Wildcard$ToPatternTypes, nomatch: mixed = undefined) =>
  $Matched_DATA_Subset | nomatch;

.search()

This is a reverse filter where the pattern is searched instead of the data

(data: Wildcard$ToPatternTypes, nomatch: mixed = undefined) =>
  $Matched_PATTERN_Subset | nomatch;

.has()

Checks if a Wildcard is present in the given pattern value. If no argument is provided, it checks the last provided value to .pattern().

(pattern?: Wildcard$ToPatternTypes) => boolean;

.logic()

(logic: 'and' | 'or', compile?: boolean) => Wildcard;

.case()

(match: boolean, compile?: boolean) => Wildcard;

.flags()

(flags: Wildcard$RegExpFlags, compile?: boolean) => Wildcard;

.reset()

() => Wildcard;