PetPeeve lets you correct the most common typographic errors and automagically normalize your strings.
MIT License
Trigger warning: Doesn't it also annoy you when people forget spaces after their punctuation,or make horrible capitalization mistakes? well, fear no more: this library is made for the likes of us.
In its essence, the library is an extensible set of function that correct the most common (and detectable) typographical and formatting mistakes people make when writing sentences, it is best used when you rely on user-submitted content but you still want your app to maintain a certain degree of consistency or even cleanliness.
Its basic usage is like so:
import PetPeeve from "petpeeve"
const corrector = PetPeeve();
corrector("Gosh!this sentence does not look nice!!");
// Outputs: Gosh! This sentence does not look nice!
PetPeeve performs a variety of operations, which can be optionally enabled or disabled according to your needs. PetPeeve
accepts an options
object, of which the keys are the functions you want to enable or disable, and the values are booleans
.
You can use this in two ways:
false
, then PetPeeve will enable all the operations except the ones you have disabled.The function returned by PetPeeve, accepts an extra optional argument if you ever want to change the options for one operation.
const corrector = PetPeeve({
capitalizeSentences: false,
removeExtraPunctuation: false
});
corrector("Gosh!this sentence does not look nice!!");
// Outputs: Gosh! this sentence does not look nice!!
corrector(
"Gosh!this sentence does not look nice!!",
{ removeExtraSpaces: true }
);
// Outputs: Gosh!this sentence does not look nice!!
Here is a list of the available operations:
Tries to detect sentences and capitalizes them.
This is a sentence
presumably this is another.
→
This is a sentence
Presumably this is another.
This is a sentence
This is another sentence
→
This is a sentence.
This is another sentence.
Fixes spacing between punctuations and following/preceding characters.
,
, .
, :
, ;
, )
, !
, ?
, “
, »
, …
).“
, «
)Moves periods outside brackets and quotation marks. E.g. A sentence (with a parenthetical.) → A sentence (with a parenthetical).; the so-called “quotes.” → the so-called “quotes”.
Removes duplicate punctuation marks. E.g. A bunch.. of extra,,,, punctuation!!! cool??? welp…… → A bunch. of extra, punctuation! cool? welp…
Removes duplicate spaces, carriage returns and new lines. E.g. So many spaces
→ So many spaces
.
Transforms an hyphen delimited dialog, to double carets. E.g.
- Hello there!
- Hello, how are you?
→
«Hello there!»
«Hello, how are you?»
Transforms certain commonly used characters into their typographically correct counterparts.
PetPeeve is not perfect by any means, and its rules were initially created with the Italian language in mind. While most of these rules are generally applicable to other languages, some are not.
Notably, the dialogue markers conventions are usually different for every language, and certain languages (such as German) might have different capitalization rules.
Ideally, we'd want to support as many languages as possible, by implementing some sort of localization mechanism similar to how other native javascript methods do.
Contributions in this sense are very much appreciated.
$ npm install --save petpeeve
import PetPeeve from "petpeeve-js";
const corrector = PetPeeve();
Specific imports:
import { Operations as corrections } from "petpeeve";
const myFixedString = corrections.capitalizeSentence("uncapitalized sentence");
// or
import { capitalizeSentences } from "petpeeve/lib/operations";
const myFixedString = capitalizeSentence("uncapitalized sentence");
Import it via a bundle that creates a global ( wider browser support ):
<script src="//unpkg.com/petpeeve/bundle.min.js"></script>
<script>
const { PetPeeve } = petpeeve;
</script>
You can specify the version you wish to import: unpkg.com
npm install
npm run build
npm test