uint8-varint
Advanced tools
Comparing version 1.0.6 to 1.0.7
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Uint8Varint = factory()}(typeof self !== 'undefined' ? self : this, function () { | ||
"use strict";var Uint8Varint=(()=>{var g=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var N=(n,t)=>{for(var r in t)g(n,r,{get:t[r],enumerable:!0})},p=(n,t,r,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of w(t))!f.call(n,i)&&i!==r&&g(n,i,{get:()=>t[i],enumerable:!(e=m(t,i))||e.enumerable});return n};var y=n=>p(g({},"__esModule",{value:!0}),n);var $={};N($,{signed:()=>a,unsigned:()=>s,zigzag:()=>z});function c(n){return n instanceof Uint8Array?{get(t){return n[t]},set(t,r){n[t]=r}}:{get(t){return n.get(t)},set(t,r){n.set(t,r)}}}var d=4294967296,o=class{constructor(t=0,r=0){this.hi=t,this.lo=r}toBigInt(t){if(t===!0)return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n);if(this.hi>>>31){let r=~this.lo+1>>>0,e=~this.hi>>>0;return r===0&&(e=e+1>>>0),-(BigInt(r)+(BigInt(e)<<32n))}return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n)}toNumber(t){return Number(this.toBigInt(t))}zzDecode(){let t=-(this.lo&1),r=((this.lo>>>1|this.hi<<31)^t)>>>0,e=(this.hi>>>1^t)>>>0;return new o(e,r)}zzEncode(){let t=this.hi>>31,r=((this.hi<<1|this.lo>>>31)^t)>>>0,e=(this.lo<<1^t)>>>0;return new o(r,e)}toBytes(t,r=0){let e=c(t);for(;this.hi>0;)e.set(r++,this.lo&127|128),this.lo=(this.lo>>>7|this.hi<<25)>>>0,this.hi>>>=7;for(;this.lo>127;)e.set(r++,this.lo&127|128),this.lo=this.lo>>>7;e.set(r++,this.lo)}static fromBigInt(t){if(t===0n)return new o;let r=t<0;r&&(t=-t);let e=Number(t>>32n)|0,i=Number(t-(BigInt(e)<<32n))|0;return r&&(e=~e>>>0,i=~i>>>0,++i>d&&(i=0,++e>d&&(e=0))),new o(e,i)}static fromNumber(t){if(t===0)return new o;let r=t<0;r&&(t=-t);let e=t>>>0,i=(t-e)/4294967296>>>0;return r&&(i=~i>>>0,e=~e>>>0,++e>4294967295&&(e=0,++i>4294967295&&(i=0))),new o(i,e)}static fromBytes(t,r=0){let e=c(t),i=new o,h=0;if(t.length-r>4){for(;h<4;++h)if(i.lo=(i.lo|(e.get(r)&127)<<h*7)>>>0,e.get(r++)<128)return i;if(i.lo=(i.lo|(e.get(r)&127)<<28)>>>0,i.hi=(i.hi|(e.get(r)&127)>>4)>>>0,e.get(r++)<128)return i;h=0}else for(;h<4;++h){if(r>=t.length)throw RangeError(`index out of range: ${r} > ${t.length}`);if(i.lo=(i.lo|(e.get(r)&127)<<h*7)>>>0,e.get(r++)<128)return i}if(t.length-r>4){for(;h<5;++h)if(i.hi=(i.hi|(e.get(r)&127)<<h*7+3)>>>0,e.get(r++)<128)return i}else if(r<t.byteLength)for(;h<5;++h){if(r>=t.length)throw RangeError(`index out of range: ${r} > ${t.length}`);if(i.hi=(i.hi|(e.get(r)&127)<<h*7+3)>>>0,e.get(r++)<128)return i}throw RangeError("invalid varint encoding")}};function u(n){return globalThis.Buffer!=null?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):n}function l(n=0){return globalThis.Buffer?.allocUnsafe!=null?u(globalThis.Buffer.allocUnsafe(n)):new Uint8Array(n)}var B=Math.pow(2,7),I=Math.pow(2,14),E=Math.pow(2,21),M=Math.pow(2,28),x=Math.pow(2,35),A=Math.pow(2,42),U=Math.pow(2,49),T=Math.pow(2,56),R=Math.pow(2,63),s={encodingLength(n){return n<B?1:n<I?2:n<E?3:n<M?4:n<x?5:n<A?6:n<U?7:n<T?8:n<R?9:10},encode(n,t,r=0){if(Number.MAX_SAFE_INTEGER!=null&&n>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return t==null&&(t=l(s.encodingLength(n))),o.fromNumber(n).toBytes(t,r),t},decode(n,t=0){return o.fromBytes(n,t).toNumber(!0)}},a={encodingLength(n){return n<0?10:s.encodingLength(n)},encode(n,t,r){return t==null&&(t=l(a.encodingLength(n))),n<0?(o.fromNumber(n).toBytes(t,r),t):s.encode(n,t,r)},decode(n,t=0){return o.fromBytes(n,t).toNumber(!1)}},z={encodingLength(n){return s.encodingLength(n>=0?n*2:n*-2-1)},encode(n,t,r){return n=n>=0?n*2:n*-2-1,s.encode(n,t,r)},decode(n,t=0){let r=s.decode(n,t);return r&1?(r+1)/-2:r/2}};return y($);})(); | ||
"use strict";var Uint8Varint=(()=>{var g=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var N=(n,t)=>{for(var r in t)g(n,r,{get:t[r],enumerable:!0})},p=(n,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of w(t))!f.call(n,e)&&e!==r&&g(n,e,{get:()=>t[e],enumerable:!(i=m(t,e))||i.enumerable});return n};var B=n=>p(g({},"__esModule",{value:!0}),n);var z={};N(z,{signed:()=>a,unsigned:()=>s,zigzag:()=>R});function c(n){return n instanceof Uint8Array?{get(t){return n[t]},set(t,r){n[t]=r}}:{get(t){return n.get(t)},set(t,r){n.set(t,r)}}}var d=4294967296,h=class n{constructor(t=0,r=0){this.hi=t,this.lo=r}toBigInt(t){if(t===!0)return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n);if(this.hi>>>31){let r=~this.lo+1>>>0,i=~this.hi>>>0;return r===0&&(i=i+1>>>0),-(BigInt(r)+(BigInt(i)<<32n))}return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n)}toNumber(t){return Number(this.toBigInt(t))}zzDecode(){let t=-(this.lo&1),r=((this.lo>>>1|this.hi<<31)^t)>>>0,i=(this.hi>>>1^t)>>>0;return new n(i,r)}zzEncode(){let t=this.hi>>31,r=((this.hi<<1|this.lo>>>31)^t)>>>0,i=(this.lo<<1^t)>>>0;return new n(r,i)}toBytes(t,r=0){let i=c(t);for(;this.hi>0;)i.set(r++,this.lo&127|128),this.lo=(this.lo>>>7|this.hi<<25)>>>0,this.hi>>>=7;for(;this.lo>127;)i.set(r++,this.lo&127|128),this.lo=this.lo>>>7;i.set(r++,this.lo)}static fromBigInt(t){if(t===0n)return new n;let r=t<0;r&&(t=-t);let i=Number(t>>32n)|0,e=Number(t-(BigInt(i)<<32n))|0;return r&&(i=~i>>>0,e=~e>>>0,++e>d&&(e=0,++i>d&&(i=0))),new n(i,e)}static fromNumber(t){if(t===0)return new n;let r=t<0;r&&(t=-t);let i=t>>>0,e=(t-i)/4294967296>>>0;return r&&(e=~e>>>0,i=~i>>>0,++i>4294967295&&(i=0,++e>4294967295&&(e=0))),new n(e,i)}static fromBytes(t,r=0){let i=c(t),e=new n,o=0;if(t.length-r>4){for(;o<4;++o)if(e.lo=(e.lo|(i.get(r)&127)<<o*7)>>>0,i.get(r++)<128)return e;if(e.lo=(e.lo|(i.get(r)&127)<<28)>>>0,e.hi=(e.hi|(i.get(r)&127)>>4)>>>0,i.get(r++)<128)return e;o=0}else for(;o<4;++o){if(r>=t.length)throw RangeError(`index out of range: ${r} > ${t.length}`);if(e.lo=(e.lo|(i.get(r)&127)<<o*7)>>>0,i.get(r++)<128)return e}if(t.length-r>4){for(;o<5;++o)if(e.hi=(e.hi|(i.get(r)&127)<<o*7+3)>>>0,i.get(r++)<128)return e}else if(r<t.byteLength)for(;o<5;++o){if(r>=t.length)throw RangeError(`index out of range: ${r} > ${t.length}`);if(e.hi=(e.hi|(i.get(r)&127)<<o*7+3)>>>0,i.get(r++)<128)return e}throw RangeError("invalid varint encoding")}};function u(n){return globalThis.Buffer!=null?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):n}function l(n=0){return globalThis.Buffer?.allocUnsafe!=null?u(globalThis.Buffer.allocUnsafe(n)):new Uint8Array(n)}var y=Math.pow(2,7),I=Math.pow(2,14),E=Math.pow(2,21),M=Math.pow(2,28),x=Math.pow(2,35),A=Math.pow(2,42),U=Math.pow(2,49),L=Math.pow(2,56),T=Math.pow(2,63),s={encodingLength(n){return n<y?1:n<I?2:n<E?3:n<M?4:n<x?5:n<A?6:n<U?7:n<L?8:n<T?9:10},encode(n,t,r=0){if(Number.MAX_SAFE_INTEGER!=null&&n>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return t==null&&(t=l(s.encodingLength(n))),h.fromNumber(n).toBytes(t,r),t},decode(n,t=0){return h.fromBytes(n,t).toNumber(!0)}},a={encodingLength(n){return n<0?10:s.encodingLength(n)},encode(n,t,r){return t==null&&(t=l(a.encodingLength(n))),n<0?(h.fromNumber(n).toBytes(t,r),t):s.encode(n,t,r)},decode(n,t=0){return h.fromBytes(n,t).toNumber(!1)}},R={encodingLength(n){return s.encodingLength(n>=0?n*2:n*-2-1)},encode(n,t,r){return n=n>=0?n*2:n*-2-1,s.encode(n,t,r)},decode(n,t=0){let r=s.decode(n,t);return r&1?(r+1)/-2:r/2}};return B(z);})(); | ||
return Uint8Varint})); |
@@ -82,3 +82,3 @@ import { LongBits } from 'longbits'; | ||
}, | ||
// @ts-expect-error | ||
// @ts-expect-error types are wrong | ||
encode(value, buf, offset) { | ||
@@ -85,0 +85,0 @@ value = value >= 0 ? value * 2 : (value * -2) - 1; |
{ | ||
"name": "uint8-varint", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Read/write varints from Uint8Arrays and Uint8ArrayLists", | ||
@@ -166,3 +166,3 @@ "license": "Apache-2.0 OR MIT", | ||
"@types/varint": "^6.0.0", | ||
"aegir": "^38.1.8", | ||
"aegir": "^40.0.0", | ||
"benchmark": "^2.1.4", | ||
@@ -169,0 +169,0 @@ "varint": "^6.0.0" |
@@ -1,5 +0,5 @@ | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
import accessor from 'byte-access' | ||
import { LongBits } from 'longbits' | ||
import { allocUnsafe } from 'uint8arrays/alloc' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
@@ -6,0 +6,0 @@ const LIMIT = 0x7fn |
@@ -1,4 +0,4 @@ | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
import { LongBits } from 'longbits' | ||
import { allocUnsafe } from 'uint8arrays/alloc' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
@@ -114,3 +114,3 @@ const N1 = Math.pow(2, 7) | ||
// @ts-expect-error | ||
// @ts-expect-error types are wrong | ||
encode (value: any, buf?: any, offset?: any) { | ||
@@ -117,0 +117,0 @@ value = value >= 0 ? value * 2 : (value * -2) - 1 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28417