quickbit-native
Advanced tools
Comparing version 2.0.3 to 2.1.0
75
index.js
@@ -5,2 +5,7 @@ const binding = require('node-gyp-build')(__dirname) | ||
exports.get = function get (field, bit) { | ||
const n = field.byteLength * 8 | ||
if (bit < 0) bit += n | ||
if (bit < 0 || bit >= n) return false | ||
return binding.quickbit_napi_get(field, bit) !== 0 | ||
@@ -10,2 +15,7 @@ } | ||
exports.set = function set (field, bit, value = true) { | ||
const n = field.byteLength * 8 | ||
if (bit < 0) bit += n | ||
if (bit < 0 || bit >= n) return false | ||
return binding.quickbit_napi_set(field, bit, value ? 1 : 0) !== 0 | ||
@@ -15,2 +25,8 @@ } | ||
exports.fill = function fill (field, value, start = 0, end = field.byteLength * 8) { | ||
const n = field.byteLength * 8 | ||
if (start < 0) start += n | ||
if (end < 0) end += n | ||
if (start < 0 || start >= field.byteLength * 8 || start >= end) return field | ||
binding.quickbit_napi_fill(field, value ? 1 : 0, start, end) | ||
@@ -21,2 +37,8 @@ return field | ||
exports.findFirst = function findFirst (field, value, position = 0) { | ||
const n = field.byteLength * 8 | ||
if (position < 0) position += n | ||
if (position < 0) position = 0 | ||
if (position >= n) return -1 | ||
return binding.quickbit_napi_find_first(field, value ? 1 : 0, position) | ||
@@ -26,2 +48,8 @@ } | ||
exports.findLast = function findLast (field, value, position = field.byteLength * 8 - 1) { | ||
const n = field.byteLength * 8 | ||
if (position < 0) position += n | ||
if (position < 0) return -1 | ||
if (position >= n) position = n - 1 | ||
return binding.quickbit_napi_find_last(field, value ? 1 : 0, position) | ||
@@ -31,19 +59,26 @@ } | ||
const Index = exports.Index = class Index { | ||
static from (fieldOrChunks) { | ||
static from (fieldOrChunks, byteLength = -1) { | ||
if (Array.isArray(fieldOrChunks)) { | ||
return new SparseIndex(fieldOrChunks) | ||
return new SparseIndex(fieldOrChunks, byteLength) | ||
} else { | ||
return new DenseIndex(fieldOrChunks) | ||
return new DenseIndex(fieldOrChunks, byteLength) | ||
} | ||
} | ||
get byteLength () { | ||
return 0 | ||
constructor (byteLength) { | ||
this._byteLength = byteLength | ||
this.handle = b4a.allocUnsafe(binding.sizeof_quickbit_index_t) | ||
} | ||
constructor () { | ||
this.handle = b4a.allocUnsafe(binding.sizeof_quickbit_index_t) | ||
get byteLength () { | ||
return this._byteLength | ||
} | ||
skipFirst (value, position = 0) { | ||
const n = this.byteLength * 8 | ||
if (position < 0) position += n | ||
if (position < 0) position = 0 | ||
if (position >= n) return n - 1 | ||
return binding.quickbit_napi_skip_first(this.handle, this.byteLength, value ? 1 : 0, position) | ||
@@ -53,2 +88,8 @@ } | ||
skipLast (value, position = this.byteLength * 8 - 1) { | ||
const n = this.byteLength * 8 | ||
if (position < 0) position += n | ||
if (position < 0) return 0 | ||
if (position >= n) position = n - 1 | ||
return binding.quickbit_napi_skip_last(this.handle, this.byteLength, value ? 1 : 0, position) | ||
@@ -59,4 +100,4 @@ } | ||
class DenseIndex extends Index { | ||
constructor (field) { | ||
super() | ||
constructor (field, byteLength) { | ||
super(byteLength) | ||
this.field = field | ||
@@ -68,2 +109,3 @@ | ||
get byteLength () { | ||
if (this._byteLength !== -1) return this._byteLength | ||
return this.field.byteLength | ||
@@ -73,2 +115,7 @@ } | ||
update (bit) { | ||
const n = this.byteLength * 8 | ||
if (bit < 0) bit += n | ||
if (bit < 0 || bit >= n) return false | ||
return binding.quickbit_napi_index_update(this.handle, this.field, bit) !== 0 | ||
@@ -79,4 +126,4 @@ } | ||
class SparseIndex extends Index { | ||
constructor (chunks) { | ||
super() | ||
constructor (chunks, byteLength) { | ||
super(byteLength) | ||
this.chunks = chunks | ||
@@ -88,2 +135,3 @@ | ||
get byteLength () { | ||
if (this._byteLength !== -1) return this._byteLength | ||
const last = this.chunks[this.chunks.length - 1] | ||
@@ -94,4 +142,9 @@ return last ? last.offset + last.field.byteLength : 0 | ||
update (bit) { | ||
const n = this.byteLength * 8 | ||
if (bit < 0) bit += n | ||
if (bit < 0 || bit >= n) return false | ||
return binding.quickbit_napi_index_update_sparse(this.handle, this.chunks, bit) !== 0 | ||
} | ||
} |
{ | ||
"name": "quickbit-native", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "libquickbit JavaScript bindings for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
343316
101