UNPKG

@ray-js/tuya-dp-transform

Version:

## 支持的协议类型

3 lines (2 loc) 15.7 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t="object"==typeof global&&global&&global.Object===Object&&global,e="object"==typeof self&&self&&self.Object===Object&&self,n=t||e||Function("return this")(),r=n.Symbol,o=Object.prototype,i=o.hasOwnProperty,u=o.toString,a=r?r.toStringTag:void 0,s=Object.prototype.toString,c=r?r.toStringTag:void 0;function f(t){{if(null==t)return void 0===t?"[object Undefined]":"[object Null]";if(c&&c in Object(t)){var e=t,r=i.call(e,a),n=e[a];try{var o=!(e[a]=void 0)}catch(e){}var f=u.call(e);return o&&(r?e[a]=n:delete e[a]),f}return s.call(t)}}function l(t){return"symbol"==typeof t||null!=t&&"object"==typeof t&&"[object Symbol]"==f(t)}var p=Array.isArray,h=r?r.prototype:void 0,d=h?h.toString:void 0;function y(t){if("string"==typeof t)return t;if(p(t))return function(t,e){for(var r=-1,n=null==t?0:t.length,o=Array(n);++r<n;)o[r]=e(t[r],r,t);return o}(t,y)+"";if(l(t))return d?d.call(t):"";var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}var g=/\s/,v=/^\s+/;function m(t){return t&&t.slice(0,function(t){for(var e=t.length;e--&&g.test(t.charAt(e)););return e}(t)+1).replace(v,"")}function b(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}var _=/^[-+]0x[0-9a-f]+$/i,x=/^0b[01]+$/i,j=/^0o[0-7]+$/i,w=parseInt;function O(t){return t?1/0===(t=function(t){if("number"==typeof t)return t;if(l(t))return NaN;if("string"!=typeof(t=b(t)?b(e="function"==typeof t.valueOf?t.valueOf():t)?e+"":e:t))return 0===t?t:+t;t=m(t);var e=x.test(t);return e||j.test(t)?w(t.slice(2),e?2:8):_.test(t)?NaN:+t}(t))||-1/0===t?17976931348623157e292*(t<0?-1:1):t==t?t:0:0===t?t:0}var T,A=n["__core-js_shared__"],N=(T=/[^.]+$/.exec(A&&A.keys&&A.keys.IE_PROTO||""))?"Symbol(src)_1."+T:"",$=Function.prototype.toString,M=/^\[object .+?Constructor\]$/,S=Function.prototype,E=Object.prototype,I=S.toString,k=E.hasOwnProperty,z=RegExp("^"+I.call(k).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function B(t){return!(!b(t)||N&&N in t)&&(function(t){if(b(t))return t=f(t),"[object Function]"==t||"[object GeneratorFunction]"==t||"[object AsyncFunction]"==t||"[object Proxy]"==t}(t)?z:M).test(function(t){if(null!=t){try{return $.call(t)}catch(t){}try{return t+""}catch(t){}}return""}(t))}function L(t,e){t=null==t?void 0:t[e];return B(t)?t:void 0}var P=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,R=/^\w*$/,C=L(Object,"create"),F=Object.prototype.hasOwnProperty,D=Object.prototype.hasOwnProperty;function q(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function U(t,e){for(var r,n=t.length;n--;)if((r=t[n][0])===e||r!=r&&e!=e)return n;return-1}q.prototype.clear=function(){this.__data__=C?C(null):{},this.size=0},q.prototype.delete=function(t){t=this.has(t)&&delete this.__data__[t];return this.size-=t?1:0,t},q.prototype.get=function(t){var e,r=this.__data__;return C?"__lodash_hash_undefined__"===(e=r[t])?void 0:e:F.call(r,t)?r[t]:void 0},q.prototype.has=function(t){var e=this.__data__;return C?void 0!==e[t]:D.call(e,t)},q.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=C&&void 0===e?"__lodash_hash_undefined__":e,this};var Y=Array.prototype.splice;function G(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}G.prototype.clear=function(){this.__data__=[],this.size=0},G.prototype.delete=function(t){var e=this.__data__,t=U(e,t);return!(t<0||(t==e.length-1?e.pop():Y.call(e,t,1),--this.size,0))},G.prototype.get=function(t){var e=this.__data__,t=U(e,t);return t<0?void 0:e[t][1]},G.prototype.has=function(t){return-1<U(this.__data__,t)},G.prototype.set=function(t,e){var r=this.__data__,n=U(r,t);return n<0?(++this.size,r.push([t,e])):r[n][1]=e,this};var K=L(n,"Map");function V(t,e){var r,t=t.__data__;return("string"==(r=typeof e)||"number"==r||"symbol"==r||"boolean"==r?"__proto__"!==e:null===e)?t["string"==typeof e?"string":"hash"]:t.map}function W(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function H(n,o){if("function"!=typeof n||null!=o&&"function"!=typeof o)throw new TypeError("Expected a function");function a(){var t=arguments,e=o?o.apply(this,t):t[0],r=a.cache;return r.has(e)?r.get(e):(t=n.apply(this,t),a.cache=r.set(e,t)||r,t)}return a.cache=new(H.Cache||W),a}W.prototype.clear=function(){this.size=0,this.__data__={hash:new q,map:new(K||G),string:new q}},W.prototype.delete=function(t){t=V(this,t).delete(t);return this.size-=t?1:0,t},W.prototype.get=function(t){return V(this,t).get(t)},W.prototype.has=function(t){return V(this,t).has(t)},W.prototype.set=function(t,e){var r=V(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this},H.Cache=W;var J,Q,X,Z=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,tt=/\\(\\)?/g,et=(X=(Q=H(J=function(t){var o=[];return 46===t.charCodeAt(0)&&o.push(""),t.replace(Z,function(t,e,r,n){o.push(r?n.replace(tt,"$1"):e||t)}),o},function(t){return 500===X.size&&X.clear(),t})).cache,Q),nt=Q;function rt(t){return null==t?"":y(t)}function ot(t,e){return p(t)?t:function(t,e){var r;if(!p(t))return r=typeof t,"number"==r||"symbol"==r||"boolean"==r||null==t||l(t)||R.test(t)||!P.test(t)||null!=e&&t in Object(e)}(t,e)?[t]:nt(rt(t))}function it(t){if("string"==typeof t||l(t))return t;var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}function ut(t,o,e){t=null==t?void 0:function(t,e){for(var r=0,n=(e=ot(o,t)).length;null!=t&&r<n;)t=t[it(e[r++])];return r&&r==n?t:void 0}(t);return void 0===t?e:t}function at(t,e,r){var n=t.length;if(r=void 0===r?n:r,!e&&n<=r)return t;var o=t,a=e,n=r,u=-1,i=o.length;(n=i<n?i:n)<0&&(n+=i),i=n<(a=a<0?i<-a?0:i+a:a)?0:n-a>>>0,a>>>=0;for(var s=Array(i);++u<i;)s[u]=o[u+a];return s}var st=RegExp("[\\u200d\\ud800-\\udfff\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff\\ufe0e\\ufe0f]");function ct(t){return st.test(t)}var ft="[\\ud800-\\udfff]",lt="[\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff]",pt="\\ud83c[\\udffb-\\udfff]",ht="[^\\ud800-\\udfff]",dt="(?:\\ud83c[\\udde6-\\uddff]){2}",yt="[\\ud800-\\udbff][\\udc00-\\udfff]",gt="(?:"+lt+"|"+pt+")?",vt="[\\ufe0e\\ufe0f]?"+gt+"(?:\\u200d(?:"+[ht,dt,yt].join("|")+")[\\ufe0e\\ufe0f]?"+gt+")*",mt="(?:"+[ht+lt+"?",lt,dt,yt,ft].join("|")+")",bt=RegExp(pt+"(?="+pt+")|"+mt+vt,"g");function _t(t){return ct(t)?t.match(bt)||[]:t.split("")}var xt=Math.floor;function jt(t,e){var r="";if(!t||e<1||9007199254740991<e)return r;for(;e%2&&(r+=t),(e=xt(e/2))&&(t+=t),e;);return r}wt="length";var wt,Ot=function(t){return null==t?void 0:t[wt]},Tt="[\\ud800-\\udfff]",At="[\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff]",Nt="\\ud83c[\\udffb-\\udfff]",$t="[^\\ud800-\\udfff]",Mt="(?:\\ud83c[\\udde6-\\uddff]){2}",St="[\\ud800-\\udbff][\\udc00-\\udfff]",Et="(?:"+At+"|"+Nt+")?",It="[\\ufe0e\\ufe0f]?"+Et+"(?:\\u200d(?:"+[$t,Mt,St].join("|")+")[\\ufe0e\\ufe0f]?"+Et+")*",kt="(?:"+[$t+At+"?",At,Mt,St,Tt].join("|")+")",zt=RegExp(Nt+"(?="+Nt+")|"+kt+It,"g");function Bt(t){return(ct(t)?function(t){for(var e=zt.lastIndex=0;zt.test(t);)++e;return e}:Ot)(t)}var Lt=Math.ceil;function Pt(t,e,r){t=rt(t),n=O(e),o=n%1;var n,o=(e=n==n?o?n-o:n:0)?Bt(t):0;return e&&o<e?function(t,e){var r=(e=void 0===e?" ":y(e)).length;if(r<2)return r?jt(e,t):e;r=jt(e,Lt(t/Bt(e)));return ct(e)?at(_t(r),0,t).join(""):r.slice(0,t)}(e-o,r)+t:t}const Rt=[],Ct=t=>{Rt.push(t)},Ft=t=>{t=Rt.indexOf(t);0<=t&&Rt.splice(t,1)},Dt=e=>{Rt.forEach(t=>{t(e)})},qt=(t,e)=>{var{defaultValue:r,type:n="number",extension:o}=t;if(void 0!==r)return r;var a=2*Ut(t,e);switch(n){case"number":case"signedNumber":case"numberBySeparate":if(o){const t=o["min"];return void 0===t?0:t}return 0;case"string":return Pt("00",a,"0");case"boolean":return!1;case"enum":if(o){const t=o["range"];return Array.isArray(t)&&t.length?t[0]:0}return 0;case"week":return[0,0,0,0,0,0,0];case"bit":return[...new Array(a).keys()].map(()=>0);case"child":return{};default:return}},Ut=(t,e)=>{var{bytes:t=1}=t;let r=t;return(r=!Array.isArray(t)&&"string"!=typeof t?r:ut(e,t,1))||1},Yt=(t,e,r)=>{var n=e["extension"],r=t.nextNumber(e,r);if(n){const{min:t=-1/0,max:e=1/0}=n;return Math.max(Math.min(r,e),t)}return r},Gt=(t,e,r)=>t.nextBits(e,r);class Kt{constructor(t,e){this.code=t,this.msg=e}}Kt.ALLREADY_TO_END="endError",Kt.UNKOWN_CONDITION="unkownConditionError";const Vt=(a,t,e)=>{var{operation:r="eq",value:n}=a,o=((t,e)=>{var r,{findType:n,prop:o}=a;switch(n){case"root":return ut(t,o);case"current":return ut(e,o);default:return null!=(r=ut(t,o))?r:ut(e,o)}})(t,e);switch(r){case"eq":return o===n;case"neq":return o!==n;case"lt":return o<n;case"lte":return o<=n;case"gt":return n<o;case"gte":return n<=o;default:return!1}},Wt=(t,e,r)=>{if(t){if("conditions"in t){const{conditions:n,type:o}=t;return"or"===o?n.some(t=>Wt(t,e,r)):n.every(t=>Wt(t,e,r))}return Vt(t,e,r)}return!0},Ht={number:Yt,signedNumber:(t,e,r)=>{var n=e["extension"],o=Ut(e,r),t=t.next(e,r);let a=parseInt(t,16);if(a>>8*o-1){let t=0;switch(o){case 1:t=127;break;case 2:t=32767;break;case 3:t=8388607;break;case 4:t=-1;break;default:console.warn("有符号数值最大支持32位数值,无法表示")}a=-1*(1+(a&t^t)),console.log("value",a,t)}if(n){const{min:t=-1/0,max:e=1/0}=n;return Math.max(Math.min(a,e),t)}return a},string:(t,e,r)=>t.next(e,r),boolean:(t,e,r)=>t.nextBoolean(e,r),bit:Gt,bits:(t,e,r)=>{const{numType:n="hex",bitType:o="right"}=e,a=t.next(e,r),u="dec"===n?10:16,i=[];for(let t=0;t<a.length;t+=2){const e=Pt(parseInt(a.slice(t,t+2),u).toString(2),8,"0").split("").map(t=>Number(t));i.push(...e)}return"right"===o&&i.reverse(),i},week:(t,e,r)=>Gt(t,e,r).slice(0,7),enum:(t,e,r)=>{var{extension:n,name:o}=e,e=t.nextNumber(e,r);if(n){const t=n["range"];if(Array.isArray(t)&&t.length)return t.includes(e)?e:(Dt({type:"warning",message:`解析到属性【${o}】时,得到的值${e}未在可枚举范围内`,data:{[o]:e}}),t[0])}return e},custom:(t,e,r)=>{const{parse:n,name:o}=e,a=t.next(e,r),u=parseInt(a,16);return"function"==typeof n?n(u,a):(Dt({type:"warning",message:`解析自定义类型属性【${o}】时,检测到未配置 parse 函数`,data:{[o]:u}}),u)},numberBySeparate:(t,e,r)=>{var{extension:n,numType:o="hex"}=e;if(1===Ut(e,r))return t.nextNumber(e,r);const a=t.next(e,r);let u=0;for(let t=0;t<a.length;t+=2)u+=parseInt(a.slice(t,t+2),"dec"===o?10:16)*Math.pow(10,a.length-2-t);if(n){const{min:t=-1/0,max:e=1/0}=n;return Math.max(Math.min(u,e),t)}return u},default:Yt},Jt=(r,t,n)=>{const e=t["childMap"],o={};return e.forEach(t=>{const e=t["condition"];if(Wt(e,n,o)){const e=Qt(r,t,o,n);o[t.name]=e}}),o},Qt=(e,r,n,o)=>{var{type:a="number",loop:u=!1,limit:t,name:i}=r;let s=1;u&&(s=t?"string"==typeof t||Array.isArray(t)?ut(n,t,0):t:1/0);const c=[];for(let t=0;t<s;t++){if(!e.hasNext()){c.length||"child"===a||c.push(qt(r,n)),0===t&&Dt({type:"warning",message:`解析到属性【${i}】时,发现已无可解析的数据,属性返回默认值`,data:{[i]:u?c:c[0]}});break}var f="child"===a?Jt(e,r,o):"function"==typeof Ht[a]?Ht[a](e,r,n):Ht.default(e,r,n);c.push(f)}return u?c:c[0]},Xt=(t,e,n)=>{var r;if(("00"===e||!e)&&"child"===(null==(r=t[0])?void 0:r.type)&&null!=(r=t[0])&&r.loop)return[];const o=(t=>{const u=function*(t){let e,r=0,n="";for(;e=2*(yield n),n=t.substr(r,e),!(r+e>=t.length);)r+=e;return n}(t);u.next();let i=0===t.length;const s=(e,t)=>{if(i)throw new Kt(Kt.ALLREADY_TO_END,"已经解析到末尾,无法再解析");const{storageType:r="be"}=e,n=Ut(e,t),{value:o,done:a}=u.next(n);if(i=a,"le"!==r)return o;{const e=[];for(let t=0;t<o.length;t+=2)e.splice(0,0,o.slice(t,t+2));return e.join("")}},r=(t,e)=>{var{numType:r="hex"}=t;return parseInt(s(t,e),"dec"===r?10:16)};return{next:s,nextNumber:r,nextBoolean:(t,e)=>Boolean(r(t,e)),nextBits:(t,e)=>{const{numType:r="hex",bitType:n="right"}=t,o=s(t,e),a="dec"===r?10:16,u=[];for(let t=0;t<o.length;t+=2){const e=Pt(parseInt(o.slice(t,t+2),a).toString(2),8,"0").split("").map(t=>Number(t));"right"===n&&e.reverse(),u.push(...e)}return u},hasNext:()=>!i}})(e);return t.forEach(t=>{const{name:e,condition:r}=t;if(Wt(r,n,n)){const r=Qt(o,t,n,n);n[e]=r}}),n},Zt=(t,e)=>Xt(e,t,{}),te=(t,e=1)=>Pt(Number(t).toString(16),2*e,"0"),ee=(t,e=1)=>Pt(Number(t).toString(),2*e,"0"),ne=(t,e,r)=>("dec"===r?ee:te)(t,e),re=(e,r)=>{if("le"!==r)return e;{const r=[];for(let t=0;t<e.length;t+=2)r.splice(0,0,e.slice(t,t+2));return r.join("")}},oe=(t,e,r)=>{var{extension:n,numType:o="hex",storageType:a="be"}=e,e=Ut(e,r);let u=t;if(n){const{min:t=-1/0,max:e=1/0}=n;u=Math.max(Math.min(u,e),t)}return re(ne(u,e,o),a)},ie=(e,t,r)=>{const{storageType:n="be",numType:o="hex",bitType:a="right"}=t,u=Ut(t,r),i=[];for(let t=0;t<u;t++){const r=8*t,n=8*(t+1),u=e.slice(r,n);if(u.length<8){const e=8-u.length;for(let t=0;t<e;t++)u.push(0)}"right"===a&&u.reverse();var s=parseInt(u.join(""),2);i.push(ne(s,1,o))}return re(i.join(""),n)},ue=(t,e,r)=>{var{storageType:n="be",name:o}=e,e=Ut(e,r),r=2*e;let a=t;return t.length>r?(Dt({type:"warning",message:`属性【${o}】转化后的值${t}长度过长,超过${e}个字节`,data:{[o]:t}}),a=t.slice(0,r)):t.length<r&&(Dt({type:"warning",message:`属性【${o}】转化后的值${t}长度过短,少于${e}个字节`,data:{[o]:t}}),a=Pt(t,r,"0")),re(a,n)},ae={number:oe,signedNumber:(t,e,r)=>{var{extension:n,storageType:o="be"}=e,e=Ut(e,r);let a=t;if(n){const{min:t=-1/0,max:e=1/0}=n;a=Math.max(Math.min(a,e),t)}if(a<0){let t=0;switch(e){case 1:t=255;break;case 2:t=65535;break;case 3:t=16777215;break;case 4:t=4294967295;break;default:console.warn("有符号数值最大支持32位数值,无法表示")}a=1+((a=-a)&t>>1^t)}return re(ne(a,e,"hex"),o)},string:ue,boolean:(t,e,r)=>oe(Number(t),e,r),bit:ie,bits:(e,t,r)=>{const{storageType:n="be",numType:o="hex",bitType:a="right"}=t,u=Ut(t,r),i=[],s=8*u;if((e=[...e]).length%8!=s)for(let t=e.length;t<s;t++)e.push(0);"right"===a&&e.reverse();for(let t=0;t<u;t++){const r=8*t,n=8*(t+1),a=e.slice(r,n),u=parseInt(a.join(""),2);i.push(ne(u,1,o))}return re(i.join(""),n)},week:(t,e,r)=>ie([...t,0],e,r),enum:(t,e,r)=>{var{extension:n,name:o}=e;let a=t;if(n){const e=n["range"];Array.isArray(e)&&e.length&&(a=e.includes(t)?t:(Dt({type:"warning",message:`属性【${o}】的值${t}不在可枚举范围内`,data:{[o]:t}}),e[0]))}return oe(a,e,r)},custom:(t,e,r)=>{const{format:n,name:o}=e;let a=null==t?void 0:t.toString();return"function"==typeof n?a=n(t):Dt({type:"warning",message:`属性【${o}】为自定义类型,检测到未配置 format 处理函数`,data:{[o]:t}}),ue(a,e,r)},numberBySeparate:(t,e,r)=>{const{extension:n,numType:o="hex",storageType:a="be"}=e,u=Ut(e,r);let i=t%Math.pow(10,2*u);if(n){const{min:t=-1/0,max:e=1/0}=n;i=Math.max(Math.min(i,e),t)}const s=[];for(let t=0;t<u;t++){const e=Math.pow(10,2*(u-1-t)),r=Math.floor(i/e),n=("dec"===o?ee:te)(r,1);i%=e,"le"===a?s.splice(0,0,n):s.push(n)}return s.join("")},default:oe},se=(r,n,t)=>{const o=[];return t.forEach(t=>{var e=t["condition"];Wt(e,n,r)&&o.push(((e,r,n)=>{var t;const{type:o="number",name:a,childMap:u,loop:i=!1,limit:s}=n;let c=1,f=null!=(t=e[a])?t:qt(n,e);i?c=s?"string"==typeof s||Array.isArray(s)?ut(e,s,0):Math.min(s,f.length):f.length:f=[f];const l=[];for(let t=0;t<c;t++){const a=null!=(p=f[t])?p:qt(n,e);var p="child"===o?se(a,r,u):"function"==typeof ae[o]?ae[o](a,n,r):ae.default(a,n,r);l.push(p)}return l.join("")})(r,n,t))}),o.join("")},ce=(t,e)=>{var r;return Array.isArray(t)&&"child"===(null==(r=e[0])?void 0:r.type)&&null!=(r=e[0])&&r.loop?"00":se(t,t,e)};var fe={parse:Zt,format:ce,onLog:Ct,offLog:Ft};exports.default=fe,exports.format=ce,exports.offLog=Ft,exports.onLog=Ct,exports.parse=Zt; //# sourceMappingURL=index.cjs.js.map