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

sensitive-phrases

v1.0.0

Published

Find and mask sensitive phrases in a sentence.

Downloads

17

Readme

Sensitive Phrases

Find and mask sensitive phrases in a sentence.

Unlike other silly tools just blindly warn about sensitive words and/or delete them, this module can brilliantly point out which and where the sensitive phrases are, and bypass some phrases that should not be considered sensitive. At the mean time, this module provides a straight-forward and yet customizable mask function that help you handle any sensitive phrases found.

Install

npm i sensitive-phrases

Example

NOTE: the following example uses Chinese language.

import * as assert from "assert";
import { find, mask } from "sensitive-phrases";

let patterns = [
    "草泥马",
    "!(一)?(头|只)草泥马", // a pattern starts with `!` indicates it should be bypassed
    "操(尼玛|你妈)?",
    "!(体|广播)操",
    "!操(练|场)"
];
let tokens = find(
    "操,草泥马,心中有千万只草泥马奔腾而过,你只能在操场操练广播操或体操",
    patterns
);

assert.deepStrictEqual(tokens, [
    { phrase: "操", index: 0 },
    { phrase: "草泥马", index: 2 }
]);

let result = mask(sentence, tokens);

assert.strictEqual(
    result,
    "*,***,心中有千万只草泥马奔腾而过,你只能在操场操练广播操或体操"
);

// Use a pair of HTML tag to highlight the sensitive patterns.
result = mask(sentence, tokens, "<s></s>");

assert.strictEqual(
    result,
    "<s>操</s>,<s>草泥马</s>,心中有千万只草泥马奔腾而过,你只能在操场操练广播操或体操"
);

NOTE: for more examples, check the test cases.

API

Before listing all the functions one by one, take a look at the type declarations:

export type Token = { phrase: string; index: number; };
export type Finder = (sentence: string) => Token[];

export function find(sentence: string, patterns: string[]): Token[];
export function mask(
    sentence: string,
    tokens: Token[],
    style?: "*" | "*-" | "-*" | "-*-" | "*-*"
): string;
export function mask(sentence: string, tokens: Token[], tag: string): string;

export function createFinder(patterns: string[]): Finder;
export function createFinderFromFile(filename: string): Promise<Finder>;

find()

This function finds and lists all sensitive phrases in a sentence. Instead of using plain words, this module uses patterns that is compatible with and will eventually be concatenated and compiled to regular expressions.

There are two types of patterns supported by this module, one indicates sensitive, and the other for bypass. Any pattern that starts with ! will not be considered sensitive, and the find function has a special method to deal with conflicts under the hood.

mask()

This function masks the sensitive phrases found by the find() function, and it supports multiple styles:

  • * Masks the whole phrase with asterisks (*), this is the default style.
  • *- Masks the head of the phrase and leave along the tail.
  • -* Masks the tail of the phrase and leave along the head.
  • -*- Masks the central part of the phrase and leave along the head and tail.
  • *-* Masks the head and tail of the phrase and leave along the central part.

Other than styles, this function also accepts a pair of XML/HTML tag (with or without attributes) that will wrap and highlight the sensitive phrases.

createFinder()

This function creates a static find function with the given patterns, so that the program won't have to compile the patterns every time.

createFinderFromFile()

Similar to createFinder(), but it loads the patterns from a file. In Node.js, it uses fs.readFile() to read the file, and in browsers (YES this module fully supports browsers), it uses XMLHttpRequest to load internet files.

Also, this function supports comments start with # in the file, and they will not be used as patterns.