UNPKG

json-comb-core

Version:
14 lines (12 loc) 7.2 kB
/** * @name json-comb-core * @fileoverview The inner core of json-comb * @version 8.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/json-comb-core/} */ import{flattenAllArrays as O}from"object-flatten-all-arrays";import{setAllValuesTo as N}from"object-set-all-values-to";import{fillMissing as K}from"object-fill-missing-keys";import{mergeAdvanced as $}from"object-merge-advanced";import{noNewKeys as R}from"object-no-new-keys";import{existy as j,isStr as A,isPlainObject as l}from"codsen-utils";import C from"semver-compare";import{includes as E}from"lodash-es";import D from"rfdc";import H from"sort-keys";import W from"p-reduce";import x from"type-detect";import F from"p-one";var I="8.1.0";var h=D(),se=I;function v(e){if(e===null)return"null";if(typeof e=="boolean"||typeof e=="number")return e.toString();if(typeof e=="string")return e;if(typeof e=="symbol")throw new TypeError;return`${e}`}function J(e,t){if(e===void 0&&t===void 0)return 0;if(e===void 0)return 1;if(t===void 0)return-1;let o=v(e),u=v(t);return o<u?-1:o>u?1:0}function k(e,t){let o=/^\d+\.\d+\.\d+$/g;return e.match(o)&&t.match(o)?C(e,t):J(e,t)}function T(e){return l(e)||Array.isArray(e)?H(e,{deep:!0,compare:k}):e}function ie(e,t){if(arguments.length===0)throw new Error("json-comb-core/getKeyset(): [THROW_ID_11] Inputs missing!");if(j(t)&&!l(t))throw new TypeError(`json-comb-core/getKeysetSync(): [THROW_ID_12] Options object must be a plain object! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);let u={...{placeholder:!1},...t},s,c;return new Promise((f,r)=>{F(e,(n,i)=>l(n)?!1:(s=i,c=n,!0)).then(n=>{if(n){r(Error(`json-comb-core/getKeyset(): [THROW_ID_13] Oops! ${s}th element resolved not to a plain object but to a ${typeof c} ${JSON.stringify(c,null,4)}`));return}return W(e,(i,g)=>$(O(i,{flattenArraysContainingStringsToBeEmpty:!0}),O(g,{flattenArraysContainingStringsToBeEmpty:!0}),{mergeArraysContainingStringsToBeEmpty:!0}),{}).then(i=>{f(N(i,u.placeholder))})})})}function q(e,t){if(arguments.length===0)throw new Error("json-comb-core/getKeysetSync(): [THROW_ID_21] Inputs missing!");if(!Array.isArray(e))throw new Error(`json-comb-core/getKeysetSync(): [THROW_ID_22] Input must be array! Currently it's: ${typeof e}`);if(e.length===0)throw new Error("json-comb-core/getKeysetSync(): [THROW_ID_23] Input array is empty!");if(j(t)&&!l(t))throw new TypeError(`json-comb-core/getKeysetSync(): [THROW_ID_24] Options object must be a plain object! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);let o={},u=h(e),c={...{placeholder:!1},...t},f={flattenArraysContainingStringsToBeEmpty:!0};return u.forEach((r,n)=>{if(!l(r))throw new TypeError(`json-comb-core/getKeysetSync(): [THROW_ID_25] Non-object (${typeof r}) detected within an array! It's the ${n}th element: ${JSON.stringify(r,null,4)}`);o=$(O(o,f),O(r,f),{mergeArraysContainingStringsToBeEmpty:!0})}),o=T(N(o,c.placeholder)),o}function le(e,t,o){if(arguments.length===0)throw new Error("json-comb-core/enforceKeyset(): [THROW_ID_31] Inputs missing!");if(arguments.length===1)throw new Error("json-comb-core/enforceKeyset(): [THROW_ID_32] Second arg missing!");let s={...{doNotFillThesePathsIfTheyContainPlaceholders:[],placeholder:!1,useNullAsExplicitFalse:!0},...o};if(s.doNotFillThesePathsIfTheyContainPlaceholders.length&&!s.doNotFillThesePathsIfTheyContainPlaceholders.every(c=>A(c)))throw new Error(`json-comb-core/enforceKeyset(): [THROW_ID_33] Array resolvedOpts.doNotFillThesePathsIfTheyContainPlaceholders contains non-string values: ${JSON.stringify(s.doNotFillThesePathsIfTheyContainPlaceholders,null,4)}`);return new Promise((c,f)=>{if(!l(e)){f(Error(`json-comb-core/enforceKeyset(): [THROW_ID_34] Input must resolve to a plain object! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`));return}if(!l(t)){f(Error(`json-comb-core/enforceKeyset(): [THROW_ID_35] Schema, 2nd arg, must resolve to a plain object! Currently it's resolving to: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`));return}c(T(h(K(h(e),h(t),s))))})}function ce(e,t,o){if(arguments.length===0)throw new Error("json-comb-core/enforceKeysetSync(): [THROW_ID_41] Inputs missing!");if(arguments.length===1)throw new Error("json-comb-core/enforceKeysetSync(): [THROW_ID_42] Second arg missing!");if(!l(e))throw new Error(`json-comb-core/enforceKeysetSync(): [THROW_ID_43] Input must be a plain object! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(!l(t))throw new Error(`json-comb-core/enforceKeysetSync(): [THROW_ID_44] Schema object must be a plain object! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);let s={...{doNotFillThesePathsIfTheyContainPlaceholders:[],placeholder:!1,useNullAsExplicitFalse:!0},...o};if(s.doNotFillThesePathsIfTheyContainPlaceholders.length&&!s.doNotFillThesePathsIfTheyContainPlaceholders.every(c=>A(c)))throw new Error(`json-comb-core/enforceKeyset(): [THROW_ID_45] Array resolvedOpts.doNotFillThesePathsIfTheyContainPlaceholders contains non-string values: ${JSON.stringify(s.doNotFillThesePathsIfTheyContainPlaceholders,null,4)}`);return T(K(h(e),t,s))}function ae(e,t){if(arguments.length===0)throw new Error("json-comb-core/noNewKeysSync(): [THROW_ID_51] All inputs missing!");if(arguments.length===1)throw new Error("json-comb-core/noNewKeysSync(): [THROW_ID_52] Schema object is missing!");if(!l(e))throw new Error(`json-comb-core/noNewKeysSync(): [THROW_ID_53] Main input (1st arg.) must be a plain object! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(!l(t))throw new Error(`json-comb-core/noNewKeysSync(): [THROW_ID_54] Schema input (2nd arg.) must be a plain object! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);return R(e,t)}function ue(e,t){if(Array.isArray(e)){if(e.length===0)return[]}else throw new TypeError(`json-comb-core/findUnusedSync(): [THROW_ID_61] The first argument should be an array. Currently it's: ${typeof e}`);if(arguments.length>1&&!l(t))throw new TypeError(`json-comb-core/findUnusedSync(): [THROW_ID_62] The second argument, options object, must be a plain object, not ${typeof t}`);let u={...{placeholder:!1,comments:"__comment__"},...t};u.comments||(u.comments="");let s=h(e);function c(r){return r.map(n=>n.charAt(0)==="."?n.slice(1):n)}function f(r,n,i=[],g=""){if(Array.isArray(r)&&r.length===0)return i;let d;if(r.every(m=>l(m))){if(d=q(r),r.length>1){let a=Object.keys(d).filter(y=>r.every(p=>(n&&p[y]===n.placeholder||p[y]===void 0)&&(!n?.comments||!E(y,n.comments))));i=i.concat(a.map(y=>`${g}.${y}`))}let m=[].concat(...Object.keys(d).filter(a=>l(d[a])||Array.isArray(d[a]))),b=m.map(a=>x(d[a])),w=m.map(a=>[].concat(...r.reduce((y,p)=>(p&&j(p[a])&&(!n||p[a]!==n.placeholder)&&(!n?.comments||!E(p[a],n.comments))&&y.push(p[a]),y),[]))),S="",_="";w.length&&w.forEach((a,y)=>{b[y]==="Array"&&(S=`[${y}]`),_=".",i=f(a,n,i,g+_+m[y]+S)})}else r.every(m=>Array.isArray(m))&&r.forEach((m,b)=>{i=f(m,n,i,`${g}[${b}]`)});return c(i)}return f(s,u)}export{le as enforceKeyset,ce as enforceKeysetSync,ue as findUnusedSync,ie as getKeyset,q as getKeysetSync,ae as noNewKeysSync,T as sortAllObjectsSync,se as version};