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

@textlint/regexp-string-matcher

v2.0.2

Published

Regexp-like string matcher.

Downloads

235,397

Readme

@textlint/regexp-string-matcher Actions Status: test

Regexp-like string matcher library.

Install

Install with npm:

npm install @textlint/regexp-string-matcher

Usage

Interface:

export interface matchPatternResult {
    match: string;
    startIndex: number;
    endIndex: number;
}
/**
 * Match regExpLikeStrings and return matchPatternResults
 * @param text target text
 * @param regExpLikeStrings an array of pattern string
 */
export declare const matchPatterns: (text: string, regExpLikeStrings: string[]) => matchPatternResult[];

Example:

import { matchPatterns } from "@textlint/regexp-string-matcher";
const inputText = `
GitHub is a web-based hosting service for version control using git.
It is mostly used for computer code.
GitHub launched in 2018-04-10.`;
// RegExp like strings
const inputPatterns = [
    "git", // => /git/g
    "/github/i", // => /github/ig
    "/(\\d{4})-(\\d{2})-(\\d{2})/" // => /\d{4}-\d{2}-\d{2}/g
];

const results = matchPatterns(inputText, inputPatterns);
assert.deepStrictEqual(results, [
    { match: "GitHub", startIndex: 1, endIndex: 7, captures: [] },
    { match: "git", startIndex: 65, endIndex: 68, captures: [] },
    { match: "GitHub", startIndex: 107, endIndex: 113, captures: [] },
    { match: "2018-04-10", startIndex: 126, endIndex: 136, captures: ["2018", "04", "10"] }
]);

RegExp-like String

This library aim to represent RegExp in JSON and use it for ignoring words. g(global) flag and u(unicode) is added by default.

| Input | Ouput | Note | |--------------|---------|--------------------------------------------| | "str" | /str/gu | convert string to regexp with global | | "/str/" | /str/gu | | | "/str/g" | /str/gu | Duplicated g is just ignored | | "/str/i" | /str/igu | | | "/str/u" | /str/ug | | | "/str/m" | /str/mgu | | | "/str/y" | /str/ygu | | | --- | --- | --- | | "/\\d+/" | /\d+/gu | You should escape meta character like \d | | "/(\\d+)/" | /\d+/gu | You can use capture |

:warning: You should escape meta character like \d in RegExp-like string.

For example, If you want to write \w(any word) in RegExp-like string, you should escape \w to \\w.

Text:

This is a pen.

RegExp-like String:

[
    "/a (\\w+)/"
]

Results:

[ { match: 'a pen', startIndex: 8, endIndex: 13, captures: ["pen"] } ]

Examples

string

text:

GitHub is a web-based hosting service for version control using git.
It is mostly used for computer code.
GitHub launched in 2018-04-10.

pattern:

[
  "GitHub"
]

results: 2 hits

**GitHub** is a web-based hosting service for version control using git.
It is mostly used for computer code.
**GitHub** launched in 2018-04-10.

Ignore Case match

text:

GitHub is a web-based hosting service for version control using git.
It is mostly used for computer code.
GitHub launched in 2018-04-10.

pattern:

[
  "/git/i"
]

results:: 3 hits

**Git**Hub is a web-based hosting service for version control using **git**.
It is mostly used for computer code.
**Git**Hub launched in 2018-04-10.

Special character

You should escape special charactor like \d in RegExp-like string.

text:

GitHub is a web-based hosting service for version control using git.
It is mostly used for computer code.
GitHub launched in 2018-04-10.

pattern:

[
  "/\\d{4}-\\d{2}-\\d{2}/"
]

results:: 1 hit

GitHub is a web-based hosting service for version control using git.
It is mostly used for computer code.
GitHub launched in **2018-04-10**.

Multi-line

text:

===START===
1st inline text.
===END===

===START===
2nd inline text.
===END===

pattern:

[
  "/===START===[\\s\\S]*?===END===/m"
]

results:: 2 hits

**===START===
1st inline text.
===END===**

**===START===
2nd inline text.
===END===**

For more details, see test/snapshots

Escape bracket

text:

TODO [Issue #1]: it will be fixed

patterns:

[
  "/TODO \\[Issue #\\d+\\]:/i"
]

:memo: You should escape bracket both. \\[ and \\],

results:

**TODO [Issue #1]:** it will be fixed

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm i -d && npm test

How to add snapshot tests?

  1. Create new dir to ./snapshots/<name>/
  2. Add input.txt and input-patterns.json
  3. Run npm run test:updateSnapshot
  4. You should verify the output results manually
  5. Run npm test and pass it
  6. Commit it

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu