Skip to content

Commit beb6f67

Browse files
committed
Add tests
1 parent b725dff commit beb6f67

22 files changed

+778
-332
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ jobs:
1818
run: npm install
1919
- name: Test
2020
run: npm test
21-
# - name: Coverage
22-
# run: npm run coverage
23-
# - name: Codecov
24-
# uses: codecov/codecov-action@v2
25-
# with:
26-
# file: coverage/lcov.info
21+
- name: Coverage
22+
run: npm run coverage
23+
- name: Codecov
24+
uses: codecov/codecov-action@v2
25+
with:
26+
file: coverage/lcov.info

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
**Encode data with built-in or custom encodings.** The (not yet official) successor to `level-codec`, that introduces "transcoders" to translate between encodings and internal data formats supported by a db. This allows a db to store keys and values in a format of its choice (Buffer, Uint8Array or String) with zero-effort support of all known encodings.
44

55
[![level badge][level-badge]](https://github.com/Level/awesome)
6+
[![Test](https://img.shields.io/github/workflow/status/Level/transcoder/Test?label=test)](https://github.com/Level/transcoder/actions/workflows/test.yml)
7+
[![Coverage](https://img.shields.io/codecov/c/github/Level/transcoder?label=&logo=codecov&logoColor=fff)](https://codecov.io/gh/Level/transcoder)
68
[![Standard](https://img.shields.io/badge/standard-informational?logo=javascript&logoColor=fff)](https://standardjs.com)
79
[![Common Changelog](https://common-changelog.org/badge.svg)](https://common-changelog.org)
810
[![Donate](https://img.shields.io/badge/donate-orange?logo=open-collective&logoColor=fff)](https://opencollective.com/level)
@@ -150,7 +152,7 @@ The `type` string should be a unique name.
150152

151153
## Contributing
152154

153-
[`Level/codec`](https://github.com/Level/codec) is an **OPEN Open Source Project**. This means that:
155+
[`Level/transcoder`](https://github.com/Level/transcoder) is an **OPEN Open Source Project**. This means that:
154156

155157
> Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
156158

index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ declare class Transcoder<T = any> {
88
constructor (formats: Iterable<string>)
99

1010
/**
11-
* Get the types of supported encodings, including transcoded encodings.
11+
* Get the types of supported encodings.
12+
* @param full Return fully qualified types with the formats of trancoded encodings (if any).
1213
*/
13-
types (): string[]
14+
types (full?: boolean): string[]
1415

1516
/**
1617
* Get the given encoding, creating a transcoder if necessary.

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class Transcoder {
1414
* @param {Iterable<string>} formats
1515
*/
1616
constructor (formats) {
17+
if (formats == null ||
18+
typeof formats[Symbol.iterator] !== 'function' ||
19+
typeof formats === 'string') {
20+
throw new TypeError("The first argument 'formats' must be an Iterable")
21+
}
22+
1723
/** @type {Map<string|Encoding<any, any, any>|EncodingOptions<any, any, any>, Encoding<any, any, any>>} */
1824
this[kEncodings] = new Map()
1925
this[kFormats] = new Set(formats)
@@ -32,17 +38,20 @@ class Transcoder {
3238
try {
3339
this.encoding(k)
3440
} catch (err) {
41+
/* istanbul ignore if: assertion */
3542
if (err.code !== 'LEVEL_ENCODING_NOT_SUPPORTED') throw err
3643
}
3744
}
3845
}
3946

40-
types () {
47+
/**
48+
* @param {boolean} [full]
49+
*/
50+
types (full) {
4151
const types = new Set()
4252

4353
for (const encoding of this[kEncodings].values()) {
44-
const type = encoding.type.split('+')[0]
45-
if (type) types.add(type)
54+
types.add(full ? encoding.type : encoding.type.split('+')[0])
4655
}
4756

4857
return Array.from(types)
@@ -67,7 +76,7 @@ class Transcoder {
6776
)
6877
}
6978
} else if (typeof encoding !== 'object' || encoding === null) {
70-
throw new TypeError('Encoding must be a string or object')
79+
throw new TypeError("First argument 'encoding' must be a string or object")
7180
} else if (encoding instanceof Encoding) {
7281
resolved = encoding
7382
} else if (encoding.format === 'view') {
@@ -89,15 +98,16 @@ class Transcoder {
8998
} else if (this[kFormats].has('buffer')) {
9099
resolved = resolved.transcode('buffer')
91100
} else {
101+
// TODO: improve error message (see tests, it's inconsistent)
92102
throw new ModuleError(
93-
`Encoding '${type}' or '${format}' is not supported`,
103+
`Encoding '${type}' is not supported`,
94104
{ code: 'LEVEL_ENCODING_NOT_SUPPORTED' }
95105
)
96106
}
97107
}
98108

99109
for (const k of [encoding, type, resolved.type]) {
100-
if (k) this[kEncodings].set(k, resolved)
110+
this[kEncodings].set(k, resolved)
101111
}
102112
}
103113

lib/encoding.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class Encoding {
2121
createBufferTranscoder
2222
}) {
2323
if (typeof encode !== 'function' && encode !== undefined) {
24-
throw new TypeError("The 'encode' option must be a function")
24+
throw new TypeError("The 'encode' option must be a function or undefined")
2525
}
2626

2727
if (typeof decode !== 'function' && decode !== undefined) {
28-
throw new TypeError("The 'decode' option must be a function")
28+
throw new TypeError("The 'decode' option must be a function or undefined")
2929
}
3030

3131
if (typeof type !== 'string' && type !== undefined) {
@@ -90,7 +90,7 @@ class Encoding {
9090
}
9191

9292
throw new ModuleError(
93-
`Encoding '${format}' is not supported`,
93+
`Encoding '${this.type}' cannot be transcoded to '${format}'`,
9494
{ code: 'LEVEL_ENCODING_NOT_SUPPORTED' }
9595
)
9696
}

lib/formats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class NativeFormat extends Encoding {
116116
* @param {string} nativeType
117117
*/
118118
constructor (nativeType) {
119-
super({ type: `native+${nativeType}`, idempotent: true, format: nativeType })
119+
super({ type: `${nativeType}+native`, idempotent: true, format: nativeType })
120120
}
121121
}
122122

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "index.js",
77
"types": "./index.d.ts",
88
"scripts": {
9-
"test": "ts-standard && tsc && standard && hallmark && nyc tape test/skip-*.js",
9+
"test": "ts-standard && tsc && standard && hallmark && nyc tape test/*.js",
1010
"test-browsers-local": "airtap --coverage --verbose test/*.js",
1111
"coverage": "nyc report -r lcovonly",
1212
"hallmark": "hallmark --fix",

test/as-buffer.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/batch.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/codec.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)