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

match-test-replace

v1.4.0

Published

Easy pattern match and replace text.

Downloads

2,289

Readme

match-test-replace

Easy text pattern match and replace text.

This library does Match -> Test -> Replace pattern.

  1. Match pattern
  2. Does test
  3. Does replace

Install

Install with npm:

npm install match-test-replace

Usage

export interface PatternMatchDictArgs {
    index: number;
    match: string;
    captures: string[];
    all: string;
}
export interface TestMatchReplaceReturnDict {
    pattern: RegExp;
    replace: (args: PatternMatchDictArgs) => string;
    test?: (args: PatternMatchDictArgs) => boolean;
    message?: (args: PatternMatchDictArgs) => string;
}
export interface TestMatchReplaceReturnResult {
    index: number;
    match: string;
    replace: string;
    message?: string;
}
export interface TestMatchReplaceReturn {
    ok: boolean;
    results: TestMatchReplaceReturnResult[];
}
/**
 * replace `text` with `results`.
 */
export declare const replaceAll: (
    text: string,
    results: TestMatchReplaceReturnResult[]
) => {
    ok: boolean;
    output: string;
};
/**
 * test `text`, match `text`, and replace `text.
 */
export declare const testMatchReplace: (text: string, dict: TestMatchReplaceReturnDict) => TestMatchReplaceReturn;

Match -> Replace

import { replaceAll, matchTestReplace } from "match-test-replace";
const text = "Hello";
const res = matchTestReplace(text, {
    pattern: /hello/i,
    replace: () => "Hello"
});
assert.ok(res.ok, "should be ok: true");
assert.strictEqual(res.results.length, 1, "1 replace");
/**
[ { index: 0, match: 'Hello', replace: 'Hello', message: undefined } ]
*/

Match -> test -> Replace

match-test-replace not replace if test return false

import { replaceAll, matchTestReplace } from "match-test-replace";
const text = "webkit is matched,but node-webkit is not match";
const res = matchTestReplace(text, {
    pattern: /(\S*?)webkit/g,
    replace: () => "WebKit",
    test: ({ captures }) => {
        return captures[0] !== "node-";
    }
});
assert.ok(res.ok === true, "should be ok: false");
assert.strictEqual(res.results.length, 1, "no replace");
assert.strictEqual(replaceAll(text, res.results).output, "WebKit is matched,but node-webkit is not match");

Complex

import * as assert from "assert";
import { replaceAll, matchTestReplace } from "match-test-replace";
import { PatternMatcher } from "nlcst-pattern-match";
import { EnglishParser } from "nlcst-parse-english";
const englishParser = new EnglishParser();
const matcher = new PatternMatcher({ parser: englishParser });
// https://developers.google.com/style/clause-order
// NG: Click Delete if you want to delete the entire document.
// OK: To delete the entire document, click Delete.
const text = 'Click Delete if you want to delete the entire document.';
const res = matchTestReplace(text, {
    pattern: /Click (\w+) if you want to (.+)./,
    replace: ({ captures }) => {
        console.log(captures);
        return `To ${captures[1]}, click ${captures[0]}.`
    },
    test: ({ all }) => {
        const pattern = matcher.tag`Click ${{
            type: "WordNode",
            data: {
                // Verb
                pos: /^VB/
            }
        }}`;
        return matcher.test(all, pattern);
    }
});
assert.ok(res.ok === true, "should be ok: true");
const output = replaceAll(text, res.results).output;
assert.strictEqual(output, "To delete the entire document, click Delete.");

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm i -d && npm test

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