Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

bleb

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bleb - npm Package Compare versions

Comparing version
0.0.1
to
0.0.2
+8
-0
index.d.ts

@@ -0,1 +1,9 @@

/**
* @example Basic Usage
* ```ts
* import * as Bleb from "bleb"
*
* console.log(Bleb.fromBigInt(1000n)) // [ 232, 6 ]
* ```
*/
export declare function fromBigInt(integer: bigint): number[];

@@ -2,0 +10,0 @@ export declare function toBigInt(data: Record<number, number>, index?: {

+23
-1

@@ -1,1 +0,23 @@

import{assert as n}from"@samual/lib/assert";function fromBigInt(n){const t=[];for(;n>=0;){let r=Number(0x7Fn&n);n>>=7n,n--&&(r|=128),t.push(r)}return t}function toBigInt(t,r={$:0}){let o=0n,i=0n;for(;;){const e=t[r.$++];if(n(null!=e,"src/index.ts:28:29"),o+=(0x7Fn&BigInt(e))+(i&&1n)<<i,i+=7n,!(128&e))return o}}export{fromBigInt,toBigInt};
import { assert } from "@samual/lib/assert"
function fromBigInt(integer) {
const result = []
for (; integer >= 0n; ) {
let value = Number(0x7fn & integer)
integer >>= 7n
integer-- && (value |= 128)
result.push(value)
}
return result
}
function toBigInt(data, index = { $: 0 }) {
let result = 0n,
offset = 0n
for (;;) {
const byte = data[index.$++]
assert(null != byte, "src/index.ts:55:29")
result += ((0x7fn & BigInt(byte)) + (offset && 1n)) << offset
offset += 7n
if (!(128 & byte)) return result
}
}
export { fromBigInt, toBigInt }

@@ -1,1 +0,38 @@

{"name":"bleb","version":"0.0.1","description":"remember to update urls in this file","repository":{"type":"git","url":"https://github.com/samualtnorman/bleb"},"author":"Samual Norman","license":"MIT","bugs":{"url":"https://github.com/samualtnorman/bleb/issues"},"homepage":"https://github.com/samualtnorman/bleb#readme","dependencies":{"@samual/lib":"0.8.1"},"type":"module","engine":{"node":">=16"},"exports":{"./*":"./*.js","./*.js":"./*.js"}}
{
"name": "bleb",
"version": "0.0.2",
"description": "Better LEB128",
"repository": "github:samualtnorman/bleb",
"author": "Samual Norman",
"license": "MIT",
"bugs": {
"url": "https://github.com/samualtnorman/bleb/issues"
},
"homepage": "https://github.com/samualtnorman/bleb#readme",
"dependencies": {
"@samual/lib": "^0.13.0"
},
"type": "module",
"exports": {
".": "./index.js",
"./*": "./*.js",
"./*.js": "./*.js"
},
"keywords": [
"encoding",
"serialization",
"encoder",
"decoder",
"decoding",
"deserialization",
"encode",
"decode",
"integer",
"bigint",
"leb128",
"bleb"
],
"engines": {
"node": "^18.20 || ^20.10 || >=22"
}
}
# Better LEB128
bleb
[![Build](https://github.com/samualtnorman/bleb/actions/workflows/build.yml/badge.svg)](https://github.com/samualtnorman/bleb/actions/workflows/build.yml) [![Lint](https://github.com/samualtnorman/bleb/actions/workflows/lint.yml/badge.svg)](https://github.com/samualtnorman/bleb/actions/workflows/lint.yml)
| Range | bleb | LEB128 |
|-----------------|---------|---------|
| 0 - 127 | 1 byte | 1 byte |
| 128 - 16383 | 2 bytes | 2 bytes |
| 16384 - 16511 | 2 bytes | 3 bytes |
| 16512 - 2097151 | 3 bytes | 3 bytes |
| Range | bleb | LEB128 |
|-------------------|---------|---------|
| 0 - 127 | 1 byte | 1 byte |
| 128 - 16383 | 2 bytes | 2 bytes |
| 16384 - 16511 | 2 bytes | 3 bytes |
| 16512 - 2097151 | 3 bytes | 3 bytes |
| 2097152 - 2113663 | 3 bytes | 4 bytes |
| ... |
## Encoding
### Basic Usage
```js
import * as Bleb from "bleb"
console.log(Bleb.fromBigInt(1000n)) // [ 232, 6 ]
```
## Decoding
### Basic Usage
```js
import * as Bleb from "bleb"
console.log(Bleb.toBigInt([ 232, 6 ])) // 1000n
```
### Less-Basic Usage
```js
import * as Bleb from "bleb"
// some data with bleb at byte offset 2
const u8View = new Uint8Array([ 177, 218, 232, 6, 197, 165, 75, 177 ])
const index = { $: 2 }
console.log(Bleb.toBigInt(u8View, index)) // 1000n
console.log(index) // { $: 4 }
```