
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
match-test-replace
Advanced tools
Easy text pattern match and replace text.
This library does Match -> Test -> Replace pattern.
pattern
test
replace
Install with npm:
npm install match-test-replace
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;
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 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");
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.");
See Releases page.
Install devDependencies and Run npm test
:
npm i -d && npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT © azu
FAQs
Easy pattern match and replace text.
The npm package match-test-replace receives a total of 115 weekly downloads. As such, match-test-replace popularity was classified as not popular.
We found that match-test-replace demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.