UNPKG

object-no-new-keys

Version:

Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)

11 lines (9 loc) 1.52 kB
/** * @name object-no-new-keys * @fileoverview Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON) * @version 5.1.9 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/object-no-new-keys/} */ import{hasOwnProp as y,isPlainObject as l}from"codsen-utils";var m="5.1.9";var O=m,b={mode:2};function x(u,d,p){if(p&&!l(p))throw new TypeError(`object-no-new-keys/noNewKeys(): [THROW_ID_02] resolvedOpts should be a plain object. It was given as ${JSON.stringify(p,null,4)} (type ${typeof p})`);let n={...b,...p};if(typeof n.mode=="string"&&["1","2"].includes(n.mode))n.mode=+n.mode;else if(![1,2].includes(n.mode))throw new TypeError('object-no-new-keys/objectNoNewKeys(): [THROW_ID_01] resolvedOpts.mode should be "1" or "2" (string or number).');function c(s,r,i,e){let o;if(l(s))l(r)?Object.keys(s).forEach(t=>{y(r,t)?(l(s[t])||Array.isArray(s[t]))&&(o={path:e.path.length?`${e.path}.${t}`:t,res:e.res},e.res=c(s[t],r[t],i,o).res):(o=e.path.length?`${e.path}.${t}`:t,e.res.push(o))}):e.res=e.res.concat(Object.keys(s).map(t=>e.path.length?`${e.path}.${t}`:t));else if(Array.isArray(s))if(Array.isArray(r))for(let t=0,a=s.length;t<a;t++)o={path:`${e.path.length?e.path:""}[${t}]`,res:e.res},i.mode===2?e.res=c(s[t],r[0],i,o).res:e.res=c(s[t],r[t],i,o).res;else e.res=e.res.concat(s.map((t,a)=>`${e.path.length?e.path:""}[${a}]`));return e}return c(u,d,n,{path:"",res:[]}).res}export{b as defaults,x as noNewKeys,O as version};