Skip to content

Commit

Permalink
Merge pull request #195 from Level/typescript
Browse files Browse the repository at this point in the history
Add and fix TypeScript tests
  • Loading branch information
vweevers committed Feb 8, 2018
2 parents e78a21a + c68595f commit 014ff8a
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 314 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -146,6 +146,12 @@ Provided with the current instance of `AbstractLevelDOWN` by default.
### AbstractChainedBatch#_serializeKey(key)
### AbstractChainedBatch#_serializeValue(value)

## TypeScript

[![experimental](https://img.shields.io/badge/stability-experimental-orange.svg)](https://nodejs.org/api/documentation.html#documentation_stability_index)

Ships with TypeScript definitions that enable automatic inference of options and key-value types on a typed `abstract-leveldown` implementation.

<a name="contributing"></a>
## Contributing

Expand Down
28 changes: 14 additions & 14 deletions abstract-chained-batch.js
Expand Up @@ -29,42 +29,41 @@ AbstractChainedBatch.prototype.put = function (key, value) {
key = this._serializeKey(key)
value = this._serializeValue(value)

if (typeof this._put === 'function') {
this._put(key, value)
} else {
this._operations.push({ type: 'put', key: key, value: value })
}
this._put(key, value)

return this
}

AbstractChainedBatch.prototype._put = function (key, value) {
this._operations.push({ type: 'put', key: key, value: value })
}

AbstractChainedBatch.prototype.del = function (key) {
this._checkWritten()

var err = this._db._checkKey(key, 'key')
if (err) { throw err }

key = this._serializeKey(key)

if (typeof this._del === 'function') {
this._del(key)
} else {
this._operations.push({ type: 'del', key: key })
}
this._del(key)

return this
}

AbstractChainedBatch.prototype._del = function (key) {
this._operations.push({ type: 'del', key: key })
}

AbstractChainedBatch.prototype.clear = function () {
this._checkWritten()

this._operations = []

if (typeof this._clear === 'function') { this._clear() }
this._clear()

return this
}

AbstractChainedBatch.prototype._clear = function noop () {}

AbstractChainedBatch.prototype.write = function (options, callback) {
this._checkWritten()

Expand All @@ -76,6 +75,7 @@ AbstractChainedBatch.prototype.write = function (options, callback) {

this._written = true

// @ts-ignore
if (typeof this._write === 'function') { return this._write(callback) }

if (typeof this._db._batch === 'function') {
Expand Down
20 changes: 9 additions & 11 deletions abstract-iterator.js
Expand Up @@ -22,19 +22,16 @@ AbstractIterator.prototype.next = function (callback) {
}

self._nexting = true
if (typeof self._next === 'function') {
return self._next(function () {
self._nexting = false
callback.apply(null, arguments)
})
}

process.nextTick(function () {
self._next(function () {
self._nexting = false
callback()
callback.apply(null, arguments)
})
}

AbstractIterator.prototype._next = function (callback) {
process.nextTick(callback)
}

AbstractIterator.prototype.end = function (callback) {
if (typeof callback !== 'function') {
throw new Error('end() requires a callback argument')
Expand All @@ -45,9 +42,10 @@ AbstractIterator.prototype.end = function (callback) {
}

this._ended = true
this._end(callback)
}

if (typeof this._end === 'function') { return this._end(callback) }

AbstractIterator.prototype._end = function (callback) {
process.nextTick(callback)
}

Expand Down
84 changes: 41 additions & 43 deletions abstract-leveldown.js
Expand Up @@ -34,20 +34,19 @@ AbstractLevelDOWN.prototype.open = function (options, callback) {
options.createIfMissing = options.createIfMissing !== false
options.errorIfExists = !!options.errorIfExists

if (typeof this._open === 'function') {
this.status = 'opening'
this._open(options, function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'open'
callback()
})
} else {
this.status = 'open'
process.nextTick(callback)
}
this.status = 'opening'
this._open(options, function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'open'
callback()
})
}

AbstractLevelDOWN.prototype._open = function (options, callback) {
process.nextTick(callback)
}

AbstractLevelDOWN.prototype.close = function (callback) {
Expand All @@ -58,20 +57,19 @@ AbstractLevelDOWN.prototype.close = function (callback) {
throw new Error('close() requires a callback argument')
}

if (typeof this._close === 'function') {
this.status = 'closing'
this._close(function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'closed'
callback()
})
} else {
this.status = 'closed'
process.nextTick(callback)
}
this.status = 'closing'
this._close(function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'closed'
callback()
})
}

AbstractLevelDOWN.prototype._close = function (callback) {
process.nextTick(callback)
}

AbstractLevelDOWN.prototype.get = function (key, options, callback) {
Expand All @@ -90,10 +88,10 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) {

options.asBuffer = options.asBuffer !== false

if (typeof this._get === 'function') {
return this._get(key, options, callback)
}
this._get(key, options, callback)
}

AbstractLevelDOWN.prototype._get = function (key, options, callback) {
process.nextTick(function () { callback(new Error('NotFound')) })
}

Expand All @@ -112,10 +110,10 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {

if (typeof options !== 'object') { options = {} }

if (typeof this._put === 'function') {
return this._put(key, value, options, callback)
}
this._put(key, value, options, callback)
}

AbstractLevelDOWN.prototype._put = function (key, value, options, callback) {
process.nextTick(callback)
}

Expand All @@ -133,10 +131,10 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) {

if (typeof options !== 'object') { options = {} }

if (typeof this._del === 'function') {
return this._del(key, options, callback)
}
this._del(key, options, callback)
}

AbstractLevelDOWN.prototype._del = function (key, options, callback) {
process.nextTick(callback)
}

Expand Down Expand Up @@ -180,10 +178,10 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
serialized[i] = e
}

if (typeof this._batch === 'function') {
return this._batch(serialized, options, callback)
}
this._batch(serialized, options, callback)
}

AbstractLevelDOWN.prototype._batch = function (array, options, callback) {
process.nextTick(callback)
}

Expand Down Expand Up @@ -227,11 +225,11 @@ function isEmptyBuffer (v) {

AbstractLevelDOWN.prototype.iterator = function (options) {
if (typeof options !== 'object') { options = {} }

options = this._setupIteratorOptions(options)
return this._iterator(options)
}

if (typeof this._iterator === 'function') { return this._iterator(options) }

AbstractLevelDOWN.prototype._iterator = function (options) {
return new AbstractIterator(this)
}

Expand Down
2 changes: 2 additions & 0 deletions abstract/put-get-del-test.js
@@ -1,3 +1,5 @@
'use strict'

var db
const verifyNotFoundError = require('./util').verifyNotFoundError
const testBuffer = Buffer.from('testbuffer')
Expand Down
7 changes: 7 additions & 0 deletions global.d.ts
@@ -0,0 +1,7 @@
// This file is required for the use of process.browser, which is
// not defined in @types/node but a property added by browserify.
declare namespace NodeJS {
interface Process {
browser: boolean;
}
}
6 changes: 2 additions & 4 deletions index.d.ts
Expand Up @@ -19,8 +19,6 @@ interface AbstractLevelDOWN<K=any, V=any, O=any, PO=any, GO=any, DO=any, IO=any,

iterator(options?: IO & AbstractIteratorOptions<K>): AbstractIterator<K, V>;

approximateSize(start: K, end: K, cb: (err: any, size: number) => void): void;

[index: string]: any;
}

Expand All @@ -42,7 +40,7 @@ export interface AbstractIteratorOptions<K=any> {
values?: boolean;
}

export type Batch<K=any, V=any> = PutBatch<K, V> | DelBatch<K>
export type Batch<K=any, V?=any> = PutBatch<K, V> | DelBatch<K>

export interface PutBatch<K=any, V=any> {
type: 'put',
Expand All @@ -56,6 +54,7 @@ export interface DelBatch<K=any, V=any> {
}

interface AbstractIterator<K=any, V=any> {
db: any;
next(callback: (err: any, key: K, value: V) => void): void;
end(callback: (err: any) => void): void;
}
Expand All @@ -82,4 +81,3 @@ interface AbstractChainedBatchConstructor {
export const AbstractLevelDOWN: AbstractLevelDOWNConstructor
export const AbstractIterator: AbstractIteratorConstructor
export const AbstractChainedBatch: AbstractChainedBatchConstructor
export function isLevelDOWN(db: any): boolean;
10 changes: 8 additions & 2 deletions package.json
Expand Up @@ -35,16 +35,22 @@
"xtend": "~4.0.0"
},
"devDependencies": {
"@types/node": "^9.3.0",
"rimraf": "^2.6.1",
"sinon": "^4.0.0",
"standard": "^10.0.3",
"tape": "^4.7.0"
"tape": "^4.7.0",
"ts-node": "^4.1.0",
"typescript": "^2.6.2"
},
"browser": {
"rimraf": false
},
"scripts": {
"test": "standard && node test.js"
"test": "standard && node test.js && npm run test-ts",
"test-ts": "npm run test-ts-types && npm run test-ts-suite",
"test-ts-types": "ts-node --type-check --no-cache test-types.ts",
"test-ts-suite": "ts-node --type-check --no-cache test.js"
},
"license": "MIT",
"engines": {
Expand Down
21 changes: 21 additions & 0 deletions test-types.ts
@@ -0,0 +1,21 @@
import { AbstractLevelDOWN } from '.'

const buf = Buffer.from('foo')

// Key and value generics
new AbstractLevelDOWN<string, string>('loc').put('key', 'value', (err: Error) => {})
new AbstractLevelDOWN<Buffer, Buffer>('loc').put(buf, buf, (err: Error) => {})
new AbstractLevelDOWN<number, object>('loc').put(1, {}, (err: Error) => {})

new AbstractLevelDOWN<string, string>('loc').get('key', (err: Error, value: string) => {})
new AbstractLevelDOWN<Buffer, Buffer>('loc').get(buf, (err: Error, value: Buffer) => {})
new AbstractLevelDOWN<number, object>('loc').get(1, (err: Error, value: object) => {})

// The generics default to "any"
new AbstractLevelDOWN('loc').put(1, {}, (err: Error) => {})
new AbstractLevelDOWN('loc').put('key', 'value', (err: Error) => {})
new AbstractLevelDOWN('loc').put(buf, false, (err: Error) => {})

new AbstractLevelDOWN('loc').get(1, (err: Error, value: any) => {})
new AbstractLevelDOWN('loc').get('key', (err: Error, value: any) => {})
new AbstractLevelDOWN('loc').get(buf, (err: Error, value: any) => {})

0 comments on commit 014ff8a

Please sign in to comment.