Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

feat: split .query into .query and .queryKeys #87

Merged
merged 6 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
- run: npx aegir test -t node --cov --bail
- uses: codecov/codecov-action@v1
test-chrome:
needs: check
Expand All @@ -50,7 +50,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
- run: npx aegir test -t browser -t webworker --bail -- --browser firefox
test-electron-main:
needs: check
runs-on: ubuntu-latest
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
"dist"
],
"scripts": {
"prepare": "aegir build --no-bundle && cp src/types.d.ts dist/src/types.d.ts",
"prepare": "aegir build --no-bundle",
"lint": "aegir lint",
"test": "aegir test",
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser",
"release": "aegir release --docs",
"release-minor": "aegir release --type minor --docs",
"release-major": "aegir release --type major --docs",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage --provider codecov",
"coverage": "aegir test --cov",
"docs": "aegir docs"
},
"repository": {
Expand Down Expand Up @@ -47,7 +46,11 @@
"iso-random-stream": "^2.0.0",
"it-all": "^1.0.2",
"it-drain": "^1.0.1",
"nanoid": "^3.0.2"
"it-filter": "^1.0.2",
"it-map": "^1.0.5",
"it-take": "^1.0.1",
"nanoid": "^3.0.2",
"uint8arrays": "^2.1.5"
},
"eslintConfig": {
"extends": "ipfs"
Expand Down
37 changes: 33 additions & 4 deletions src/adapter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
'use strict'

const { filter, sortAll, take, map } = require('./utils')
const { sortAll } = require('./utils')
const drain = require('it-drain')
const map = require('it-map')
const filter = require('it-filter')
const take = require('it-take')

/**
* @typedef {import('./key')} Key
* @typedef {import('./types').Pair} Pair
* @typedef {import('./types').Datastore} Datastore
* @typedef {import('./types').Options} Options
* @typedef {import('./types').Query} Query
* @typedef {import('./types').KeyQuery} KeyQuery
* @typedef {import('./types').Batch} Batch
*/

Expand Down Expand Up @@ -173,11 +177,36 @@ class Adapter {
it = take(it, q.limit)
}

if (q.keysOnly === true) {
return map(it, (e) => /** @type {Pair} */({ key: e.key }))
return it
}

/**
* @param {KeyQuery} q
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not simply using the Query type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query defines the Filter/Sort functions which use Pair/s as arguments and/or return types. I thought about using generics to define the arg/return types of those methods but it seemed more straightforward to just have another type.

* @param {Options} [options]
*/
queryKeys (q, options) {
/** @type {Query} */
const query = {
...q,
filters: [],
orders: []
}

return it
// override filters to just deal with keys
if (Array.isArray(q.filters)) {
query.filters = q.filters.map(filter => {
return (item) => filter(item.key)
})
}

// override orders to just deal with keys
if (Array.isArray(q.orders)) {
query.orders = q.orders.map(order => {
return (a, b) => order(a.key, b.key)
})
}

return map(this.query(query, options), ({ key }) => key)
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* @typedef {import('./types').Batch} Batch
* @typedef {import('./types').Options} Options
* @typedef {import('./types').Query} Query
* @typedef {import('./types').QueryFilter} QueryFilter
* @typedef {import('./types').QueryOrder} QueryOrder
* @typedef {import('./types').KeyQuery} KeyQuery
* @typedef {import('./types').KeyQueryFilter} KeyQueryFilter
* @typedef {import('./types').KeyQueryOrder} KeyQueryOrder
* @typedef {import('./types').Pair} Pair
*/

Expand Down
16 changes: 7 additions & 9 deletions src/key.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict'

const { nanoid } = require('nanoid')
const { utf8Encoder, utf8Decoder } = require('./utils')

const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')

const symbol = Symbol.for('@ipfs/interface-datastore/key')
const pathSepS = '/'
const pathSepB = utf8Encoder.encode(pathSepS)
const pathSepB = new TextEncoder().encode(pathSepS)
const pathSep = pathSepB[0]

/**
Expand All @@ -31,7 +33,7 @@ class Key {
*/
constructor (s, clean) {
if (typeof s === 'string') {
this._buf = utf8Encoder.encode(s)
this._buf = uint8ArrayFromString(s)
} else if (s instanceof Uint8Array) {
this._buf = s
} else {
Expand All @@ -54,15 +56,11 @@ class Key {
/**
* Convert to the string representation
*
* @param {string} [encoding='utf8'] - The encoding to use.
* @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
* @returns {string}
*/
toString (encoding = 'utf8') {
if (encoding === 'utf8' || encoding === 'utf-8') {
return utf8Decoder.decode(this._buf)
}

return new TextDecoder(encoding).decode(this._buf)
return uint8ArrayToString(this._buf, encoding)
}

/**
Expand Down
Loading