From 71588542893f234d7ef0037118308403e1894155 Mon Sep 17 00:00:00 2001 From: Jon Winton Date: Mon, 18 Jun 2018 10:47:58 -0400 Subject: [PATCH] isUri (#8) * adding test for Uri * fix name --- README.md | 1 + index.js | 1 + lib/isUri/README.md | 17 +++++++++++++++++ lib/isUri/index.js | 14 ++++++++++++++ lib/isUri/index.test.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 lib/isUri/README.md create mode 100644 lib/isUri/index.js create mode 100644 lib/isUri/index.test.js diff --git a/README.md b/README.md index 8e60d18..6f3f972 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ npm install --save clayutils * **isList** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isList) * **isPage** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isPage) * **isPublished** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isPublished) +* **isUri** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isUri) * **isUser** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isUser) * **jsonPrefixToSlug** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/jsonPrefixToSlug) * **jsonSlugToPrefix** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/jsonSlugToPrefix) diff --git a/index.js b/index.js index 47b01e1..625b5b9 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ module.exports.isDefaultComponent = require('./lib/isDefaultComponent'); module.exports.isPage = require('./lib/isPage'); module.exports.isPublished = require('./lib/isPublished'); module.exports.isList = require('./lib/isList'); +module.exports.isUri = require('./lib/isUri'); module.exports.isUser = require('./lib/isUser'); module.exports.replaceVersion = require('./lib/replaceVersion'); module.exports.uriPrefixToSlug = require('./lib/uriPrefixToSlug'); diff --git a/lib/isUri/README.md b/lib/isUri/README.md new file mode 100644 index 0000000..4bf89c2 --- /dev/null +++ b/lib/isUri/README.md @@ -0,0 +1,17 @@ +### isUri + +Check if '/_uris/' is in the uri + +#### Params + +* `uri` _string_ + +**Returns** _boolean_ + +#### Example + +```js +isUri('nymag.com/_uris/someuriid') +//=> true + +``` diff --git a/lib/isUri/index.js b/lib/isUri/index.js new file mode 100644 index 0000000..6d3c0c9 --- /dev/null +++ b/lib/isUri/index.js @@ -0,0 +1,14 @@ +'use strict'; + +const isUriStringCheck = require('../strCheck'); + +/** + * First test if argument is a String. If true, test if '/_uris/' is in the string. + * Otherwise, throw an error. + * @param {string} uri + * @return {Boolean} + */ +module.exports = function (uri) { + isUriStringCheck.strCheck(uri); + return uri.toLowerCase().indexOf('/_uris/') > -1; +}; diff --git a/lib/isUri/index.test.js b/lib/isUri/index.test.js new file mode 100644 index 0000000..294eae5 --- /dev/null +++ b/lib/isUri/index.test.js @@ -0,0 +1,28 @@ +'use strict'; + +const name = __filename.split('/').pop().split('.').shift(), + fn = require('./' + name), + expect = require('chai').expect; + +describe('isUri', () => { + it('returns true if uri reference', () => { + expect(fn('domain.com/_uris/foo')).to.equal(true); + }); + + it('returns true if uri instance reference', () => { + expect(fn('nymag.com/scienceofus/_uris/foobarbaz@published')).to.equal(true); + }); + + it('throws an error if the URI passed in is not a string', () => { + const nonStringArgument = function () { + return fn([0, 1, 2, 3]); + }; + + expect(nonStringArgument).to.throw(Error); + }); + + it('returns false if non-uri reference', () => { + expect(fn('domain.com/_users/foo')).to.equal(false); + expect(fn('domain.com/_components/foo')).to.equal(false); + }); +});