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.7 to 0.1.8

16

lib/KeyedDB.d.ts
declare type PaginationMode = 'before' | 'after';
export default class KeyedDB<T> {
export declare type Comparable<T, K> = {
key: (v: T) => K;
compare: (a: K, b: K) => number;
};
export default class KeyedDB<T, K> {
private array;

@@ -12,7 +16,7 @@ private dict;

*/
constructor(key: (v: T) => number, id?: (v: T) => string);
constructor(key: ((v: T) => number) | Comparable<T, K>, id?: (v: T) => string);
get length(): number;
toJSON(): T[];
insert(value: T): void;
slice(start: number, end: number): KeyedDB<T>;
slice(start: number, end: number): KeyedDB<T, K>;
delete(value: T): T;

@@ -23,3 +27,3 @@ clear(): void;

updateKey(value: T, update: (value: T) => void): void;
filter(predicate: (value: T, index: number) => boolean): KeyedDB<T>;
filter(predicate: (value: T, index: number) => boolean): KeyedDB<T, K>;
/**

@@ -40,3 +44,3 @@ * Get the values of the data in a paginated manner

*/
paginated(cursor: number | null, limit: number, predicate?: (value: T, index: number) => boolean, mode?: PaginationMode): T[];
paginated(cursor: K | null, limit: number, predicate?: (value: T, index: number) => boolean, mode?: PaginationMode): T[];
private filtered;

@@ -50,3 +54,3 @@ private firstIndex;

*/
export declare function binarySearch<T>(array: T[], predicate: (T: any) => number): number;
export declare function binarySearch<T>(array: T[], predicate: (t: T) => number): number;
export {};

@@ -11,4 +11,11 @@ "use strict";

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

@@ -32,6 +39,6 @@ this.array = [];

this.array.unshift(value);
else if (this.key(value) !== this.key(this.array[index]))
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(value)}, of inserting: ${this.idGetter(value)}, present: ${this.idGetter(this.array[index])}`);
throw new Error(`duplicate key: ${this.key.key(value)}, of inserting: ${this.idGetter(value)}, present: ${this.idGetter(this.array[index])}`);
}

@@ -51,3 +58,3 @@ else {

const index = this.firstIndex(value);
if (index < 0 || index >= this.array.length || this.key(value) !== this.key(this.array[index])) {
if (index < 0 || index >= this.array.length || this.key.key(value) !== this.key.key(this.array[index])) {
return null;

@@ -69,2 +76,3 @@ }

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

@@ -92,3 +100,3 @@ update(value);

paginatedByValue(value, limit, predicate, mode = 'after') {
return this.paginated(value && this.key(value), limit, predicate, mode);
return this.paginated(value && this.key.key(value), limit, predicate, mode);
}

@@ -104,7 +112,7 @@ /**

let index = mode === 'after' ? 0 : this.array.length;
if (cursor) {
index = binarySearch(this.array, v => cursor - this.key(v));
if (cursor !== null && typeof cursor !== 'undefined') {
index = binarySearch(this.array, v => this.key.compare(cursor, this.key.key(v)));
if (index < 0)
index = 0;
if (this.key(this.array[index]) === cursor)
if (this.key.key(this.array[index]) === cursor)
index += (mode === 'after' ? 1 : 0);

@@ -144,3 +152,3 @@ }

firstIndex(value) {
return binarySearch(this.array, v => this.key(value) - this.key(v));
return binarySearch(this.array, v => this.key.compare(this.key.key(value), this.key.key(v)));
}

@@ -147,0 +155,0 @@ }

{
"name": "@adiwajshing/keyed-db",
"version": "0.1.7",
"version": "0.1.8",
"description": "Lightweight library to store an in-memory DB",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/adiwajshing/keyed-db",

@@ -19,5 +19,13 @@ # Keyed DB

db = new KeyedDB<T> (t => t.uniqueNumberKeyProperty, t => t.optionalUniqueIDProperty)
// compare with a custom function
db = new KeyedDB<T> ({
key: t => t.someProperty,
compare: (t1, t2) => someComputation(t1, t2) // return -1 if t1 < t2, 0 if t1=t2 & 1 if t1 > t2
}, t => t.optionalUniqueIDProperty)
db.insert (value) // insert value in DB
db.delete (value) // delete value
db.updateKey (value, value => value.uniqueKeyProperty = newValue) // update the key of a value
// update the key of a value,
// will automatically place object after key change
db.updateKey (value, value => value.uniqueKeyProperty = newValue)
db.paginated (someCursor, 20) // get X results after the given cursor (null for the first X results)

@@ -52,3 +60,3 @@

}
console.log (db.all()) // return sorted array
console.log (db.all()) // return internal sorted array
console.log (db.paginated(null, 20)) // return first 20 chats

@@ -55,0 +63,0 @@ console.log (db.paginated(null, 20, null, 'before')) // return last 20 chats

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