string-apostrophes
Version:
Comprehensive, HTML-entities-aware tool to typographically-correct the apostrophes and single/double quotes
44 lines (42 loc) • 17.3 kB
JavaScript
/**
* @name string-apostrophes
* @fileoverview Comprehensive, HTML-entities-aware tool to typographically-correct the apostrophes and single/double quotes
* @version 4.0.23
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-apostrophes/}
*/
;var stringApostrophes=(()=>{var M=Object.create;var q=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames,P=Object.getOwnPropertySymbols,Z=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty,z=Object.prototype.propertyIsEnumerable;var C=(e,i,r)=>i in e?q(e,i,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[i]=r,d=(e,i)=>{for(var r in i||(i={}))j.call(i,r)&&C(e,r,i[r]);if(P)for(var r of P(i))z.call(i,r)&&C(e,r,i[r]);return e};var G=(e,i)=>()=>(i||e((i={exports:{}}).exports,i),i.exports),X=(e,i)=>{for(var r in i)q(e,r,{get:i[r],enumerable:!0})},N=(e,i,r,o)=>{if(i&&typeof i=="object"||typeof i=="function")for(let l of K(i))!j.call(e,l)&&l!==r&&q(e,l,{get:()=>i[l],enumerable:!(o=W(i,l))||o.enumerable});return e};var Y=(e,i,r)=>(r=e!=null?M(Z(e)):{},N(i||!e||!e.__esModule?q(r,"default",{value:e,enumerable:!0}):r,e)),ee=e=>N(q({},"__esModule",{value:!0}),e);var _=G((de,L)=>{"use strict";L.exports=ne;function D(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function ne(e){if(e=e||{},e.circles)return se(e);let i=new Map;if(i.set(Date,u=>new Date(u)),i.set(Map,(u,a)=>new Map(o(Array.from(u),a))),i.set(Set,(u,a)=>new Set(o(Array.from(u),a))),e.constructorHandlers)for(let u of e.constructorHandlers)i.set(u[0],u[1]);let r=null;return e.proto?n:l;function o(u,a){let s=Object.keys(u),t=new Array(s.length);for(let c=0;c<s.length;c++){let p=s[c],$=u[p];typeof $!="object"||$===null?t[p]=$:$.constructor!==Object&&(r=i.get($.constructor))?t[p]=r($,a):ArrayBuffer.isView($)?t[p]=D($):t[p]=a($)}return t}function l(u){if(typeof u!="object"||u===null)return u;if(Array.isArray(u))return o(u,l);if(u.constructor!==Object&&(r=i.get(u.constructor)))return r(u,l);let a={};for(let s in u){if(Object.hasOwnProperty.call(u,s)===!1)continue;let t=u[s];typeof t!="object"||t===null?a[s]=t:t.constructor!==Object&&(r=i.get(t.constructor))?a[s]=r(t,l):ArrayBuffer.isView(t)?a[s]=D(t):a[s]=l(t)}return a}function n(u){if(typeof u!="object"||u===null)return u;if(Array.isArray(u))return o(u,n);if(u.constructor!==Object&&(r=i.get(u.constructor)))return r(u,n);let a={};for(let s in u){let t=u[s];typeof t!="object"||t===null?a[s]=t:t.constructor!==Object&&(r=i.get(t.constructor))?a[s]=r(t,n):ArrayBuffer.isView(t)?a[s]=D(t):a[s]=n(t)}return a}}function se(e){let i=[],r=[],o=new Map;if(o.set(Date,s=>new Date(s)),o.set(Map,(s,t)=>new Map(n(Array.from(s),t))),o.set(Set,(s,t)=>new Set(n(Array.from(s),t))),e.constructorHandlers)for(let s of e.constructorHandlers)o.set(s[0],s[1]);let l=null;return e.proto?a:u;function n(s,t){let c=Object.keys(s),p=new Array(c.length);for(let $=0;$<c.length;$++){let y=c[$],b=s[y];if(typeof b!="object"||b===null)p[y]=b;else if(b.constructor!==Object&&(l=o.get(b.constructor)))p[y]=l(b,t);else if(ArrayBuffer.isView(b))p[y]=D(b);else{let A=i.indexOf(b);A!==-1?p[y]=r[A]:p[y]=t(b)}}return p}function u(s){if(typeof s!="object"||s===null)return s;if(Array.isArray(s))return n(s,u);if(s.constructor!==Object&&(l=o.get(s.constructor)))return l(s,u);let t={};i.push(s),r.push(t);for(let c in s){if(Object.hasOwnProperty.call(s,c)===!1)continue;let p=s[c];if(typeof p!="object"||p===null)t[c]=p;else if(p.constructor!==Object&&(l=o.get(p.constructor)))t[c]=l(p,u);else if(ArrayBuffer.isView(p))t[c]=D(p);else{let $=i.indexOf(p);$!==-1?t[c]=r[$]:t[c]=u(p)}}return i.pop(),r.pop(),t}function a(s){if(typeof s!="object"||s===null)return s;if(Array.isArray(s))return n(s,a);if(s.constructor!==Object&&(l=o.get(s.constructor)))return l(s,a);let t={};i.push(s),r.push(t);for(let c in s){let p=s[c];if(typeof p!="object"||p===null)t[c]=p;else if(p.constructor!==Object&&(l=o.get(p.constructor)))t[c]=l(p,a);else if(ArrayBuffer.isView(p))t[c]=D(p);else{let $=i.indexOf(p);$!==-1?t[c]=r[$]:t[c]=a(p)}}return i.pop(),r.pop(),t}}});var le={};X(le,{convertAll:()=>ie,convertOne:()=>J,defaults:()=>T,version:()=>ue});var oe={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function V(e,i){if(!Array.isArray(e)||!e.length)return e;let r=d(d({},oe),i),o,l;if(r.strictlyTwoElementsInRangeArrays&&!e.every((a,s)=>!Array.isArray(a)||a.length!==2?(o=s,l=a.length,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ${o}th range (${JSON.stringify(e[o],null,4)}) has not two but ${l} elements!`);if(!e.every((a,s)=>!Array.isArray(a)||!Number.isInteger(a[0])||a[0]<0||!Number.isInteger(a[1])||a[1]<0?(o=s,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ${o}th range (${JSON.stringify(e[o],null,4)}) does not consist of only natural numbers!`);let n=e.length**2,u=0;return Array.from(e).sort((a,s)=>(r.progressFn&&(u+=1,r.progressFn(Math.floor(u*100/n))),a[0]===s[0]?a[1]<s[1]?-1:a[1]>s[1]?1:0:a[0]<s[0]?-1:1))}var I={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function R(e,i){function r(t){return!!t&&typeof t=="object"&&!Array.isArray(t)}if(!Array.isArray(e)||!e.length)return null;let o;if(i)if(r(i)){if(o=d(d({},I),i),o.progressFn&&r(o.progressFn)&&!Object.keys(o.progressFn).length)o.progressFn=null;else if(o.progressFn&&typeof o.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "${typeof o.progressFn}", equal to ${JSON.stringify(o.progressFn,null,4)}`);if(![1,2,"1","2"].includes(o.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof o.mergeType}", equal to ${JSON.stringify(o.mergeType,null,4)}`);if(typeof o.joinRangesThatTouchEdges!="boolean")throw new Error(`ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof o.joinRangesThatTouchEdges}", equal to ${JSON.stringify(o.joinRangesThatTouchEdges,null,4)}`)}else throw new Error(`emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:
${JSON.stringify(i,null,4)} (type ${typeof i})`);else o=d({},I);let l=e.filter(t=>Array.isArray(t)).map(t=>[...t]).filter(t=>t[2]!==void 0||t[0]!==t[1]),n,u,a;o.progressFn?n=V(l,{progressFn:t=>{a=Math.floor(t/5),a!==u&&(u=a,o.progressFn(a))}}):n=V(l);let s=n.length-1;for(let t=s;t>0;t--)o.progressFn&&(a=Math.floor((1-t/s)*78)+21,a!==u&&a>u&&(u=a,o.progressFn(a))),(n[t][0]<=n[t-1][0]||!o.joinRangesThatTouchEdges&&n[t][0]<n[t-1][1]||o.joinRangesThatTouchEdges&&n[t][0]<=n[t-1][1])&&(n[t-1][0]=Math.min(n[t][0],n[t-1][0]),n[t-1][1]=Math.max(n[t][1],n[t-1][1]),n[t][2]!==void 0&&(n[t-1][0]>=n[t][0]||n[t-1][1]<=n[t][1])&&n[t-1][2]!==null&&(n[t][2]===null&&n[t-1][2]!==null?n[t-1][2]=null:n[t-1][2]!=null?+o.mergeType==2&&n[t-1][0]===n[t][0]?n[t-1][2]=n[t][2]:n[t-1][2]+=n[t][2]:n[t-1][2]=n[t][2]),n.splice(t,1),t=n.length);return n.length?n:null}var re=!0,O="Invariant failed";function U(e,i){if(!e){if(re)throw new Error(O);var r=typeof i=="function"?i():i,o=r?"".concat(O,": ").concat(r):O;throw new Error(o)}}function x(e,i,r){let o=0,l=0;if(arguments.length===0)throw new Error("ranges-apply: [THROW_ID_01] inputs missing!");if(typeof e!="string")throw new TypeError(`ranges-apply: [THROW_ID_02] first input argument must be a string! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(i&&!Array.isArray(i))throw new TypeError(`ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ${typeof i}, equal to: ${JSON.stringify(i,null,4)}`);if(r&&typeof r!="function")throw new TypeError(`ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ${typeof r}, equal to: ${JSON.stringify(r,null,4)}`);if(!(i!=null&&i.filter(c=>c).length))return e;let n;Array.isArray(i)&&Number.isInteger(i[0])&&Number.isInteger(i[1])?n=[Array.from(i)]:n=Array.from(i);let u=n.length,a=0;n.filter(c=>c).forEach((c,p)=>{if(r&&(o=Math.floor(a/u*10),o!==l&&(l=o,r(o))),!Array.isArray(c))throw new TypeError(`ranges-apply: [THROW_ID_05] ranges array, second input arg., has ${p}th element not an array: ${JSON.stringify(c,null,4)}, which is ${typeof c}`);if(!Number.isInteger(c[0])){if(!Number.isInteger(+c[0])||+c[0]<0)throw new TypeError(`ranges-apply: [THROW_ID_06] ranges array, second input arg. has ${p}th element, array ${JSON.stringify(c,null,0)}. Its first element is not an integer, string index, but ${typeof c[0]}, equal to: ${JSON.stringify(c[0],null,4)}.`);n[p][0]=+n[p][0]}if(!Number.isInteger(c[1])){if(!Number.isInteger(+c[1])||+c[1]<0)throw new TypeError(`ranges-apply: [THROW_ID_07] ranges array, second input arg. has ${p}th element, array ${JSON.stringify(c,null,0)}. Its second element is not an integer, string index, but ${typeof c[1]}, equal to: ${JSON.stringify(c[1],null,4)}.`);n[p][1]=+n[p][1]}a+=1});let s=R(n,{progressFn:c=>{r&&(o=10+Math.floor(c/10),o!==l&&(l=o,r(o)))}});U(s);let t=s.length;if(t>0){let c=e.slice(s[t-1][1]);e=s.reduce((p,$,y,b)=>{r&&(o=20+Math.floor(y/t*80),o!==l&&(l=o,r(o)));let A=y===0?0:b[y-1][1],k=b[y][0];return`${p}${e.slice(A,k)}${b[y][2]||""}`},""),e+=c}return e}var Q=Y(_(),1);var Ee=(0,Q.default)();var g="\u2018",m="\u2019",h="\u201C",f="\u201D";var S="\u2032",v="\u2033";var H=[".",",",";","!","?"];function E(e){return F(e)&&e.charCodeAt(0)>=48&&e.charCodeAt(0)<=57}function w(e){return F(e)&&e.length===1&&e.toUpperCase()!==e.toLowerCase()}function F(e){return typeof e=="string"}var B="4.0.23";var ue=B,T={convertEntities:!1,convertApostrophes:!0};function J(e,i){var t;if(typeof e!="string")throw new Error(`string-apostrophes/convertOne(): [THROW_ID_01] first input argument should be string! It's been passed as ${e} (its typeof ${typeof e})`);if(typeof i!="object"||Array.isArray(i))throw new Error(`string-apostrophes/convertOne(): [THROW_ID_02] options object should be a plain object. It has was passed as ${JSON.stringify(i,null,4)} (its typeof is ${typeof i})`);if(!Number.isInteger(i.from)||i.from<0)throw new Error(`string-apostrophes/convertOne(): [THROW_ID_03] options objects key "to", a starting string index, should be a natural number! It was given as ${i.from} (its typeof is ${typeof i.from})`);if(i.from>=e.length)throw new Error(`string-apostrophes/convertOne(): [THROW_ID_04] opts.from is beyond str length! opts.from was passed as ${i.from} and str.length is ${e.length}`);let{from:r=0,to:o,value:l,convertEntities:n,convertApostrophes:u,offsetBy:a}=d(d({},T),i);Number.isInteger(o)||(o=r+1);let s=[];return l&&["'",g,m,S].includes(l)||o===r+1&&["'",g,m,S].includes(e[r])?e[r-1]&&e[o]&&E(e[r-1])&&!w(e[o])?u&&e.slice(r,o)!==(n?"′":S)&&l!==(n?"′":S)?s.push([r,o,n?"′":S]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):e[o]&&e[o+1]&&e[o]==="n"&&e.slice(r,o)===e.slice(o+1,o+1+(o-r))?u&&e.slice(r,o+2)!==(n?"’n’":`${m}n${m}`)&&l!==(n?"’n’":`${m}n${m}`)?(s.push([r,o+2,n?"’n’":`${m}n${m}`]),typeof a=="function"&&a(2)):!u&&e.slice(r,o+2)!=="'n'"&&l!=="'n'"&&(s.push([r,o+2,"'n'"]),typeof a=="function"&&a(2)):e[o]&&e[o].toLowerCase()==="t"&&(!((t=e[o+1])!=null&&t.trim())||e[o+1].toLowerCase()==="i")||e[o]&&e[o+2]&&e[o].toLowerCase()==="t"&&e[o+1].toLowerCase()==="w"&&(e[o+2].toLowerCase()==="a"||e[o+2].toLowerCase()==="e"||e[o+2].toLowerCase()==="i"||e[o+2].toLowerCase()==="o")||e[o]&&e[o+1]&&e[o].toLowerCase()==="e"&&e[o+1].toLowerCase()==="m"||e[o]&&e[o+4]&&e[o].toLowerCase()==="c"&&e[o+1].toLowerCase()==="a"&&e[o+2].toLowerCase()==="u"&&e[o+3].toLowerCase()==="s"&&e[o+4].toLowerCase()==="e"||e[o]&&E(e[o])?u&&e.slice(r,o)!==(n?"’":m)&&l!==(n?"’":m)?s.push([r,o,n?"’":m]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):e[r-1]&&e[o]&&H.includes(e[r-1])?e[o].trim()?e[o]==='"'&&e[o+1]&&!e[o+1].trim()&&(u&&e.slice(r,o+1)!==(n?"’”":`${m}${f}`)&&l!==(n?"’”":`${m}${f}`)?(s.push([r,o+1,`${n?"’”":`${m}${f}`}`]),typeof a=="function"&&a(1)):!u&&e.slice(r,o+1)!==`'"`&&l!==`'"`&&(s.push([r,o+1,`'"`]),typeof a=="function"&&a(1))):u&&e.slice(r,o)!==(n?"’":m)&&l!==(n?"’":m)?s.push([r,o,n?"’":m]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):r===0&&e.slice(o).trim()?u&&e.slice(r,o)!==(n?"‘":g)&&l!==(n?"‘":g)?s.push([r,o,n?"‘":g]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):!e[o]&&e.slice(0,r).trim()?u&&e.slice(r,o)!==(n?"’":m)&&l!==(n?"’":m)?s.push([r,o,n?"’":m]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):e[r-1]&&e[o]&&(w(e[r-1])||E(e[r-1]))&&(w(e[o])||E(e[o]))?u?(e[o]&&e[r-5]&&e[r-5].toLowerCase()==="h"&&e[r-4].toLowerCase()==="a"&&e[r-3].toLowerCase()==="w"&&e[r-2].toLowerCase()==="a"&&e[r-1].toLowerCase()==="i"&&e[o].toLowerCase()==="i"||e[r-1]&&e[r-1].toLowerCase()==="o"&&e[o+2]&&e[o].toLowerCase()==="a"&&e[o+1].toLowerCase()==="h"&&e[o+2].toLowerCase()==="u")&&e.slice(r,o)!==(n?"‘":g)&&l!==(n?"‘":g)?s.push([r,o,n?"‘":g]):e.slice(r,o)!==(n?"’":m)&&l!==(n?"’":m)&&s.push([r,o,n?"’":m]):e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):e[o]&&(w(e[o])||E(e[o]))?u&&e.slice(r,o)!==(n?"‘":g)&&l!==(n?"‘":g)?s.push([r,o,n?"‘":g]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):w(e[r-1])||E(e[r-1])?u&&e.slice(r,o)!==(n?"’":m)&&l!==(n?"’":m)?s.push([r,o,n?"’":m]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):e[r-1]&&!e[r-1].trim()?u&&e.slice(r,o)!==(n?"‘":g)&&l!==(n?"‘":g)?s.push([r,o,n?"‘":g]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"]):e[o]&&!e[o].trim()&&(u&&e.slice(r,o)!==(n?"’":m)&&l!==(n?"’":m)?s.push([r,o,n?"’":m]):!u&&e.slice(r,o)!=="'"&&l!=="'"&&s.push([r,o,"'"])):(['"',h,f,v].includes(l)||o===r+1&&['"',h,f,v].includes(e[r]))&&(e[r-1]&&E(e[r-1])&&e[o]&&e[o]!=="'"&&e[o]!=='"'&&e[o]!==m&&e[o]!==f&&e[o]!==g&&e[o]!==h?u&&e.slice(r,o)!==(n?"″":v)&&l!==(n?"″":v)?s.push([r,o,n?"″":v]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']):e[r-1]&&e[o]&&H.includes(e[r-1])?e[o].trim()?e[o]==="'"&&e[o+1]&&!e[o+1].trim()&&(u&&e.slice(r,o+1)!==(n?"”’":`${f}${m}`)&&l!==(n?"”’":`${f}${m}`)?(s.push([r,o+1,n?"”’":`${f}${m}`]),typeof a=="function"&&a(1)):!u&&e.slice(r,o+1)!==`"'`&&l!==`"'`&&(s.push([r,o+1,`"'`]),typeof a=="function"&&a(1))):u&&e.slice(r,o)!==(n?"”":f)&&l!==(n?"”":f)?s.push([r,o,n?"”":f]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']):r===0&&e[o]&&e.slice(o).trim()?u&&e.slice(r,o)!==(n?"“":h)&&l!==(n?"“":h)?s.push([r,o,n?"“":h]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']):!e[o]&&e.slice(0,r).trim()?u&&e.slice(r,o)!==(n?"”":f)&&l!==(n?"”":f)?s.push([r,o,n?"”":f]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']):e[o]&&(w(e[o])||E(e[o]))?u&&e.slice(r,o)!==(n?"“":h)&&l!==(n?"“":h)?s.push([r,o,n?"“":h]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']):e[r-1]&&(w(e[r-1])||E(e[r-1]))?u&&e.slice(r,o)!==(n?"”":f)&&l!==(n?"”":f)?s.push([r,o,n?"”":f]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']):e[r-1]&&!e[r-1].trim()?u&&e.slice(r,o)!==(n?"“":h)&&l!==(n?"“":h)?s.push([r,o,n?"“":h]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']):e[o]&&!e[o].trim()&&(u&&e.slice(r,o)!==(n?"”":f)&&l!==(n?"”":f)?s.push([r,o,n?"”":f]):!u&&e.slice(r,o)!=='"'&&l!=='"'&&s.push([r,o,'"']))),s}function ie(e,i){if(typeof e!="string")throw new Error(`string-apostrophes: [THROW_ID_10] first input argument should be string! It's been passed as ${e} (its typeof ${typeof e})`);if(i&&(typeof i!="object"||Array.isArray(i)))throw new Error(`string-apostrophes: [THROW_ID_11] options object should be a plain object! It was passed as ${JSON.stringify(i,null,4)} (its typeof is ${typeof i})`);if(!e)return{result:e,ranges:null};let r=[],o=d(d({},T),i),l=e.length;for(let n=0;n<l;n++){o.from=n,o.offsetBy=a=>{n+=a};let u=J(e,o);Array.isArray(u)&&u.length&&(r=r.concat(u))}return{result:x(e,r),ranges:r}}return ee(le);})();
/**
* @name ranges-sort
* @fileoverview Sort string index ranges
* @version 6.0.14
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-sort/}
*/
/**
* @name ranges-merge
* @fileoverview Merge and sort string index ranges
* @version 9.0.19
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-merge/}
*/
/**
* @name ranges-apply
* @fileoverview Take an array of string index ranges, delete/replace the string according to them
* @version 7.0.20
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-apply/}
*/
/**
* @name codsen-utils
* @fileoverview Various utility functions
* @version 1.6.8
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/codsen-utils/}
*/