Comparing version 0.0.1 to 0.0.2
27
index.js
const base58 = require('bs58') | ||
const multihash = require('multihashes') | ||
const urlPattern = /^https?:\/\/[^\/]+\/ip(f|n)s\/(\w+)/ | ||
const urlPattern = /^https?:\/\/[^\/]+\/(ip(f|n)s)\/(\w+)/ | ||
@@ -16,3 +16,3 @@ function isMultihash (hash) { | ||
function isIPFSUrl (url) { | ||
function isIpfsUrl (url) { | ||
const match = url.match(urlPattern) | ||
@@ -23,10 +23,29 @@ if (!match) { | ||
const hash = match[2] | ||
if (match[1] !== 'ipfs') { | ||
return false | ||
} | ||
const hash = match[3] | ||
return isMultihash(hash) | ||
} | ||
function isIpnsUrl (url) { | ||
const match = url.match(urlPattern) | ||
if (!match) { | ||
return false | ||
} | ||
if (match[1] !== 'ipns') { | ||
return false | ||
} | ||
return true | ||
} | ||
module.exports = { | ||
multihash: isMultihash, | ||
url: isIPFSUrl, | ||
ipfsUrl: isIpfsUrl, | ||
ipnsUrl: isIpnsUrl, | ||
url: (url) => (isIpfsUrl(url) || isIpnsUrl(url)), | ||
urlPattern: urlPattern | ||
} |
{ | ||
"name": "is-ipfs", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A set of utilities to help identify IPFS resources", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,3 +7,3 @@ is-ipfs | ||
A set of utilities to help identify IPFS resources. | ||
A set of utilities to help identify [IPFS](https://ipfs.io/) resources. | ||
@@ -23,5 +23,12 @@ | ||
isIPFS.url('http://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // true | ||
isIPFS.url('https://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // true | ||
isIPFS.url('https://ipfs.io/ipfs/github.com') // true | ||
isIPFS.url('https://github.com/ipfs/js-ipfs/blob/master/README.md') // false | ||
isIPFS.url('https://google.com') // false | ||
isIPFS.ipfsUrl('https://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // true | ||
isIPFS.ipfsUrl('https://ipfs.io/ipfs/github.com') // false | ||
isIPFS.ipnsUrl('https://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // false | ||
isIPFS.ipnsUrl('https://ipfs.io/ipfs/github.com') // true | ||
``` | ||
@@ -35,8 +42,17 @@ | ||
### `isIPFS.url(hash)` | ||
### `isIPFS.url(url)` | ||
Returns `true` if the provided string is a valid IPFS or IPNS url or `false` otherwise. | ||
### `isIPFS.ipfsUrl(url)` | ||
Returns `true` if the provided string is a valid IPFS url or `false` otherwise. | ||
**Note:** the regex used for this check is also exported as `isIPFS.urlPattern` | ||
### `isIPFS.ipnsUrl(url)` | ||
Returns `true` if the provided string is a valid IPNS url or `false` otherwise. | ||
**Note:** the regex used for these checks is also exported as `isIPFS.urlPattern` | ||
## License | ||
@@ -43,0 +59,0 @@ |
69
test.js
@@ -18,3 +18,59 @@ const test = require('tape') | ||
test('isIPFS.url should match an ipfs.io url', function (t) { | ||
test('isIPFS.ipfsUrl should match an ipfs url', function (t) { | ||
const actual = isIPFS.ipfsUrl('http://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') | ||
const expected = true | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.ipfsUrl should not match an ipns url', function (t) { | ||
const actual = isIPFS.ipfsUrl('http://ipfs.io/ipns/github.com/') | ||
const expected = false | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.ipfsUrl should not match a github ipfs repo url', function (t) { | ||
const actual = isIPFS.ipfsUrl('https://github.com/ipfs/js-ipfs/blob/master/README.md') | ||
const expected = false | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.ipfsUrl should not match an google url', function (t) { | ||
const actual = isIPFS.ipfsUrl('https://google.com') | ||
const expected = false | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.ipnsUrl should not match an ipfs url', function (t) { | ||
const actual = isIPFS.ipnsUrl('http://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') | ||
const expected = false | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.ipnsUrl should not match an ipns url', function (t) { | ||
const actual = isIPFS.ipnsUrl('http://ipfs.io/ipns/github.com/') | ||
const expected = true | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.ipnsUrl should not match a github ipfs repo url', function (t) { | ||
const actual = isIPFS.ipnsUrl('https://github.com/ipfs/js-ipfs/blob/master/README.md') | ||
const expected = false | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.ipnsUrl should not match an google url', function (t) { | ||
const actual = isIPFS.ipnsUrl('https://google.com') | ||
const expected = false | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.url should match an ipfs url', function (t) { | ||
const actual = isIPFS.url('http://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') | ||
@@ -26,4 +82,11 @@ const expected = true | ||
test('isIPFS.url should match an ipns url', function (t) { | ||
const actual = isIPFS.url('http://ipfs.io/ipns/github.com/') | ||
const expected = true | ||
t.is(actual, expected) | ||
t.end() | ||
}) | ||
test('isIPFS.url should not match a github ipfs repo url', function (t) { | ||
const actual = isIPFS.multihash('https://github.com/ipfs/js-ipfs/blob/master/README.md') | ||
const actual = isIPFS.url('https://github.com/ipfs/js-ipfs/blob/master/README.md') | ||
const expected = false | ||
@@ -35,3 +98,3 @@ t.is(actual, expected) | ||
test('isIPFS.url should not match an google url', function (t) { | ||
const actual = isIPFS.multihash('https://google.com') | ||
const actual = isIPFS.url('https://google.com') | ||
const expected = false | ||
@@ -38,0 +101,0 @@ t.is(actual, expected) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6840
126
59