petpeeve
v1.0.0
Published
Petpeeve.js lets you correct the most common typographic errors and automagically normalize your strings according to sane formatting rules.
Downloads
5
Maintainers
Readme
Usage
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!
Operations
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:
- If all the values you pass are
false
, then PetPeeve will enable all the operations except the ones you have disabled. - Otherwise, PetPeeve will only perform the operations you have whitelisted.
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:
capitalizeSentences
Tries to detect sentences and capitalizes them.
- Always capitalizes the beginning of a string. E.g. beginning capitalization → Beginning capitalization.
- Capitalizes after appropriate punctuation marks (., ? and !). E.g. I am excited! what about you? → I am excited! What about you?.
- Capitalizes after newlines. E.g.
→This is a sentence presumably this is another.
This is a sentence Presumably this is another.
fixEndingPunctuation
- Adds a period at the end of each line. E.g.
→This is a sentence This is another sentence
This is a sentence. This is another sentence.
- Transforms quotation marks into double carets if sentence begins and ends with them. E.g. “Hey man!” → «Hey man!»
- Respects pre-existing punctuation.
fixPunctuationSpace
Fixes spacing between punctuations and following/preceding characters.
- Adds spaces after most punctuations marks (
,
,.
,:
,;
,)
,!
,?
,“
,»
,…
). - Remove spaces after certain punctuations marks (
“
,«
) - Preserve spaces in certain exceptions; e.g. when digits are present (time formatting: 10:30, decimal/currency formatting: 10,30, 10.30).
periodOutsideBrackets
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”.
removeExtraPunctuation
Removes duplicate punctuation marks. E.g. A bunch.. of extra,,,, punctuation!!! cool??? welp…… → A bunch. of extra, punctuation! cool? welp…
removeExtraSpaces
Removes duplicate spaces, carriage returns and new lines. E.g. So many
spaces
→ So many spaces
.
smartDialogMarkers
Transforms an hyphen delimited dialog, to double carets. E.g.
- Hello there!
- Hello, how are you?
→
«Hello there!»
«Hello, how are you?»
smartPunctuation
Transforms certain commonly used characters into their typographically correct counterparts.
- "" (straight quotes) → “” (proper opening and closing)
- << >> → « »
- -- → – (En dash)
- --- → — (Em dash)
- ... / . . . (three periods, with or without spacing) → … (ellipsis)
⚠️ Warnings
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.
Install / Import
$ 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 from HTML, with CDN
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
Contribute
npm install
npm run build
npm test