path-sage
Version:
An awesome Object-path notation library
1 lines • 3.15 kB
JavaScript
var t,e,r,o;function n(t){return"object"!=typeof t||null===t}function l(t){if("object"!=typeof t||null===t)throw SyntaxError("Invalid Object Type")}function i(t,e){if(0===e.length){for(let e of Object.keys(t))delete t[e];return}for(let r=e.length;--r>0;)if(n(t=t[e[r]]))throw EvalError("Could not fully evaluate the object path");if(y(t)){let r=parseInt(e[0],10);if(isNaN(r))throw SyntaxError("key is not a Number");t.splice(r,1)}else delete t[e[0]]}function c(t,e){let r,o,n,l=Object.keys(t);if(0===l.length)return e?[e]:[];let i=[];for(r of l)o=t[r],n=""===e?r:f.test(r)?r.includes("'")?`${e}.["${r}"]`:`${e}.['${r}']`:Array.isArray(t)?`${e}[${r}]`:`${e}.${r}`,"object"==typeof o&&null!==o?i.push(...c(o,n)):i.push(n);return i}function a(n){return t.hasOwnProperty(n)?t[n].slice(0)||[]:(++r>o&&-1!==o&&(t={},r=1),y(n))?(t[n]=n.reverse()).slice(0):(!function(t){if("string"!=typeof t)throw TypeError("Invalid Notation Type");if(0!==t.length){if(!function(t){let e,r,o,n=0;for(r=0,o=t.length;r<o;r++)"["===(e=t[r])?n++:"]"===e&&n--;return 0===n}(t))throw SyntaxError("All brackets must be placed correctly");if(!function(t){let e,r,o,n;for(r=0,o=t.length;r<o;r++)("'"===(n=t[r])||'"'===n||"`"===n)&&(e?e===n&&(e=void 0):e=n);return void 0===e}(t))throw SyntaxError("All Quotes must be placed correctly")}}(n),(t[n]=(function(t,e){let r,o;let n=[],l=/\[\s*(\d+)(?=\s*])|\[\s*(["'`])((?:\\.|(?!\2).)*)\2\s*]|[\w$]+/g;for(;r=l.exec(t);)if(o=r[1]||r[3]||r[0],!s.has(o)){if(!e&&u.has(o))throw SyntaxError("Disallowed Key encountered");n.push(o)}if(!y(n))throw SyntaxError("Could not tokenize Notation");return n})(n,e).reverse()).slice(0))}"use strict";t={},e=!1,r=0,o=-1;const u=new Set(["this","__proto__","prototype","constructor"]),s=new Set(["['']",'[""]',"[``]",""]),f=/\.|\[|\]|\"|\'|\s/,h=Object.hasOwn||Object.call.bind(Object.hasOwnProperty),y=Array.isArray||function(t){return t&&t.constructor===Array};class p{static setProperty(t,e,r){l(t),function(t,e,r){if(0!==e.length){for(let r=e.length;--r>0;)if(n(t=t[e[r]]))throw EvalError("Could not fully evaluate the object path");t[e[0]]=r}}(t,a(e),r)}static getProperty(t,e){return l(t),function(t,e){if(0===e.length)return t;for(let r=e.length;--r>0;)if(n(t=t[e[r]]))throw EvalError("Could not fully evaluate the object path");return t[e[0]]}(t,a(e))}static hasProperty(t,e,r=!1){return l(t),function(t,e,r){if(0===e.length)return!0;for(let o=e.length,l,i;o-- >0;){if(!n(i=t[l=e[o]])&&0!==o||void 0!==i){t=i;continue}if(r)return{depth:e.length-o-1,left:++o,failedKey:l,currentObject:t};return!1}return!0}(t,a(e),r)}static removeProperty(t,e){l(t),i(t,a(e))}static deleteProperty(t,e){l(t),i(t,a(e))}static create(t={},e=""){l(t),function(t,e){if(0!==e.length){for(let r=e.length,o;--r>0;)n(t[o=e[r]])&&(t[o]={}),t=t[o];h(t,e=e[0])||(t[e]={})}}(t,a(e))}static keys(t){return l(t),c(t,"")}static getPaths(t){return l(t),c(t,"")}static clearCache(){t={},r=0}static configure(t={}){if(!("object"==typeof t&&!y(t)&&null!==t))throw TypeError("Invalid Options Type");"boolean"==typeof t.allowKeys&&(e=t.allowKeys);let r=parseInt(t.cacheSize,10);"number"==typeof r&&!isNaN(r)&&r>=-1&&(o=r)}}module.exports=p;