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

os agnostic #12

Merged
merged 9 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"David Dias <daviddias.p@gmail.com>",
"Erin Dachtler <download333@gmail.com>",
"Juan Batiz-Benet <juan@benet.ai>",
"dignifiedquire <dignifiedquire@gmail.com>"
"dignifiedquire <dignifiedquire@gmail.com>",
"Richard Schneider <makaretu@gmail.com>"
Copy link
Member

Choose a reason for hiding this comment

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

no need to do this, will be picked up from the commits on release

],
"bundleDependencies": []
}
}
21 changes: 12 additions & 9 deletions src/key.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* @flow */
'use strict'

const path = require('path')
const uuid = require('uuid/v4')

const pathSepS = path.sep
const pathSep = new Buffer(pathSepS, 'utf8')[0]
const pathSepS = '/'
const pathSepB = new Buffer(pathSepS, 'utf8')
const pathSep = pathSepB[0]

/**
* A Key represents the unique identifier of an object.
Expand Down Expand Up @@ -110,14 +110,12 @@ class Key {
this._buf = new Buffer(pathSepS, 'utf8')
}

this._buf = new Buffer(path.normalize(this.toString()))

if (this._buf[0] !== pathSep) {
this._buf = Buffer.concat([new Buffer(pathSepS, 'utf8'), this._buf])
this._buf = Buffer.concat([pathSepB, this._buf])
}

// normalize does not remove trailing slashes
if (this.toString().length > 1 && this._buf[this._buf.length - 1] === pathSep) {
while (this._buf.length > 1 && this._buf[this._buf.length - 1] === pathSep) {
this._buf = this._buf.slice(0, -1)
}
}
Expand Down Expand Up @@ -252,7 +250,12 @@ class Key {
*
*/
path () /* : Key */ {
return new Key(this.parent().toString() + pathSepS + this.type())
let p = this.parent().toString()
if (!p.endsWith(pathSepS)) {
p += pathSepS
}
p += this.type()
return new Key(p)
}

/**
Expand All @@ -268,7 +271,7 @@ class Key {
parent () /* : Key */ {
const list = this.list()
if (list.length === 1) {
return new Key(pathSepS, false)
return new Key(pathSepS)
}

return new Key(list.slice(0, -1).join(pathSepS))
Expand Down
11 changes: 5 additions & 6 deletions src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ const parallel = require('async/parallel')
const map = require('async/map')
const each = require('async/each')
const crypto = require('libp2p-crypto')
const path = require('path')

const Key = require('../src').Key
const n = (p) => path.normalize(p)

/* ::
import type {Datastore, Callback} from '../src'
Expand Down Expand Up @@ -238,9 +236,9 @@ module.exports = (test/* : Test */) => {
series([
(cb) => b.commit(cb),
(cb) => parallel([
(cb) => pull(check(store).query({prefix: n('/a')}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: n('/z')}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: n('/q')}), pull.collect(cb))
(cb) => pull(check(store).query({prefix: '/a'}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: '/z'}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: '/q'}), pull.collect(cb))
], (err, res) => {
expect(err).to.not.exist()
expect(res[0]).to.have.length(count)
Expand Down Expand Up @@ -290,7 +288,7 @@ module.exports = (test/* : Test */) => {

const tests = [
['empty', {}, [hello, world, hello2]],
['prefix', {prefix: n('/z')}, [world, hello2]],
['prefix', {prefix: '/z'}, [world, hello2]],
['1 filter', {filters: [filter1]}, [world, hello2]],
['2 filters', {filters: [filter1, filter2]}, [hello2]],
['limit', {limit: 1}, 1],
Expand Down Expand Up @@ -329,6 +327,7 @@ module.exports = (test/* : Test */) => {
const expected = t[2]
if (Array.isArray(expected)) {
if (t[1].orders == null) {
expect(res).to.have.length(expected.length)
const s = (a, b) => {
if (a.key.toString() < b.key.toString()) {
return 1
Expand Down
63 changes: 30 additions & 33 deletions test/key.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
'use strict'

const expect = require('chai').expect
const path = require('path')

const Key = require('../src').Key

const pathSep = path.sep
const n = (p) => path.normalize(p)
const pathSep = '/'

describe('Key', () => {
const clean = (s) => {
let fixed = n(s)
let fixed = s
if (fixed.startsWith(pathSep + pathSep)) {
fixed = fixed.slice(1)
}
Expand All @@ -34,7 +32,7 @@ describe('Key', () => {
ktype = lnparts.slice(0, -1).join(':')
}
const kname = lnparts[lnparts.length - 1]
const kchild = clean(fixed + n('/cchildd'))
const kchild = clean(fixed + '/cchildd')
const kparent = pathSep + namespaces.slice(0, -1).join(pathSep)
const kpath = clean(kparent + pathSep + ktype)
const kinstance = fixed + ':inst'
Expand Down Expand Up @@ -62,35 +60,33 @@ describe('Key', () => {
validKey('')
validKey('abcde')
validKey('disahfidsalfhduisaufidsail')
validKey(n('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/'))
validKey('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/')
validKey('4215432143214321432143214321')
validKey(n('a\\b\\c//d\\'))
validKey(n('/fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/'))
validKey('a/b/c/d/')
validKey('abcde:fdsfd')
validKey('disahfidsalfhduisaufidsail:fdsa')
validKey(n('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/:'))
validKey('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/:')
validKey('4215432143214321432143214321:')
validKey(n('fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/f:fdaf'))
})

it('ancestry', () => {
const k1 = new Key(n('/A/B/C'))
const k2 = new Key(n('/A/B/C/D'))
const k1 = new Key('/A/B/C')
const k2 = new Key('/A/B/C/D')

expect(k1.toString()).to.be.eql(n('/A/B/C'))
expect(k2.toString()).to.be.eql(n('/A/B/C/D'))
expect(k1.toString()).to.be.eql('/A/B/C')
expect(k2.toString()).to.be.eql('/A/B/C/D')

const checks = [
k1.isAncestorOf(k2),
k2.isDecendantOf(k1),
new Key(n('/A')).isAncestorOf(k2),
new Key(n('/A')).isAncestorOf(k1),
!new Key(n('/A')).isDecendantOf(k2),
!new Key(n('/A')).isDecendantOf(k1),
k2.isDecendantOf(new Key(n('/A'))),
k1.isDecendantOf(new Key(n('/A'))),
!k2.isAncestorOf(new Key(n('/A'))),
!k1.isAncestorOf(new Key(n('/A'))),
new Key('/A').isAncestorOf(k2),
new Key('/A').isAncestorOf(k1),
!new Key('/A').isDecendantOf(k2),
!new Key('/A').isDecendantOf(k1),
k2.isDecendantOf(new Key('/A')),
k1.isDecendantOf(new Key('/A')),
!k2.isAncestorOf(new Key('/A')),
!k1.isAncestorOf(new Key('/A')),
!k2.isAncestorOf(k2),
!k1.isAncestorOf(k1)
]
Expand All @@ -103,8 +99,8 @@ describe('Key', () => {
})

it('type', () => {
const k1 = new Key(n('/A/B/C:c'))
const k2 = new Key(n('/A/B/C:c/D:d'))
const k1 = new Key('/A/B/C:c')
const k2 = new Key('/A/B/C:c/D:d')

expect(k1.isAncestorOf(k2)).to.eql(true)
expect(k2.isDecendantOf(k1)).to.eql(true)
Expand All @@ -116,13 +112,14 @@ describe('Key', () => {

it('random', () => {
const keys = {}
for (let i = 0; i < 1000; i++) {
const k = 100
for (let i = 0; i < k; i++) {
const r = Key.random()
expect(keys).to.not.have.key(r.toString())
keys[r.toString()] = true
}

expect(Object.keys(keys)).to.have.length(1000)
expect(Object.keys(keys)).to.have.length(k)
})

it('less', () => {
Expand All @@ -134,12 +131,12 @@ describe('Key', () => {
expect(bk.less(ak)).to.eql(false)
}

checkLess(n('/a/b/c'), n('/a/b/c/d'))
checkLess(n('/a/b'), n('/a/b/c/d'))
checkLess(n('/a'), n('/a/b/c/d'))
checkLess(n('/a/a/c'), n('/a/b/c'))
checkLess(n('/a/a/d'), n('/a/b/c'))
checkLess(n('/a/b/c/d/e/f/g/h'), n('/b'))
checkLess(pathSep, n('/a'))
checkLess('/a/b/c', '/a/b/c/d')
checkLess('/a/b', '/a/b/c/d')
checkLess('/a', '/a/b/c/d')
checkLess('/a/a/c', '/a/b/c')
checkLess('/a/a/d', '/a/b/c')
checkLess('/a/b/c/d/e/f/g/h', '/b')
checkLess(pathSep, '/a')
})
})
4 changes: 4 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ describe('utils', () => {
'helloworld'
)
})

it('provides a temp folder', () => {
expect(utils.tmpdir()).to.not.equal('')
})
})