Skip to content

Instantly share code, notes, and snippets.

@ciolt
Last active December 7, 2016 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ciolt/bf6e4eec0f9d4dd6b67f5fb9cad9eba4 to your computer and use it in GitHub Desktop.
Save ciolt/bf6e4eec0f9d4dd6b67f5fb9cad9eba4 to your computer and use it in GitHub Desktop.
Convert dumb quotes to smart quotes and add em dashes where needed.
String.prototype.Smartify = function () {
var base = this.valueOf(); // gets the value of the original string
var _a = new RegExp(/([\"]([a-zA-Z0-9\s\S]{0,}?)([\"]))/, 'gm'); // filter for large chunks of dumb quotes ["which is shit like this"]
var _b = new RegExp(/([^\"]([a-zA-Z0-9\s\S]{0,}?)([^\"]))/, 'gm'); // sub-filter for everything that isn't dumb quotes [which is shit like this]
var _c = new RegExp(/([0-9]){1,}([\"]){1}/, 'gm'); // filter for smartifying [5"] with [5“]
var _d = new RegExp(/([^\s0-9]{1,}[\'][a-zA-Z]{1,})/, 'gm'); // filter for getting words like [don't] and [couldn't]
var _e = new RegExp(/([\']([a-zA-Z0-9\s\S]{0,}?)([\']))/, 'gm'); // gets large chunks of dumb apostrophes like ['What did she say?'] and ['thus we heard it']
var _f = new RegExp(/([^\s0-9]{0,}[\'][a-zA-Z]{0,})/, 'gm'); // gets the remaining dumb apostrophes like ['bout] and [lyin']
var _g = new RegExp(/(\s[\-]{1,2}\s)|((\s){0,1}[\-]{2}(\s){0,1})|((\s){0,1}[\-]\s)|(\s[\-](\s){0,1})/, 'gm'); // gets all the makeshift em dashes and converts them to REAL em dashes
var _h = new RegExp(/[\"]/, 'gm'); // matches all dumb quotes
var _i = new RegExp(/[\']/, 'gm'); // matches all dumb apostrophes
var _j = new RegExp(/[\-]/, 'gm'); // gets all simple dashes
var mod_string =
base.replace(_c, function (a) {
var b = a.replace(_h, "");
return `${b}“`; // converts shorthand inches measurement [6"] to use a smart quote
}) .replace(_a, function (a) {
var b = a.replace(_h, "");
return `“${b}”`; // converts quotations to use smart quotes
}) .replace(_d, function (a) {
var b = a.replace(_i, "’");
return b; // converts words like [don't] to use a smart quote
}) .replace(_f, function (a) {
var b = a.replace(_i, "’");
return `${b}`;
}) .replace(_e, function (a) {
var b = a.replace(_i, "");
return `‘${b}’`; // converts apostrophes to use
}) .replace(_g, " — ") // em dash conversion
.replace(_j, "–"); // replaces simple dashes with long conjunctive dashes
return mod_string;
};
@ciolt
Copy link
Author

ciolt commented Dec 7, 2016

Sample paragraph:

At one o'clock, we were all at work again. I, having completed sixty-four lids, and the supply being consumed, was put at "molding in." Everybody was rushing- girls of all ages and appearances and hurrying men- and I went along, as one of the throng. It was the boys' tower. Famed to be said to be "ain't worth nothing." I was shocked, a man of about 6'5" has just given me the "ok" to say 'no.' It couldn't have been this way. Shocked-- as he was. No--he mustn't, it was only feasible to continue towin' on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment