string-process-comma-separated
Version:
Extracts chunks from possibly comma or whatever-separated string
12 lines (10 loc) • 2.57 kB
JavaScript
/**
* @name string-process-comma-separated
* @fileoverview Extracts chunks from possibly comma or whatever-separated string
* @version 4.2.3
* @author Roy Revelt
* @license MIT
* {@link https://codsen.com/os/string-process-comma-separated/}
*/
var $="4.2.3";var O=$;function E(s,l){if(typeof s!="string")throw new Error(`string-process-comma-separated: [THROW_ID_01] input must be string! It was given as ${typeof s}, equal to:
${JSON.stringify(s,null,4)}`);if(!s.length||!l||!l.cb&&!l.errCb)return;let e={...{from:0,to:s.length,offset:0,leadingWhitespaceOK:!1,trailingWhitespaceOK:!1,oneSpaceAfterCommaOK:!1,innerWhitespaceAllowed:!1,separator:",",cb:null,errCb:null},...l};Number.isInteger(l.from)||(e.from=0),Number.isInteger(l.to)||(e.to=s.length),Number.isInteger(l.offset)||(e.offset=0);let i=null,o=null,p=!1,a=[],f=null,n=!0;for(let t=e.from;t<e.to;t++){if(s[t].trim()&&s[t]!==e.separator&&(f=t),i===null&&s[t].trim()&&(!e.separator||s[t]!==e.separator)&&(p||(p=!0),a.length&&(a.length>1&&a.forEach((r,u)=>{u&&e.errCb&&e.errCb([[r+e.offset,r+1+e.offset]],"Remove separator.",n)}),a=[]),i=t),Number.isInteger(i)&&(t>i&&e.separator&&s[t]===e.separator||t+1===e.to)){let r=s.slice(i,t+1===e.to&&s[t]!==e.separator&&s[t].trim()?t+1:t);typeof e.cb=="function"&&e.cb(i+e.offset,(t+1===e.to&&s[t]!==e.separator&&s[t].trim()?t+1:f+1)+e.offset),i=null}if(!s[t].trim()&&o===null&&(o=t),o!==null&&(s[t].trim()||t+1===e.to)){if(o===e.from)!e.leadingWhitespaceOK&&typeof e.errCb=="function"&&e.errCb([[o+e.offset,(t+1===e.to?t+1:t)+e.offset]],"Remove whitespace.",n);else if(!s[t].trim()&&t+1===e.to)!e.trailingWhitespaceOK&&typeof e.errCb=="function"&&e.errCb([[o+e.offset,t+1+e.offset]],"Remove whitespace.",n);else if((!e.oneSpaceAfterCommaOK||!(s[t].trim()&&t>e.from+1&&s[t-1]===" "&&s[t-2]===","))&&(!e.innerWhitespaceAllowed||!(p&&s[o-1]&&s[t].trim()&&s[t]!==e.separator&&s[o-1]!==e.separator))){let r=o,u=t;t+1===e.to&&s[t]!==e.separator&&!s[t].trim()&&(u+=1);let m="";e.oneSpaceAfterCommaOK&&(s[o]===" "&&s[o-1]===e.separator?r+=1:s[o]!==" "&&(m=" "));let c="Remove whitespace.";!e.innerWhitespaceAllowed&&p&&s[o-1]&&s[t].trim()&&s[t]!==e.separator&&s[o-1]!==e.separator&&(n=!1,c="Bad whitespace."),m.length&&e.errCb?e.errCb([[r+e.offset,u+e.offset,m]],c,n):e.errCb&&e.errCb([[r+e.offset,u+e.offset]],c,n),n=!0}o=null}s[t]===e.separator&&(!p&&e.errCb?e.errCb([[t+e.offset,t+1+e.offset]],"Remove separator.",n):a.push(t)),t+1===e.to&&e.errCb&&a.forEach(r=>{e.errCb([[r+e.offset,r+1+e.offset]],"Remove separator.",n)})}}export{E as processCommaSep,O as version};