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

highlight-mistakes

v1.0.0

Published

Highlight mistakes and their absence in a piece of text.

Downloads

86

Readme

highlight-mistakes

Highlight mistakes and their absence in a piece of text.

NPM Version Downloads Stats

Table of Contents

  1. Installation
  2. Why?
  3. Usage
    1. Basic Example
  4. Correctness

Installation

npm install --save highlight-mistakes

I would recommend TypeScript or some other setup that provides auto-completion and type checking.

Why?

highlight-mistakes highlights mistakes and their absence in a piece of text based on a given specification. It is intended as a complementary tool for authoring and proof-reading written text.

Usage

highlight-mistakes exports two functions: proofreadWith and highlightWith. To use proofreadWith, you have to define these things:

  • A list of rules describing what should be marked as a mistake and what should be verified as correct.
  • A function that highlights a single mistake and one for non-mistakes.

highlightWith can be used to further process the output of proofreadWith, e.g. to make non-breaking spaces neither marked as correct nor incorrect visible to a human editor.

Basic Example

The canonical example is trying to enforce correct usage of non-breaking spaces and/or non-breaking hyphens, since those are difficult or impossible to tell from their breaking counterparts by visual inspection.

Here, we want non-breaking spaces between numbers and units (just a handful for illustration purposes) – e.g. "144 Hz" or "1 TB" – as well as in phrases like "Core i7". We also want non-breaking hyphens in "G‑Sync" and phrases like "i7‑8700K". (The text in question is assumed to be in HTML, hence &nbsp;. That's also why we use <pre>; otherwise a highlighted space might not be visible at all.)

import { proofreadWith, simpleRule } from "highlight-mistakes";

const DESC_NBSP = {
    bad: " ",
    good: "&nbsp;",
    info: "NBSP",
};

const DESC_NBH = {
    bad: "-",
    good: "‑",
    info: "NBH",
};

const RULES_NB_SPACE = [
    simpleRule(DESC_NBSP)(/\d+/, /[nµmcdkMGTP]?(?:Hz|b|bit|B|byte)\b/),
    simpleRule(DESC_NBSP)(/Core/, /i\d/),
];

const RULES_NB_HYPHEN = [
    simpleRule(DESC_NBH)(/G/, /Sync/),
    simpleRule(DESC_NBH)(/i\d/, /\d{4}/),
];

function markAs(className: string): (info: string | null) => (s: string) => string {
    return _ => s => `<pre class="${className}">${s}</pre>`;
}

const proofread = proofreadWith({
    rules: RULES_NB_SPACE.concat(RULES_NB_HYPHEN),
    markMistake: markAs("mistake"),
    markVerified: markAs("verified"),
});

console.log(proofread("G-Sync is from Nvidia, the up to 4.7&nbsp;GHz Core i7-8700K is from Intel, and 240 Hz monitors are great for gaming."));

In this example, the hyphens in G-Sync and i7-8700K and the spaces in Core i7 and 240 Hz are highlighted as mistakes, whereas the non-breaking space in 4.7&nbsp;GHz is highlighted as verified.

Correctness

highlight-mistakes is no better than the patterns fed into it. It's up to the user of this library to define what is correct and incorrect as precisely as possible. The intention is to aid a human in detecting mistakes; neither soundness nor completeness is probably feasible in most cases.