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

@wider/regexp-gitignore

v1.1.3

Published

Convert gitIgnore files to a Regular Expression

Downloads

4

Readme

@wider/regexp_gitignore - Convert gitIgnore declarations into a regular expression

You provide the list of gitIgnore formatted definition files or JSON objects that define the files of interest

You get back a regular expression that can be used to test a well formed file path string. Returns true if the given string matches the file specification. true means that the file is to be ignored.

Alternatively, instead of a path string, you can test same functionality on a nodeJS.fs.dirent

Documentation

Full Documentation

Install

npm install "@wider/regexp_gitignore"

Use within your project

The following illustrates the use of pattern .gitignore definitions that are in the run time current working directory. Here we are looking for a package.json that is not in the gitignore folders (such as node_modules) and not in your own !jsDoc folder or a files whose names start .myOwn

import path from "path";
import gitIgnore from "@wider/regexp_gitignore";

const source_git = path.join(process.cwd(), "./.gitignore"); //find the standard gitignore file in the directory of the applications  or wherever you may have placed it 
const source_myOwn = ["!jsDoc/", ".myOwn*"]; // add in your own extras - which eg you might pick up from your own `settings.json`
const directions = {exclude : source_git, include : source_myOwn};

const myRegularExpression = gitIgnore(directions);

// 	you can then use this regular expression repeatedly as required
if (myRegularExpression.test(path.resolve("./whatEverYouWant")))
	yourCodeForAFileToBeIgnored;
else
	yourCodeForAFileToBeAccepted

About the directions

The directions as to which files should be accepted or rejected are given as an object with typeDef of directionsToCreateAFilter as seen in the regexp_gitignore API.

You can provide Array objects for both include and exclude directions - if you include both then the exclude are applied first.

If you provide real gitignore files please remember that .gitignore system itself operates with a collection of ignore specifications in different locations within nodeJS and the system as indicated by the gitignore documentation. You need to provide all the file components of relevance to you in the order they are to apply so as to get an identical impact to what git tools would do. In many cases just your main ./.gitignore file alone may suffice.

The files selected by include are not equivalent of the ! operator in .gitignore exclude file. In gitignore the ! operator can override the other directives. Placing the entry instead in the include section will only find files in locations that have not been excluded. You can still use the ! operator in both include and exclude but it may make your brain work overtime working out what you are achieving - it is more efficient to use a positive exclude than a negative include and easier for the next developer to read.

About the conversion

The conversion can give rise to seriously large regular expressions, but they will invariably perform faster than the equivalent code in javascript.

The characters and diphthongs such as * . / ! ** \! and newline are recognised for their gitignore use and newlines and comments # are stripped out.

The tutorial in the jsDoc documentation in this package includes examples.

Limitation

This is a regular expression parsing of the file's full path. Paths provided via nodeJS path.resolve() have trailing directory delimiter stripped off and so it is not possible for this tool to detect if a path you provide is intended to be a directory. If you need to detect if the reference is a directory, please use instead

import {GipDirent} from "@wider/regexp_gitignore";

const myGip = new GipDirent(sameObjectAsRegexp_gitignore);

if (myGip.test(dirEntOrFullFilePath))
	yourCodeForAFileToBeIgnored;