Update code structure

This commit is contained in:
Yarmo Mackenbach 2020-10-24 19:10:28 +02:00
parent 1b1d11c802
commit c262f96d4f
3 changed files with 26 additions and 27 deletions

View file

@ -16,20 +16,7 @@ limitations under the License.
const validUrl = require('valid-url')
const bent = require('bent')
const req = bent('GET')
const { serviceprovidersList, serviceproviders } = require('./serviceproviders')
const matchServiceproviders = (uri) => {
let matches = [], sp
serviceprovidersList.forEach((spName, i) => {
sp = serviceproviders[spName]
if (sp.reURI.test(uri)) {
matches.push(sp.processURI(uri))
}
})
return matches
}
const serviceproviders = require('./serviceproviders')
const verify = async (uri, fingerprint, opts) => {
if (!opts) { opts = {} }
@ -38,7 +25,7 @@ const verify = async (uri, fingerprint, opts) => {
throw new Error('Not a valid URI')
}
const spMatches = matchServiceproviders(uri)
const spMatches = serviceproviders.match(uri)
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches
@ -72,4 +59,3 @@ const verify = async (uri, fingerprint, opts) => {
exports.verify = verify
exports.serviceproviders = serviceproviders
exports.serviceprovidersList = serviceprovidersList

View file

@ -13,16 +13,29 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
exports.serviceprovidersList = [
exports.list = [
'dns',
'xmpp',
'twitter',
'hackernews',
]
exports.serviceproviders = {
exports.data = {
dns: require('./serviceproviders/dns'),
xmpp: require('./serviceproviders/xmpp'),
twitter: require('./serviceproviders/twitter'),
hackernews: require('./serviceproviders/hackernews'),
}
exports.match = (uri) => {
let matches = [], sp
serviceprovidersList.forEach((spName, i) => {
sp = serviceproviders[spName]
if (sp.reURI.test(uri)) {
matches.push(sp.processURI(uri))
}
})
return matches
}

View file

@ -18,30 +18,30 @@ const assert = require('chai').assert
const request = require('supertest')
const doipjs = require('../src')
doipjs.serviceprovidersList.forEach((sp, i) => {
doipjs.serviceproviders.list.forEach((sp, i) => {
describe(`serviceproviders.${sp}`, () => {
it('should be an object', () => {
expect(doipjs.serviceproviders[sp]).to.be.a('object')
expect(doipjs.serviceproviders.data[sp]).to.be.a('object')
})
it('should have a RegExp instance named "reURI"', () => {
expect(doipjs.serviceproviders[sp].reURI).to.be.instanceof(RegExp)
expect(doipjs.serviceproviders.data[sp].reURI).to.be.instanceof(RegExp)
})
it('should have a function named "processURI" (2 arguments)', () => {
expect(doipjs.serviceproviders[sp].processURI).to.be.a('function')
expect(doipjs.serviceproviders[sp].processURI).to.have.length(2)
expect(doipjs.serviceproviders.data[sp].processURI).to.be.a('function')
expect(doipjs.serviceproviders.data[sp].processURI).to.have.length(2)
})
it('should have an array named "tests"', () => {
expect(doipjs.serviceproviders[sp].tests).to.be.instanceof(Array)
expect(doipjs.serviceproviders.data[sp].tests).to.be.instanceof(Array)
})
doipjs.serviceproviders[sp].tests.forEach((test, j) => {
doipjs.serviceproviders.data[sp].tests.forEach((test, j) => {
if (test.shouldMatch) {
it(`should match "${test.uri}"`, () => {
expect(doipjs.serviceproviders[sp].reURI.test(test.uri)).to.be.true
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.true
})
} else {
it(`should not match "${test.uri}"`, () => {
expect(doipjs.serviceproviders[sp].reURI.test(test.uri)).to.be.false
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.false
})
}
})