Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update deps and types #79

Merged
merged 2 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"devDependencies": {
"@types/chai": "^4.2.8",
"@types/mocha": "^8.0.0",
"aegir": "^32.0.2",
"aegir": "^33.0.0",
"uint8arrays": "^2.0.5",
"util": "^0.12.3"
},
"dependencies": {
"multiaddr": "^8.0.0"
"multiaddr": "^9.0.1"
},
"contributors": [
"David Dias <[email protected]>",
Expand Down
52 changes: 44 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use strict'

const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')

/**
* @typedef {(a: Multiaddr | string) => boolean} MatchesFunction
* @typedef {(a: string[]) => boolean | null | string[]} PartialMatchesFunction
*/

/*
* Valid combinations
Expand Down Expand Up @@ -144,25 +149,44 @@ exports.IPFS = P2P
* Validation funcs
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* @param {PartialMatchesFunction} partialMatch
*/
function makeMatchesFunction (partialMatch) {
return function matches (a) {
if (!multiaddr.isMultiaddr(a)) {
/**
* @type {MatchesFunction}
*/
function matches (a) {
if (!Multiaddr.isMultiaddr(a)) {
try {
a = multiaddr(a)
a = new Multiaddr(a)
} catch (err) { // catch error
return false // also if it's invalid it's propably not matching as well so return false
return false // also if it's invalid it's probably not matching as well so return false
}
}
const out = partialMatch(a.protoNames())

if (out === null) {
return false
}

if (out === true || out === false) {
return out
}

return out.length === 0
}

return matches
}

function and () {
const args = Array.from(arguments)
/**
* @param {...any} args
*/
function and (...args) {
/**
* @type {PartialMatchesFunction}
*/
function partialMatch (a) {
if (a.length < args.length) {
return null
Expand Down Expand Up @@ -192,6 +216,9 @@ function and () {
function or () {
const args = Array.from(arguments)

/**
* @type {PartialMatchesFunction}
*/
function partialMatch (a) {
let out = null
args.some((arg) => {
Expand All @@ -218,13 +245,19 @@ function or () {
return result
}

/**
* @param {string} n
*/
function base (n) {
const name = n

/**
* @type {MatchesFunction}
*/
function matches (a) {
if (typeof a === 'string') {
try {
a = multiaddr(a)
a = new Multiaddr(a)
} catch (err) { // catch error
return false // also if it's invalid it's propably not matching as well so return false
}
Expand All @@ -237,6 +270,9 @@ function base (n) {
return false
}

/**
* @type {PartialMatchesFunction}
*/
function partialMatch (protos) {
if (protos.length === 0) {
return null
Expand Down
20 changes: 13 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict'

const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')
const uint8ArrayFromString = require('uint8arrays/from-string')

const mafmt = require('./../src')
Expand Down Expand Up @@ -190,13 +190,16 @@ describe('multiaddr validation', function () {
'/dns6/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
].concat(goodCircuit)

function assertMatches (p) {
const tests = Array.from(arguments).slice(1)
/**
* @param {mafmt.Mafmt} p
* @param {...string[]} tests
*/
function assertMatches (p, ...tests) {
tests.forEach(function (test) {
test.forEach(function (testcase) {
try {
expect(p.matches(testcase), `assertMatches: ${testcase} (string)`).to.be.eql(true)
const ma = multiaddr(testcase)
const ma = new Multiaddr(testcase)
expect(p.matches(ma), `assertMatches: ${testcase} (multiaddr object)`).to.be.eql(true)
expect(p.matches(ma.bytes), `assertMatches: ${testcase} (multiaddr.bytes)`).to.be.eql(true)
} catch (err) {
Expand All @@ -207,8 +210,11 @@ describe('multiaddr validation', function () {
})
}

function assertMismatches (p) {
const tests = Array.from(arguments).slice(1)
/**
* @param {mafmt.Mafmt} p
* @param {...string[]} tests
*/
function assertMismatches (p, ...tests) {
tests.forEach(function (test) {
test.forEach(function (testcase) {
try {
Expand All @@ -217,7 +223,7 @@ describe('multiaddr validation', function () {
try {
// if testcase string happens to be a valid multiaddr,
// we expect 'p' test to also return false for Multiaddr object and Uint8Array versions
validMultiaddrObj = multiaddr(testcase)
validMultiaddrObj = new Multiaddr(testcase)
} catch (e) {
// Ignoring testcase as the string is not a multiaddr
// (There is a separate 'Uint8Array is invalid' test later below)
Expand Down
38 changes: 5 additions & 33 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@

{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"target": "ES5",
"noImplicitAny": false,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"allowJs": true,
"checkJs": true,
"baseUrl": ".",
"paths": {
"mafmt": [
"./src",
"../src"
]
},
"types": [
"node",
"mocha",
"chai"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
"outDir": "dist"
},
"files": [
"./src/index.d.ts"
],
"include": [
"./test/**/*.spec.js"
"src",
"test"
]
}
}