ordered-binary
Advanced tools
Comparing version 1.1.3 to 1.2.0
17
index.js
@@ -22,2 +22,7 @@ /* | ||
let nullTerminate = false | ||
let textEncoder | ||
try { | ||
textEncoder = new TextEncoder() | ||
} catch (error) {} | ||
/* | ||
@@ -34,5 +39,5 @@ * Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering | ||
let c1 = key.charCodeAt(0) | ||
if (c1 < 28) // escape character | ||
if (!(c1 >= 28)) // escape character | ||
target[position++] = 27 | ||
if (strLength < 0x20) { | ||
if (strLength < 0x40) { | ||
let i, c2 | ||
@@ -62,4 +67,6 @@ for (i = 0; i < strLength; i++) { | ||
} | ||
} else if (target.utf8Write) { | ||
position += target.utf8Write(key, position, 2000) | ||
} else { | ||
position += target.utf8Write(key, position, 2000) | ||
position += textEncoder.encodeInto(key, target.subarray(position)).written | ||
} | ||
@@ -276,3 +283,3 @@ break | ||
export function compareKeys(a, b) { | ||
// compare with type consistency that matches ordered-binary | ||
// compare with type consistency that matches binary comparison | ||
if (typeof a == 'object') { | ||
@@ -325,1 +332,3 @@ if (!a) { | ||
} | ||
export const MINIMUM_KEY = null | ||
export const MAXIMUM_KEY = Buffer.from([0xff]) |
{ | ||
"name": "ordered-binary", | ||
"author": "Kris Zyp", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -0,8 +1,34 @@ | ||
[![npm version](https://img.shields.io/npm/dw/ordered-binary)](https://www.npmjs.org/package/ordered-binary) | ||
[![npm version](https://img.shields.io/npm/v/ordered-binary.svg?style=flat-square)](https://www.npmjs.org/package/ordered-binary) | ||
[![license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE) | ||
<a href="https://dev.doctorevidence.com/"><img src="./assets/powers-dre.png" width="203" /></a> | ||
The ordered-binary provides a representation of JavaScript primitives as NodeJS Buffers, such that there binary values are naturally ordered such that it matches primitive ordering. For example, since -2.0321 > -2.04, then `toBufferKey(-2.0321)` will be greater than `toBufferKey(-2.04)` as a binary representation, in left-to-right evaluation. This is particular useful for storing keys as binaries with something like LevelDB, to avoid any custom sorting. | ||
The ordered-binary package provides a representation of JavaScript primitives, serialized into binary format (NodeJS Buffers or Uint8Arrays), such that the binary values are naturally ordered such that it matches the natural ordering or values. For example, since -2.0321 > -2.04, then `toBufferKey(-2.0321)` will be greater than `toBufferKey(-2.04)` as a binary representation, in left-to-right evaluation. This is particular useful for storing keys as binaries with something like LMDB or LevelDB, to avoid any custom sorting. | ||
The main module exports two functions: | ||
The ordered-binary package supports strings, numbers, booleans, symbols, null, as well as an array of primitives. Here is an example of ordering of primitive values: | ||
``` | ||
Buffer.from([0]) // buffers are left unchanged, and this is the minimum value | ||
Symbol.for('even symbols') | ||
-10 // negative supported | ||
-1.1 // decimals supported | ||
400 | ||
3E10 | ||
'Hello' | ||
['Hello', 'World'] | ||
'World' | ||
'hello' | ||
['hello', 1, 'world'] | ||
['hello', 'world'] | ||
Buffer.from([0xff]) | ||
``` | ||
The main module exports these functions: | ||
`writeKey(key: string | number | boolean | null | Array, target: Buffer, position: integer, inSequence?: boolean)` - Writes the provide key to the target buffer | ||
`readKey(buffer, start, end, inSequence)` - Reads the key from the buffer, given the provided start and end, as a primitive value | ||
`toBufferKey(jsPrimitive)` - This accepts a string, number, or boolean as the argument, and returns a `Buffer`. | ||
`fromBufferKey(bufferKey, multiple)` - This accepts a Buffer and returns a JavaScript primitive value. This can also parse buffers that hold multiple values delimited by a byte `30`, by setting the second argument to true (in which case it will return an array). |
@@ -46,2 +46,3 @@ import { assert } from 'chai' | ||
assert.strictEqual(fromBufferKey(toBufferKey('hello')), 'hello') | ||
assert.strictEqual(fromBufferKey(toBufferKey('')), '') | ||
}) | ||
@@ -48,0 +49,0 @@ test('string comparison', () => { |
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
46584
781
35