Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@adiwajshing/keyed-db

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

@adiwajshing/keyed-db - npm Package Compare versions

Comparing version 0.1.8 to 0.2.0

lib/BinarySearch.d.ts

23

lib/KeyedDB.d.ts

@@ -1,7 +0,3 @@

declare type PaginationMode = 'before' | 'after';
export declare type Comparable<T, K> = {
key: (v: T) => K;
compare: (a: K, b: K) => number;
};
export default class KeyedDB<T, K> {
import { IKeyedDB, Identifiable, Comparable, PaginationMode } from "./Types";
export default class KeyedDB<T, K> implements IKeyedDB<T, K> {
private array;

@@ -12,10 +8,11 @@ private dict;

/**
*
* @param key Return the unique key used to sort items
* @param id The unique ID for the items
*/
constructor(key: ((v: T) => number) | Comparable<T, K>, id?: (v: T) => string);
constructor(key: Comparable<T, K>, id?: Identifiable<T>);
get length(): number;
get first(): T;
get last(): T;
toJSON(): T[];
insert(value: T): void;
insert(...values: T[]): void;
slice(start: number, end: number): KeyedDB<T, K>;

@@ -44,11 +41,5 @@ delete(value: T): T;

paginated(cursor: K | null, limit: number, predicate?: (value: T, index: number) => boolean, mode?: PaginationMode): T[];
private _insertSingle;
private filtered;
private firstIndex;
}
/**
*
* @param array the array to search in
* @param predicate return a value of < 0, if the item you're looking for should come before, 0 if it is the item you're looking for
*/
export declare function binarySearch<T>(array: T[], predicate: (t: T) => number): number;
export {};
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.binarySearch = void 0;
const BinarySearch_1 = __importDefault(require("./BinarySearch"));
class KeyedDB {
/**
*
* @param key Return the unique key used to sort items

@@ -11,10 +13,3 @@ * @param id The unique ID for the items

constructor(key, id) {
if (typeof key === 'function') {
this.key = {
key,
compare: (a, b) => a - b
};
}
else
this.key = key;
this.key = key;
this.idGetter = id || (v => this.key.key(v).toString());

@@ -25,25 +20,15 @@ this.dict = {};

get length() {
return this.all().length;
return this.array.length;
}
get first() {
return this.array[0];
}
get last() {
return this.array[this.array.length - 1];
}
toJSON() {
return this.array;
}
insert(value) {
if (!value)
throw new Error('undefined value');
if (this.array.length > 0) {
const index = this.firstIndex(value);
if (index >= this.array.length)
this.array.push(value);
else if (index < 0)
this.array.unshift(value);
else if (this.key.key(value) !== this.key.key(this.array[index]))
this.array.splice(index, 0, value);
else
throw new Error(`duplicate key: ${this.key.key(value)}, of inserting: ${this.idGetter(value)}, present: ${this.idGetter(this.array[index])}`);
}
else {
this.array.push(value);
}
this.dict[this.idGetter(value)] = value;
insert(...values) {
values.forEach(v => this._insertSingle(v));
}

@@ -75,3 +60,2 @@ slice(start, end) {

updateKey(value, update) {
var v = 121;
this.delete(value);

@@ -111,3 +95,3 @@ update(value);

if (cursor !== null && typeof cursor !== 'undefined') {
index = binarySearch(this.array, v => this.key.compare(cursor, this.key.key(v)));
index = BinarySearch_1.default(this.array, v => this.key.compare(cursor, this.key.key(v)));
if (index < 0)

@@ -120,2 +104,21 @@ index = 0;

}
_insertSingle(value) {
if (!value)
throw new Error('undefined value');
if (this.array.length > 0) {
const index = this.firstIndex(value);
if (index >= this.array.length)
this.array.push(value);
else if (index < 0)
this.array.unshift(value);
else if (this.key.key(value) !== this.key.key(this.array[index]))
this.array.splice(index, 0, value);
else
throw new Error(`duplicate key: ${this.key.key(value)}, of inserting: ${this.idGetter(value)}, present: ${this.idGetter(this.array[index])}`);
}
else {
this.array.push(value);
}
this.dict[this.idGetter(value)] = value;
}
filtered(start, count, mode, predicate) {

@@ -151,37 +154,5 @@ let arr;

firstIndex(value) {
return binarySearch(this.array, v => this.key.compare(this.key.key(value), this.key.key(v)));
return BinarySearch_1.default(this.array, v => this.key.compare(this.key.key(value), this.key.key(v)));
}
}
exports.default = KeyedDB;
/**
*
* @param array the array to search in
* @param predicate return a value of < 0, if the item you're looking for should come before, 0 if it is the item you're looking for
*/
function binarySearch(array, predicate) {
let low = 0;
let high = array.length;
if (array.length === 0)
return low;
if (predicate(array[low]) < 0)
return low - 1;
else if (predicate(array[low]) === 0)
return low;
const maxPred = predicate(array[high - 1]);
if (maxPred > 0)
return high;
else if (maxPred === 0)
return high - 1;
while (low !== high) {
const mid = low + Math.floor((high - low) / 2);
const pred = predicate(array[mid]);
if (pred < 0)
high = mid;
else if (pred > 0)
low = mid + 1;
else
return mid;
}
return low;
}
exports.binarySearch = binarySearch;
{
"name": "@adiwajshing/keyed-db",
"version": "0.1.8",
"version": "0.2.0",
"description": "Lightweight library to store an in-memory DB",

@@ -32,4 +32,4 @@ "homepage": "https://github.com/adiwajshing/keyed-db",

"ts-node-dev": "^1.0.0-pre.61",
"typescript": "^3.9.7"
"typescript": "^4.0.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc