@dream2023/data-mapping
Version:
@dream2023/data-mapping is a perfect object mapping solution。
2 lines (1 loc) • 6.53 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).dataMapping={})}(this,(function(e){"use strict";function t(e,t){var n=Object.keys(t),r=Object.keys(e);return n.forEach((function(n){r.includes(n)||(e[n]=t[n])})),e}function n(e){return"[object Object]"===Object.prototype.toString.call(e.$)&&delete(e=t(e,e.$)).$,e}var r=function(e){return null==e};function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function c(e){for(var t=1;arguments.length>t;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){o(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}var a,u={};function f(){return u}function s(){return a}var l=/[\w).+\-_$\]]/;function p(e){var t,n,r,o,i,c=!1,a=!1,u=!1,f=!1,s=0,p=0,y=0,b=0;for(r=0;e.length>r;r++)if(n=t,t=e.charCodeAt(r),c)39===t&&92!==n&&(c=!1);else if(a)34===t&&92!==n&&(a=!1);else if(u)96===t&&92!==n&&(u=!1);else if(f)47===t&&92!==n&&(f=!1);else if(124!==t||124===e.charCodeAt(r+1)||124===e.charCodeAt(r-1)||s||p||y){switch(t){case 34:a=!0;break;case 39:c=!0;break;case 96:u=!0;break;case 40:y++;break;case 41:y--;break;case 91:p++;break;case 93:p--;break;case 123:s++;break;case 125:s--}if(47===t){for(var h=r-1,v=void 0;h>=0&&" "===(v=e.charAt(h));h--);v&&l.test(v)||(f=!0)}}else void 0===o?(b=r+1,o=e.slice(0,r).trim()):g();function g(){(i||(i=[])).push(e.slice(b,r).trim()),b=r+1}if(void 0===o?o=e.slice(0,r).trim():0!==b&&g(),i)for(r=0;i.length>r;r++)o=d(o,i[r]);return o}function d(e,t){var n=t.indexOf("(");if(0>n)return"".concat(t,"(").concat(e,")");var r=t.slice(0,n),o=t.slice(n+1);return"".concat(r,"(").concat(e).concat(")"!==o?","+o:o)}var y=/\{\{((?:.|\r?\n)+?)\}\}/g,b=/[-.*+?^${}()|[\]/\\]/g;function h(e,t){var n=t?function(e){var t=e[0].replace(b,"\\$&"),n=e[1].replace(b,"\\$&");return RegExp(t+"((?:.|\\n)+?)"+n,"g")}(t):y;if(!n.test(e))return'"'.concat(e,'"');for(var r,o,i,c=[],a=[],u=n.lastIndex=0;r=n.exec(e);){(o=r.index)>u&&(a.push(i=e.slice(u,o)),c.push(JSON.stringify(i)));var f=p(r[1].trim());c.push(f),u=o+r[0].length}return e.length>u&&(i=e.slice(u),c.push(JSON.stringify(i))),c.join(" + ")}var v,g="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function O(e){if(Object.keys)return Object.keys(e);var t=[];for(var n in e)t.push(n);return t}function m(e,t){if(e.forEach)return e.forEach(t);for(var n=0;e.length>n;n++)t(e[n],n,e)}function w(){try{return Object.defineProperty({},"_",{}),function(e,t,n){Object.defineProperty(e,t,{writable:!0,enumerable:!1,configurable:!0,value:n})}}catch(e){return function(e,t,n){e[t]=n}}}var x=["Array","Boolean","Date","Error","EvalError","Function","Infinity","JSON","Math","NaN","Number","Object","RangeError","ReferenceError","RegExp","String","SyntaxError","TypeError","URIError","decodeURI","decodeURIComponent","encodeURI","encodeURIComponent","escape","eval","isFinite","isNaN","parseFloat","parseInt","undefined","unescape"];function j(){}function E(e){if(!(this instanceof E))return new E(e);this.code=e}function C(e){if(_(e))return e;var t=new j;return"object"==typeof e&&m(O(e),(function(n){t[n]=e[n]})),t}function _(e){return e instanceof j}j.prototype={},E.prototype.runInContext=function(e){if(!(e instanceof j))throw new TypeError("needs a 'context' argument.");if(g.document){var t=g.document.createElement("iframe");t.style||(t.style={}),t.style.display="none",g.document.body.appendChild(t);var n=t.contentWindow,r=n.eval,o=n.execScript;!r&&o&&(o.call(n,"null"),r=n.eval),m(O(e),(function(t){n[t]=e[t]})),m(x,(function(t){e[t]&&(n[t]=e[t])}));var i=O(n),c=r.call(n,this.code);return m(O(n),(function(t){(t in e||-1===function(e,t){if(k)return e.indexOf(t);for(var n=0;e.length>n;++n)if(e[n]===t)return n;return-1}(i,t))&&(e[t]=n[t])})),m(x,(function(t){t in e||("function"!=typeof v&&(v=w),v(e,t,n[t]))})),g.document.body.removeChild(t),c}return function(e,t){var n=O(g);return n.push("with (this.__ctx__){return eval(this.__code__)}"),Function.apply(null,n).apply({__code__:e,__ctx__:t})}(this.code,e)},E.prototype.runInThisContext=function(){return Function("code","return eval(code);").call(g,this.code)},E.prototype.runInNewContext=function(e){var t=C(e),n=this.runInContext(t);return e&&m(O(t),(function(n){e[n]=t[n]})),n};var I={runInContext:function(e,t,n){return new E(e,n).runInContext(t,n)},isContext:_,createContext:C,createScript:function(e){return new E(e)},Script:E,runInThisContext:function(e,t){return new E(e,t).runInThisContext(t)},runInNewContext:function(e,t,n){return new E(e,n).runInNewContext(t,n)}},k=[].indexOf;const N=e=>"object"==typeof e&&null!==e,P=e=>N(e)&&!(e instanceof RegExp)&&!(e instanceof Error)&&!(e instanceof Date),S=(e,t,n,r=new WeakMap)=>{if(n={deep:!1,target:{},...n},r.has(e))return r.get(e);r.set(e,n.target);const{target:o}=n;delete n.target;const i=e=>e.map((e=>P(e)?S(e,t,n,r):e));if(Array.isArray(e))return i(e);for(const[c,a]of Object.entries(e)){let[u,f]=t(c,a,e);n.deep&&P(f)&&(f=Array.isArray(f)?i(f):S(f,t,n,r)),o[u]=f}return o};function F(e,t,n){if(r(e)||r(t))return e;var o=h(e,n||s());return o==='"'.concat(e,'"')?e:I.runInNewContext(o,c(c({},t),f()))}e.clearDelimiters=function(){a=void 0},e.clearFilters=function(){u={}},e.compilerStr=F,e.dataMapping=function(e){var t=e.schema,o=e.data,i=e.delimiters,c=e.defaultValue,a=void 0===c?"schema-data":c;if(r(o)||r(t))switch(a){case"schema":return t;case"data":return o;default:return t||o}return"string"==typeof t?F(t,o,i):"function"==typeof t?t(o):n(((e,t,n)=>{if(!N(e))throw new TypeError(`Expected an object, got \`${e}\` (${typeof e})`);return S(e,t,n)})(t,(function(e,t){return"string"==typeof t?[e,F(t,o,i)]:"function"==typeof t?[e,t(o)]:[e,t]}),{deep:!0}))},e.getDelimiters=s,e.getFilters=f,e.isNil=r,e.mergeObj=t,e.parseFilters=p,e.parseText=h,e.replace$=n,e.setDelimiters=function(e){a=e},e.setFilter=function(e,t){u[e]=t},e.setFilters=function(e){u=c(c({},u),e)},Object.defineProperty(e,"__esModule",{value:!0})}));