UNPKG

edit-package-json

Version:

Edit package.json without parsing, as string, to keep the formatting intact

17 lines (15 loc) 9.43 kB
/** * @name edit-package-json * @fileoverview Edit package.json without parsing, as string, to keep the formatting intact * @version 0.8.27 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/edit-package-json/} */ import{left as V,right as P,chompLeft as U}from"string-left-right";import{rApply as v}from"ranges-apply";import{isStr as O,isNum as F}from"codsen-utils";var k="0.8.27";var _=k;function J(e){return Array.isArray(e)?e.join("."):O(e)?e:String(e)}function M(e){return O(e)&&e.startsWith('"')&&e.endsWith('"')?`${JSON.stringify(e.slice(1,e.length-1),null,0)}`:JSON.stringify(e,null,0)}function f(e,y){if(e[y]!=="\\")return!0;let h=U(e,y,{mode:1},"\\");return!!(F(h)&&(y-h)%2!==0)}function I({str:e,path:y,valToInsert:h,mode:N}){let n=0;function t(a){e[n]}let x=e.length,j=[];t(`077 main(): MODE=${N}`);let w=["{","}","[","]",":",","],D=h;O(h)&&!h.startsWith('"')&&!h.startsWith("{")&&(D=`"${h}"`);let b=[],o=[],c=!1,i=!1,r=!1,g=null,E=null,u=null,d=null,S=null,T=null,l;function W(){return typeof l=="number"}let s=!1,m=[];function A(){g=null,E=null,u=null,d=null,S=null,T=null}A();let $=[];for(n=0;n<x;n++){if(t(` \x1B[36m===============================\x1B[39m \x1B[35m${`str[ ${n} ] = ${e[n]?.trim()?e[n]:JSON.stringify(e[n],null,0)}`}\x1B[39m \x1B[36m===============================\x1B[39m `),typeof l!="number"&&e[n-1]==="["&&(i=!0,e[n]!=="]"&&(c=!1)),typeof l!="number"&&e[n-1]==="{"&&(c=!0,e[n]!=="}"&&(i=!1)),typeof l!="number"&&e[n]==="{"&&f(e,n-1)&&!r&&(i&&(s||(t(`198 \x1B[33mcurrentPath\x1B[39m = ${JSON.stringify($,null,4)}`),$[$.length-1]=$[$.length-1]+1,t(`207 \x1B[32mSET\x1B[39m ${`\x1B[33m${`currentPath[${$.length-1}]`}\x1B[39m`} = ${$[$.length-1]}`))),b.push(n),t(`215 \x1B[32mPUSH\x1B[39m \x1B[33mwithinObjectIndexes\x1B[39m = ${JSON.stringify(b,null,4)}`)),typeof l!="number"&&e[n]==="}"&&f(e,n-1)&&!r&&(b.pop(),t(`231 \x1B[31mPOP\x1B[39m \x1B[33mwithinObjectIndexes\x1B[39m = ${JSON.stringify(b,null,4)}`)),typeof l!="number"&&e[n]==="]"&&f(e,n-1)&&!r&&(o.pop(),t(`248 \x1B[32mPOP\x1B[39m \x1B[33mwithinArrayIndexes\x1B[39m = ${JSON.stringify(o,null,4)}`),$.pop(),t(`256 POP path, now = ${JSON.stringify($,null,4)}`),t("258 \x1B[31mRESET\x1B[39m"),A(),s&&(s=!1,t(`267 \x1B[32mSET\x1B[39m \x1B[33mitsTheFirstElem\x1B[39m = ${s}`))),typeof l!="number"&&e[n]==="]"&&(o.length?o.length&&(!b.length||o[o.length-1]>b[b.length-1])&&(i=!0):(i=!1,b.length&&!c&&(c=!0))),typeof l!="number"&&e[n]==="}"&&(b.length?(!o.length||b[b.length-1]>o[o.length-1])&&(c=!0):c=!1),i&&J(y)===$.join(".")&&!r&&e[n].trim()&&(r=!0,t(`329 \x1B[32mSET\x1B[39m \x1B[33mreplaceThisValue\x1B[39m = ${r}`),u=n,t(`334 \x1B[32mSET\x1B[39m \x1B[33mvalueStartedAt\x1B[39m = ${u}`)),typeof l!="number"&&e[n]==="["&&f(e,n-1)&&!r&&(o.push(n),s=!0,t(`348 \x1B[32mPUSH\x1B[39m \x1B[33mwithinArrayIndexes\x1B[39m = ${JSON.stringify(o,null,4)}; \x1B[33mitsTheFirstElem\x1B[39m = ${s}`),$.push(0),t(`359 \x1B[32mPUSH\x1B[39m zero to path, now = ${JSON.stringify($,null,0)}`)),i&&e[n]===","&&s&&!(typeof u=="number"&&d===null)&&(s=!1,t(`379 \x1B[32mSET\x1B[39m \x1B[33mitsTheFirstElem\x1B[39m = ${s}`)),!r&&u===null&&e[n].trim()&&!w.includes(e[n])&&(i||!i&&S!==null)&&(t("415 catching the start of a value clauses"),u=n,t(`418 \x1B[32mSET\x1B[39m \x1B[33mvalueStartedAt\x1B[39m = ${u}`),i&&(s?(s=!1,t(`426 \x1B[32mSET\x1B[39m \x1B[33mitsTheFirstElem\x1B[39m = ${s}`)):typeof $[$.length-1]=="number"&&($[$.length-1]=$[$.length-1]+1,t(`432 \x1B[32mSET\x1B[39m ${`\x1B[33m${`currentPath[${$.length-1}]`}\x1B[39m`} = ${$[$.length-1]}`)))),!r&&typeof l!="number"&&(i||!i&&S!==null)&&typeof u=="number"&&u<n&&d===null&&(e[u]==='"'&&e[n]==='"'&&e[n-1]!=="\\"||e[u]!=='"'&&!e[n].trim()||["}",","].includes(e[n]))&&(t("451 catching the end of a value clauses"),T=e.slice(u,e[u]==='"'?n+1:n),t(`457 \x1B[32mSET\x1B[39m \x1B[33mkeyValue\x1B[39m = ${T}`),d=n,t(`461 \x1B[32mSET\x1B[39m \x1B[33mvalueEndedAt\x1B[39m = ${d}`)),!r&&!i&&e[n]==='"'&&e[n-1]!=="\\"&&S===null&&g===null&&E===null&&e[n+1]&&(g=n+1,t(`478 \x1B[32mSET\x1B[39m \x1B[33mkeyStartedAt\x1B[39m = ${g}`)),!r&&!i&&e[n]==='"'&&e[n-1]!=="\\"&&E===null&&typeof g=="number"&&u===null&&g<n&&(E=n+1,S=e.slice(g,n),t(`501 \x1B[32mSET\x1B[39m \x1B[33mkeyEndedAt\x1B[39m = ${E}; \x1B[33mkeyName\x1B[39m = ${S}`),$.push(S),t(`506 PUSH to path, now = ${JSON.stringify($,null,4)}`),J(y)===$.join(".")&&(r=!0,t(`515 \x1B[32mSET\x1B[39m \x1B[33mreplaceThisValue\x1B[39m = ${r}`))),!r&&typeof l!="number"&&e[n]===","&&c&&($.pop(),t(`535 POP(), now \x1B[33mcurrentPath\x1B[39m = ${JSON.stringify($,null,0)}`)),!r&&(typeof d=="number"&&n>=d||["}","]"].includes(e[V(e,n)])&&["}","]"].includes(e[n])||e[n]==="}"&&e[V(e,n)]==="{")&&e[n].trim()&&(t("552 \x1B[36m\u2588\u2588\x1B[39m catch the end of a key-value pair clauses"),e[n]===","&&!["}","]"].includes(e[P(e,n)])?(t("555 \x1B[31mRESET\x1B[39m"),A()):e[n]==="}"&&(t("558 closing curlie caught"),(d||e[V(e,n)]!=="{")&&($.pop(),t(`569 POP(), now \x1B[33mcurrentPath\x1B[39m = ${JSON.stringify($,null,0)}`)),t(`577 currently, currentlyWithinObject: ${c}`),t(`578 currently, currentlyWithinArray: ${i}`),o.length&&b.length&&o[o.length-1]>b[b.length-1]&&(c=!1,i=!0),t("616 \x1B[31mRESET\x1B[39m"),A())),!r&&e[n]==="{"&&O(S)&&u===null&&T===null&&(t("630 \x1B[31mRESET\x1B[39m"),A()),e[n].trim()&&r&&u===null&&typeof E=="number"&&n>E&&![":"].includes(e[n])&&(u=n,t(`644 \x1B[32mSET\x1B[39m \x1B[33mvalueStartedAt\x1B[39m = ${u}`)),e[n]==='"'&&f(e,n-1)&&(typeof g=="number"&&E===null||typeof u=="number"&&d===null)&&typeof l!="number"&&(l=n,t(`658 SET \x1B[33mwithinQuotesSince\x1B[39m = ${l}; withinQuotes = ${W()}`)),m.length&&e[n]===m[m.length-1]&&f(e,n-1)?(m.pop(),t(`677 \x1B[32mPOP\x1B[39m skipUntilTheFollowingIsMet = ${JSON.stringify(m,null,4)}`)):(typeof l!="number"||l===n)&&r&&!i&&typeof u=="number"&&(e[n]==="{"&&f(e,n-1)?(m.push("}"),t(`695 \x1B[32mPUSH\x1B[39m \x1B[33mskipUntilTheFollowingIsMet\x1B[39m = ${JSON.stringify(m,null,4)}`)):e[n]==="["&&f(e,n-1)?(m.push("]"),t(`705 \x1B[32mPUSH\x1B[39m \x1B[33mskipUntilTheFollowingIsMet\x1B[39m = ${JSON.stringify(m,null,4)}`)):e[n]==='"'&&f(e,n-1)&&(m.push('"'),t(`715 \x1B[32mPUSH\x1B[39m \x1B[33mskipUntilTheFollowingIsMet\x1B[39m = ${JSON.stringify(m,null,4)}`))),e[n]==='"'&&f(e,n-1)&&typeof l=="number"&&l!==n&&(l=void 0,t(`753 RESET \x1B[33mwithinQuotesSince\x1B[39m = ${l}; withinQuotes = ${W()}`)),r&&Array.isArray(m)&&!m.length&&typeof u=="number"&&n>u&&(t("766 within catch the end of the value when replaceThisValue is on clauses"),typeof l!="number"&&(e[u]==="["&&e[n]==="]"||e[u]==="{"&&e[n]==="}"||e[u]==='"'&&e[n]==='"'||!["[","{",'"'].includes(e[u])&&e[u].trim()&&(!e[n].trim()||w.includes(e[n])&&f(e,n-1))))){if(t(`780 INSIDE CATCH-END CLAUSES currently ${`\x1B[33m${`str[valueStartedAt=${u}]`}\x1B[39m`} = ${JSON.stringify(e[u],null,4)}`),N==="set"){t("789 \x1B[32mRETURN\x1B[39m");let a="";e.slice(u,n+(e[n].trim()?1:0)).includes(` `)&&e[n+(e[n].trim()?1:0)]!==` `&&(a=` `);let p=n+(e[n].trim()?1:0);return(i&&!['"',"[","{"].includes(e[u])&&e[P(e,p-1)]!=="]"||e[p-1]===","&&e[u-1]!=='"')&&(p-=1),i&&e[u-1]==='"'&&(u=u-1),`${e.slice(0,u)}${M(D)}${a}${e.slice(p)}`}if(N==="del"){t("848 \x1B[32mRETURN\x1B[39m"),t(`851 \x1B[33mkeyStartedAt\x1B[39m = ${JSON.stringify(g,null,4)}; val = ${(i?u:g)-1}`);let a=V(e,(i?u:g)-1);typeof a=="number"&&a++,t(`864 \x1B[32mSET\x1B[39m initial \x1B[33mstartingPoint\x1B[39m = ${a}`);let p=n+(e[n].trim()?1:0);typeof a=="number"&&e[a-1]===","&&["}","]"].includes(e[P(e,p-1)])&&(a-=1,t(`873 SET \x1B[33mstartingPoint\x1B[39m = ${a}`)),e[p]===","&&(p+=1,t(`879 SET \x1B[33mendingPoint\x1B[39m = ${p}`)),t(`883 \x1B[33mstartingPoint\x1B[39m = ${JSON.stringify(a,null,4)}; \x1B[33mendingPoint\x1B[39m = ${JSON.stringify(p,null,4)};`),j.push([a,p]),t(`896 \x1B[32mFINAL PUSH\x1B[39m \x1B[33mranges\x1B[39m = ${JSON.stringify(j,null,4)}`),t("902 then \x1B[31mBREAK\x1B[39m");break}}(c&&i||i&&typeof l!="number"&&e[n]==="]"&&f(e,n-1))&&(i=!1,c=!0),t(`${`\x1B[${l?32:31}m${`withinQuotesSince${typeof l=="number"?`=${l}`:""}`}\x1B[39m`}; ${`\x1B[${c?32:31}mcurrentlyWithinObject\x1B[39m`}; ${`\x1B[${i?32:31}mcurrentlyWithinArray\x1B[39m`}; ${`\x1B[${r?32:31}mreplaceThisValue\x1B[39m`}; ${`\x1B[${s?32:31}mitsTheFirstElem\x1B[39m`}; ${`\x1B[${m.length?32:31}m${`skipUntilTheFollowingIsMet${m?`: ${JSON.stringify(m,null,0)}`:""}`}\x1B[39m`}`),t(`current path: ${JSON.stringify($.join("."),null,0)}`),t(`\x1B[33mkeyName\x1B[39m = ${S}; \x1B[33mkeyValue\x1B[39m = ${T}; \x1B[33mkeyStartedAt\x1B[39m = ${g}; \x1B[33mkeyEndedAt\x1B[39m = ${E}; \x1B[33mvalueStartedAt\x1B[39m = ${u}; \x1B[33mvalueEndedAt\x1B[39m = ${d}`),t(`\x1B[33mwithinArrayIndexes\x1B[39m = ${JSON.stringify(o,null,0)}; \x1B[33mwithinObjectIndexes\x1B[39m = ${JSON.stringify(b,null,0)};`)}return t(` \x1B[36m=============================== FIN.\x1B[39m`),t(`947 RETURN applied ${JSON.stringify(v(e,j),null,4)}`),v(e,j)}function q(e,y,h){if(!O(e)||!e.length)throw new Error(`edit-package-json/set(): [THROW_ID_01] first input argument must be a non-empty string. It was given as ${JSON.stringify(e,null,4)} (type ${typeof e})`);return I({str:e,path:y,valToInsert:h,mode:"set"})}function z(e,y){if(!O(e)||!e.length)throw new Error(`edit-package-json/del(): [THROW_ID_02] first input argument must be a non-empty string. It was given as ${JSON.stringify(e,null,4)} (type ${typeof e})`);return I({str:e,path:y,mode:"del"})}export{z as del,q as set,_ as version};