Skip to content

Commit

Permalink
type-lint: add details iterator options.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jun 18, 2023
1 parent 31f8c3a commit bd6047e
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 44 deletions.
192 changes: 156 additions & 36 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,77 @@ const HIGH = Buffer.alloc(255, 0xff);
* @property {(location: string) => void} backup
*/

/**
* @typedef {Object} IterOptions
* @property {Buffer} [gte]
* @property {Buffer} [gt]
* @property {Buffer} [lte]
* @property {Buffer} [lt]
* @property {boolean} [keys]
* @property {boolean} [values]
* @property {boolean} [fillCache]
* @property {boolean} [reverse]
* @property {number} [limit]
*/

/**
* @template T
* @typedef {Object} RangeOptions
* @property {Buffer} [gte]
* @property {Buffer} [gt]
* @property {Buffer} [lte]
* @property {Buffer} [lt]
* @property {boolean} [reverse]
* @property {number} [limit]
* @property {EachCallback<T>} [parse]
*/

/**
* @template T
* @typedef {Object} KeysOptions
* @property {Buffer} [gte]
* @property {Buffer} [gt]
* @property {Buffer} [lte]
* @property {Buffer} [lt]
* @property {boolean} [reverse]
* @property {number} [limit]
* @property {KeysCallback<T>} [parse]
*/

/**
* @template T
* @typedef {Object} ValuesOptions
* @property {Buffer} [gte]
* @property {Buffer} [gt]
* @property {Buffer} [lte]
* @property {Buffer} [lt]
* @property {boolean} [reverse]
* @property {number} [limit]
* @property {ValuesCallback<T>} [parse]
*/

/**
* @template T
* @callback EachCallback
* @param {Buffer} key
* @param {Buffer} value
* @returns {T}
*/

/**
* @template T
* @callback KeysCallback
* @param {Buffer} key
* @returns {T}
*/

/**
* @template T
* @callback ValuesCallback
* @param {Buffer} value
* @returns {T}
*/

/**
* DB
*/
Expand Down Expand Up @@ -270,7 +341,7 @@ class DB {
/**
* Retrieve a record from the database.
* @param {Buffer} key
* @returns {Promise} - Returns Buffer.
* @returns {Promise<Buffer>} - Returns Buffer.
*/

get(key) {
Expand Down Expand Up @@ -363,7 +434,7 @@ class DB {

/**
* Create an iterator.
* @param {Object} options
* @param {IterOptions} options
* @returns {Iterator}
*/

Expand Down Expand Up @@ -394,7 +465,7 @@ class DB {
* Calculate approximate database size.
* @param {Buffer|null} start - Start key.
* @param {Buffer|null} end - End key.
* @returns {Promise} - Returns Number.
* @returns {Promise<Number>} - Returns Number.
*/

approximateSize(start, end) {
Expand Down Expand Up @@ -461,7 +532,7 @@ class DB {
/**
* Test whether a key exists.
* @param {Buffer} key
* @returns {Promise} - Returns Boolean.
* @returns {Promise<Boolean>} - Returns Boolean.
*/

async has(key) {
Expand All @@ -471,8 +542,9 @@ class DB {

/**
* Collect all keys from iterator options.
* @param {Object} options - Iterator options.
* @returns {Promise} - Returns Array.
* @template T
* @param {RangeOptions<T>} [options] - Iterator options.
* @returns {Promise<IteratorItem[]|T[]>} - Returns Array.
*/

async range(options) {
Expand All @@ -495,8 +567,9 @@ class DB {

/**
* Collect all keys from iterator options.
* @param {Object} options - Iterator options.
* @returns {Promise} - Returns Array.
* @template T
* @param {KeysOptions<T>} [options] - Iterator options.
* @returns {Promise<Array>} - Returns Array.
*/

async keys(options) {
Expand All @@ -519,8 +592,9 @@ class DB {

/**
* Collect all keys from iterator options.
* @param {Object} options - Iterator options.
* @returns {Promise} - Returns Array.
* @template T
* @param {ValuesOptions<T>} [options] - Iterator options.
* @returns {Promise<Array>} - Returns Array.
*/

async values(options) {
Expand Down Expand Up @@ -946,9 +1020,8 @@ class Iterator {
/**
* Create an iterator.
* @constructor
* @ignore
* @param {DB} db
* @param {Object} [options=null]
* @param {IterOptions} [options=null]
* @param {Buffer} [prefix=null]
*/

Expand Down Expand Up @@ -1054,6 +1127,8 @@ class Iterator {

/**
* For each.
* @template T
* @param {EachCallback<T>} cb
* @returns {Promise}
*/

Expand Down Expand Up @@ -1211,68 +1286,103 @@ class Iterator {

/**
* Collect all keys and values from iterator options.
* @param {Function} parse
* @returns {Promise} - Returns Array.
* @template T
* @param {EachCallback<T>} [parse]
* @returns {Promise<IteratorItem[]|T[]>} - Returns Array.
*/

async range(parse) {
assert(!parse || typeof parse === 'function');

if (!parse) {
/** @type {IteratorItem[]} */
const items = [];

await this.each((key, value) => {
items.push(new IteratorItem(key, value));
});

return items;
}

/** @type {T[]} */
const items = [];

await this.each((key, value) => {
if (parse) {
const item = parse(key, value);
const item = parse(key, value);

if (item != null)
items.push(item);
} else {
items.push(new IteratorItem(key, value));
}
if (item != null)
items.push(item);
});

return items;
}

/**
* Collect all keys from iterator options.
* @param {Function} parse
* @returns {Promise} - Returns Array.
* @template T
* @param {KeysCallback<T>} [parse]
* @returns {Promise<T[]|Buffer[]>} - Returns Array.
*/

async keys(parse) {
assert(!parse || typeof parse === 'function');

if (!parse) {
/** @type {Buffer[]} */
const items = [];

await this.each((key, value) => {
items.push(key);
});

return items;
}

/** @type {T[]} */
const items = [];

await this.each((key, value) => {
if (parse)
key = parse(key);
/** @type {T|null} */
const parsed = parse(key);

if (key != null)
items.push(key);
if (parsed != null)
items.push(parsed);
});

return items;
}

/**
* Collect all values from iterator options.
* @param {Function} parse
* @returns {Promise} - Returns Array.
* @template T
* @param {ValuesCallback<T>} [parse]
* @returns {Promise<T[]|Buffer[]>} - Returns Array.
*/

async values(parse) {
assert(!parse || typeof parse === 'function');

if (!parse) {
/** @type {Buffer[]} */
const items = [];

await this.each((key, value) => {
items.push(value);
});

return items;
}

/** @type {T[]} */
const items = [];

await this.each((key, value) => {
if (parse)
value = parse(value);
/** @type {T|null} */
const parsed = parse(value);

if (value != null)
items.push(value);
if (parsed != null)
items.push(parsed);
});

return items;
Expand All @@ -1287,7 +1397,6 @@ class IteratorItem {
/**
* Create an iterator item.
* @constructor
* @ignore
* @param {Buffer} key
* @param {Buffer} value
* @property {Buffer} key
Expand Down Expand Up @@ -1398,11 +1507,22 @@ class DBOptions {
*/

class IteratorOptions {
/** @type {Buffer} */
gte;

/** @type {Buffer} */
lte;

/** @type {Buffer} */
gt;

/** @type {Buffer} */
lt;

/**
* Create iterator options.
* @constructor
* @ignore
* @param {Object} options
* @param {IterOptions} options
*/

constructor(options) {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test": "bmocha --reporter spec test/*-test.js"
},
"dependencies": {
"bsert": "~0.0.10",
"bsert": "~0.0.12",
"loady": "~0.0.5"
},
"devDependencies": {
Expand Down

0 comments on commit bd6047e

Please sign in to comment.