Comparing version 1.0.2 to 2.0.0-a.0
@@ -13,5 +13,4 @@ import { encodingLength } from "./encoding-length.js"; | ||
numbers.forEach((n) => { | ||
const bytes = encode(n); | ||
result.set(bytes, offset); | ||
offset += bytes.byteLength; | ||
const [_, len] = encode(n, result, offset); | ||
offset += len; | ||
}); | ||
@@ -18,0 +17,0 @@ return result; |
/** | ||
* Encode number as varint bytes. | ||
* | ||
* @param num - Number to encode | ||
* @return Uint8Array - `num` encoded as bytes according to varint spec. | ||
* @param num - Number to encode. | ||
* @param out - Uint8Array bytes to write to. If not specified, empty buffer is used. | ||
* @param offset - Offset to use for the `out` buffer. | ||
* @return [Uint8Array, number] - Uint8Array with`num` encoded as bytes according to varint spec, and number of bytes written. | ||
*/ | ||
export declare function encode(num: number): Uint8Array; | ||
export declare function encode(num: number, out?: Uint8Array, offset?: number): [Uint8Array, number]; |
@@ -9,22 +9,23 @@ import { encodingLength } from "./encoding-length.js"; | ||
* | ||
* @param num - Number to encode | ||
* @return Uint8Array - `num` encoded as bytes according to varint spec. | ||
* @param num - Number to encode. | ||
* @param out - Uint8Array bytes to write to. If not specified, empty buffer is used. | ||
* @param offset - Offset to use for the `out` buffer. | ||
* @return [Uint8Array, number] - Uint8Array with`num` encoded as bytes according to varint spec, and number of bytes written. | ||
*/ | ||
export function encode(num) { | ||
export function encode(num, out = new Uint8Array(encodingLength(num)), offset = 0) { | ||
if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) { | ||
throw new RangeError(`Could not encode ${num} as varint`); | ||
} | ||
const buffer = new Uint8Array(encodingLength(num)); | ||
let bytes = 0; | ||
let bytes = offset; | ||
while (num >= INT) { | ||
buffer[bytes++] = (num & 0xff) | MSB; | ||
out[bytes++] = (num & 0xff) | MSB; | ||
num /= 128; | ||
} | ||
while (num & MSBALL) { | ||
buffer[bytes++] = (num & 0xff) | MSB; | ||
out[bytes++] = (num & 0xff) | MSB; | ||
num >>>= 7; | ||
} | ||
buffer[bytes] = num | 0; | ||
return buffer; | ||
out[bytes] = num | 0; | ||
return [out, bytes - offset + 1]; | ||
} | ||
//# sourceMappingURL=encode.js.map |
{ | ||
"name": "varintes", | ||
"version": "1.0.2", | ||
"version": "2.0.0-a.0", | ||
"description": "Unsigned Varint encoding and decoding, exposed as ESModule", | ||
@@ -67,14 +67,13 @@ "keywords": [ | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/.bin/tsm ./node_modules/uvu/bin.js . .+\\.test\\.ts", | ||
"build": "./node_modules/.bin/tsc --project tsconfig.build.json", | ||
"prepublishOnly": "pnpm run build" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^2.7.1", | ||
"tsm": "^2.2.2", | ||
"typescript": "^4.8.3", | ||
"typescript": "^4.8.4", | ||
"uint8arrays": "^3.1.0", | ||
"uvu": "^0.5.6" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/.bin/tsm ./node_modules/uvu/bin.js . .+\\.test\\.ts", | ||
"build": "./node_modules/.bin/tsc --project tsconfig.build.json" | ||
} | ||
} | ||
} |
@@ -52,4 +52,9 @@ # Varintes | ||
# To Do | ||
- [ ] Faster encode by loop unrolling | ||
- [ ] Faster decode by loop unrolling | ||
## License | ||
MIT or APACHE-2.0 |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
18750
181
60
2