UNPKG

string-dashes

Version:

Comprehensive, HTML-entities-aware tool to typographically-correct the dashes and hyphens

51 lines (49 loc) 14.8 kB
/** * @name string-dashes * @fileoverview Comprehensive, HTML-entities-aware tool to typographically-correct the dashes and hyphens * @version 1.3.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-dashes/} */ "use strict";var stringDashes=(()=>{var z=Object.create;var w=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols,Y=Object.getPrototypeOf,C=Object.prototype.hasOwnProperty,ee=Object.prototype.propertyIsEnumerable;var _=(e,r,n)=>r in e?w(e,r,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[r]=n,m=(e,r)=>{for(var n in r||(r={}))C.call(r,n)&&_(e,n,r[n]);if(I)for(var n of I(r))ee.call(r,n)&&_(e,n,r[n]);return e};var re=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports),te=(e,r)=>{for(var n in r)w(e,n,{get:r[n],enumerable:!0})},V=(e,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let l of X(r))!C.call(e,l)&&l!==n&&w(e,l,{get:()=>r[l],enumerable:!(s=G(r,l))||s.enumerable});return e};var H=(e,r,n)=>(n=e!=null?z(Y(e)):{},V(r||!e||!e.__esModule?w(n,"default",{value:e,enumerable:!0}):n,e)),ne=e=>V(w({},"__esModule",{value:!0}),e);var N=re((Se,B)=>{"use strict";B.exports=oe;function b(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function oe(e){if(e=e||{},e.circles)return ue(e);let r=new Map;if(r.set(Date,u=>new Date(u)),r.set(Map,(u,a)=>new Map(s(Array.from(u),a))),r.set(Set,(u,a)=>new Set(s(Array.from(u),a))),e.constructorHandlers)for(let u of e.constructorHandlers)r.set(u[0],u[1]);let n=null;return e.proto?o:l;function s(u,a){let i=Object.keys(u),t=new Array(i.length);for(let f=0;f<i.length;f++){let c=i[f],g=u[c];typeof g!="object"||g===null?t[c]=g:g.constructor!==Object&&(n=r.get(g.constructor))?t[c]=n(g,a):ArrayBuffer.isView(g)?t[c]=b(g):t[c]=a(g)}return t}function l(u){if(typeof u!="object"||u===null)return u;if(Array.isArray(u))return s(u,l);if(u.constructor!==Object&&(n=r.get(u.constructor)))return n(u,l);let a={};for(let i in u){if(Object.hasOwnProperty.call(u,i)===!1)continue;let t=u[i];typeof t!="object"||t===null?a[i]=t:t.constructor!==Object&&(n=r.get(t.constructor))?a[i]=n(t,l):ArrayBuffer.isView(t)?a[i]=b(t):a[i]=l(t)}return a}function o(u){if(typeof u!="object"||u===null)return u;if(Array.isArray(u))return s(u,o);if(u.constructor!==Object&&(n=r.get(u.constructor)))return n(u,o);let a={};for(let i in u){let t=u[i];typeof t!="object"||t===null?a[i]=t:t.constructor!==Object&&(n=r.get(t.constructor))?a[i]=n(t,o):ArrayBuffer.isView(t)?a[i]=b(t):a[i]=o(t)}return a}}function ue(e){let r=[],n=[],s=new Map;if(s.set(Date,i=>new Date(i)),s.set(Map,(i,t)=>new Map(o(Array.from(i),t))),s.set(Set,(i,t)=>new Set(o(Array.from(i),t))),e.constructorHandlers)for(let i of e.constructorHandlers)s.set(i[0],i[1]);let l=null;return e.proto?a:u;function o(i,t){let f=Object.keys(i),c=new Array(f.length);for(let g=0;g<f.length;g++){let h=f[g],p=i[h];if(typeof p!="object"||p===null)c[h]=p;else if(p.constructor!==Object&&(l=s.get(p.constructor)))c[h]=l(p,t);else if(ArrayBuffer.isView(p))c[h]=b(p);else{let E=r.indexOf(p);E!==-1?c[h]=n[E]:c[h]=t(p)}}return c}function u(i){if(typeof i!="object"||i===null)return i;if(Array.isArray(i))return o(i,u);if(i.constructor!==Object&&(l=s.get(i.constructor)))return l(i,u);let t={};r.push(i),n.push(t);for(let f in i){if(Object.hasOwnProperty.call(i,f)===!1)continue;let c=i[f];if(typeof c!="object"||c===null)t[f]=c;else if(c.constructor!==Object&&(l=s.get(c.constructor)))t[f]=l(c,u);else if(ArrayBuffer.isView(c))t[f]=b(c);else{let g=r.indexOf(c);g!==-1?t[f]=n[g]:t[f]=u(c)}}return r.pop(),n.pop(),t}function a(i){if(typeof i!="object"||i===null)return i;if(Array.isArray(i))return o(i,a);if(i.constructor!==Object&&(l=s.get(i.constructor)))return l(i,a);let t={};r.push(i),n.push(t);for(let f in i){let c=i[f];if(typeof c!="object"||c===null)t[f]=c;else if(c.constructor!==Object&&(l=s.get(c.constructor)))t[f]=l(c,a);else if(ArrayBuffer.isView(c))t[f]=b(c);else{let g=r.indexOf(c);g!==-1?t[f]=n[g]:t[f]=a(c)}}return r.pop(),n.pop(),t}}});var ye={};te(ye,{convertAll:()=>de,convertOne:()=>K,defaults:()=>j,version:()=>he});var se={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function D(e,r){if(!Array.isArray(e)||!e.length)return e;let n=m(m({},se),r),s,l;if(n.strictlyTwoElementsInRangeArrays&&!e.every((a,i)=>!Array.isArray(a)||a.length!==2?(s=i,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, ".concat(s,"th range (").concat(JSON.stringify(e[s],null,4),") has not two but ").concat(l," elements!"));if(!e.every((a,i)=>!Array.isArray(a)||!Number.isInteger(a[0])||a[0]<0||!Number.isInteger(a[1])||a[1]<0?(s=i,!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, ".concat(s,"th range (").concat(JSON.stringify(e[s],null,4),") does not consist of only natural numbers!"));let o=e.length**2,u=0;return Array.from(e).sort((a,i)=>(n.progressFn&&(u+=1,n.progressFn(Math.floor(u*100/o))),a[0]===i[0]?a[1]<i[1]?-1:a[1]>i[1]?1:0:a[0]<i[0]?-1:1))}var J={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function L(e,r){function n(t){return!!t&&typeof t=="object"&&!Array.isArray(t)}if(!Array.isArray(e)||!e.length)return null;let s;if(r)if(n(r)){if(s=m(m({},J),r),s.progressFn&&n(s.progressFn)&&!Object.keys(s.progressFn).length)s.progressFn=null;else if(s.progressFn&&typeof s.progressFn!="function")throw new Error('ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "'.concat(typeof s.progressFn,'", equal to ').concat(JSON.stringify(s.progressFn,null,4)));if(![1,2,"1","2"].includes(s.mergeType))throw new Error('ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "'.concat(typeof s.mergeType,'", equal to ').concat(JSON.stringify(s.mergeType,null,4)));if(typeof s.joinRangesThatTouchEdges!="boolean")throw new Error('ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "'.concat(typeof s.joinRangesThatTouchEdges,'", equal to ').concat(JSON.stringify(s.joinRangesThatTouchEdges,null,4)))}else throw new Error("emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:\n".concat(JSON.stringify(r,null,4)," (type ").concat(typeof r,")"));else s=m({},J);let l=e.filter(t=>Array.isArray(t)).map(t=>[...t]).filter(t=>t[2]!==void 0||t[0]!==t[1]),o,u,a;s.progressFn?o=D(l,{progressFn:t=>{a=Math.floor(t/5),a!==u&&(u=a,s.progressFn(a))}}):o=D(l);let i=o.length-1;for(let t=i;t>0;t--)s.progressFn&&(a=Math.floor((1-t/i)*78)+21,a!==u&&a>u&&(u=a,s.progressFn(a))),(o[t][0]<=o[t-1][0]||!s.joinRangesThatTouchEdges&&o[t][0]<o[t-1][1]||s.joinRangesThatTouchEdges&&o[t][0]<=o[t-1][1])&&(o[t-1][0]=Math.min(o[t][0],o[t-1][0]),o[t-1][1]=Math.max(o[t][1],o[t-1][1]),o[t][2]!==void 0&&(o[t-1][0]>=o[t][0]||o[t-1][1]<=o[t][1])&&o[t-1][2]!==null&&(o[t][2]===null&&o[t-1][2]!==null?o[t-1][2]=null:o[t-1][2]!=null?+s.mergeType==2&&o[t-1][0]===o[t][0]?o[t-1][2]=o[t][2]:o[t-1][2]+=o[t][2]:o[t-1][2]=o[t][2]),o.splice(t,1),t=o.length);return o.length?o:null}var ie=!0,T="Invariant failed";function F(e,r){if(!e){if(ie)throw new Error(T);var n=typeof r=="function"?r():r,s=n?"".concat(T,": ").concat(n):T;throw new Error(s)}}function W(e,r,n){let s=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: ".concat(typeof e,", equal to: ").concat(JSON.stringify(e,null,4)));if(r&&!Array.isArray(r))throw new TypeError("ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ".concat(typeof r,", equal to: ").concat(JSON.stringify(r,null,4)));if(n&&typeof n!="function")throw new TypeError("ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ".concat(typeof n,", equal to: ").concat(JSON.stringify(n,null,4)));if(!(r!=null&&r.filter(f=>f).length))return e;let o;Array.isArray(r)&&Number.isInteger(r[0])&&Number.isInteger(r[1])?o=[Array.from(r)]:o=Array.from(r);let u=o.length,a=0;o.filter(f=>f).forEach((f,c)=>{if(n&&(s=Math.floor(a/u*10),s!==l&&(l=s,n(s))),!Array.isArray(f))throw new TypeError("ranges-apply: [THROW_ID_05] ranges array, second input arg., has ".concat(c,"th element not an array: ").concat(JSON.stringify(f,null,4),", which is ").concat(typeof f));if(!Number.isInteger(f[0])){if(!Number.isInteger(+f[0])||+f[0]<0)throw new TypeError("ranges-apply: [THROW_ID_06] ranges array, second input arg. has ".concat(c,"th element, array ").concat(JSON.stringify(f,null,0),". Its first element is not an integer, string index, but ").concat(typeof f[0],", equal to: ").concat(JSON.stringify(f[0],null,4),"."));o[c][0]=+o[c][0]}if(!Number.isInteger(f[1])){if(!Number.isInteger(+f[1])||+f[1]<0)throw new TypeError("ranges-apply: [THROW_ID_07] ranges array, second input arg. has ".concat(c,"th element, array ").concat(JSON.stringify(f,null,0),". Its second element is not an integer, string index, but ").concat(typeof f[1],", equal to: ").concat(JSON.stringify(f[1],null,4),"."));o[c][1]=+o[c][1]}a+=1});let i=L(o,{progressFn:f=>{n&&(s=10+Math.floor(f/10),s!==l&&(l=s,n(s)))}});F(i);let t=i.length;if(t>0){let f=e.slice(i[t-1][1]);e=i.reduce((c,g,h,p)=>{n&&(s=20+Math.floor(h/t*80),s!==l&&(l=s,n(s)));let E=h===0?0:p[h-1][1],Z=p[h][0];return"".concat(c).concat(e.slice(E,Z)).concat(p[h][2]||"")},""),e+=f}return e}var M=H(N(),1);var Re=(0,M.default)();var le="\u2018",ae="\u2019",fe="\u201C",ce="\u201D",v="\u2013",A="\u2014";function d(e){return y(e)&&e.charCodeAt(0)>=48&&e.charCodeAt(0)<=57}function S(e){return y(e)&&"\u060B$\u20BC\u17DB\xA5\u20A1\u20B1\xA3\u20AC\xA2\u20B9\uFDFC\u20AA\u20A9\u20AD\u20A8\u20AE\u20A6\u20BD\u20AB\u0E3F\u20A9\u20BA\u20B4".includes(e)}function O(e){return y(e)&&e.length===1&&e.toUpperCase()!==e.toLowerCase()}function k(e){return y(e)&&(e==='"'||e==="'"||e===le||e===ae||e===fe||e===ce)}function R(e){return!y(e)||!O(e)?!1:e===e.toUpperCase()&&e!==e.toLowerCase()}function x(e){return!y(e)||!e?!1:!e[0].trim()}function y(e){return typeof e=="string"}var P=H(N(),1);var Ce=(0,P.default)();var $="\xA0";function ge({str:e,idx:r=0,stopAtNewlines:n=!1,stopAtRawNbsp:s=!1}){if(typeof e!="string"||!e.length||((!r||typeof r!="number")&&(r=0),!e[r+1]))return null;if(e[r+1]&&(e[r+1].trim()||n&&"\n\r".includes(e[r+1])||s&&e[r+1]===$))return r+1;if(e[r+2]&&(e[r+2].trim()||n&&"\n\r".includes(e[r+2])||s&&e[r+2]===$))return r+2;for(let l=r+1,o=e.length;l<o;l++)if(e[l].trim()||n&&"\n\r".includes(e[l])||s&&e[l]===$)return l;return null}function q(e,r=0){return ge({str:e,idx:r,stopAtNewlines:!1,stopAtRawNbsp:!1})}function pe({str:e,idx:r,stopAtNewlines:n,stopAtRawNbsp:s}){if(typeof e!="string"||!e.length||((!r||typeof r!="number")&&(r=0),r<1))return null;if(e[~-r]&&(e[~-r].trim()||n&&"\n\r".includes(e[~-r])||s&&e[~-r]===$))return~-r;if(e[r-2]&&(e[r-2].trim()||n&&"\n\r".includes(e[r-2])||s&&e[r-2]===$))return r-2;for(let l=r;l--;)if(e[l]&&(e[l].trim()||n&&"\n\r".includes(e[l])||s&&e[l]===$))return l;return null}function U(e,r=0){return pe({str:e,idx:r,stopAtNewlines:!1,stopAtRawNbsp:!1})}var Q="1.3.0";var he=Q,j={convertEntities:!1,convertDashes:!0};function K(e,r){if(typeof e!="string")throw new Error("string-dashes/convertOne(): [THROW_ID_01] first input argument should be string! It's been passed as ".concat(e," (its typeof ").concat(typeof e,")"));if(typeof r!="object"||Array.isArray(r))throw new Error("string-dashes/convertOne(): [THROW_ID_02] options object should be a plain object. It has was passed as ".concat(JSON.stringify(r,null,4)," (its typeof is ").concat(typeof r,")"));if(!Number.isInteger(r.from)||r.from<0)throw new Error('string-dashes/convertOne(): [THROW_ID_03] options objects key "to", a starting string index, should be a natural number! It was given as '.concat(r.from," (its typeof is ").concat(typeof r.from,")"));if(r.from>=e.length)throw new Error("string-dashes/convertOne(): [THROW_ID_04] opts.from is beyond str length! opts.from was passed as ".concat(r.from," and str.length is ").concat(e.length));let{from:n=0,to:s,value:l,convertEntities:o,convertDashes:u,offsetBy:a}=m(m({},j),r);Number.isInteger(s)||(s=n+1);let i=[];if(!u)return null;if((l&&["-",A].includes(l)||s===n+1&&["-",A].includes(e[n]))&&(e[n-1]&&e[s]&&d(e[n-1])&&d(e[s])||!O(e[n-2])&&!O(e[s+1])&&R(e[n-1])&&R(e[s]))&&i.push([n,s,o?"&ndash;":v]),l&&["-",v].includes(l)||s===n+1&&["-",v].includes(e[n]))if(e[n-1]&&e[s]&&x(e[n-1])&&x(e[s])){let t=null,f=null;e[n-2]&&(e[n-2].trim()?t=n-2:t=U(e,n)),e[s+1]&&(e[s+1].trim()?f=s+1:f=q(e,n)),d(e[t])&&d(e[f])||d(e[t])&&S(e[f])||S(e[t])&&d(e[f])?i.push([n,s,o?"&ndash;":v]):i.push([n,s,o?"&mdash;":A])}else e[n-1]&&O(e[n-1])&&e[s]&&k(e[s])&&i.push([n,s,o?"&mdash;":A]);return i.length?i:null}function de(e,r){if(typeof e!="string")throw new Error("string-dashes: [THROW_ID_10] first input argument should be string! It's been passed as ".concat(e," (its typeof ").concat(typeof e,")"));if(r&&(typeof r!="object"||Array.isArray(r)))throw new Error("string-dashes: [THROW_ID_11] options object should be a plain object! It was passed as ".concat(JSON.stringify(r,null,4)," (its typeof is ").concat(typeof r,")"));if(!e)return{result:e,ranges:null};let n=[],s=m(m({},j),r),l=e.length;for(let o=0;o<l;o++){s.from=o,s.offsetBy=a=>{o+=a};let u=K(e,s);Array.isArray(u)&&u.length&&(n=n.concat(u))}return{result:W(e,n),ranges:n}}return ne(ye);})(); /** * @name ranges-sort * @fileoverview Sort string index ranges * @version 6.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/ranges-sort/} */ /** * @name ranges-merge * @fileoverview Merge and sort string index ranges * @version 9.1.0 * @author Roy Revelt * @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.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/ranges-apply/} */ /** * @name codsen-utils * @fileoverview Various utility functions * @version 1.7.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/codsen-utils/} */ /** * @name string-left-right * @fileoverview Looks up the first non-whitespace character to the left/right of a given index * @version 6.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-left-right/} */