From 56ab1598fce8c2a9dc7551f3ae8f827599e2032c Mon Sep 17 00:00:00 2001 From: Frederick Corcino Alejo Date: Wed, 2 May 2018 12:56:44 -0400 Subject: [PATCH] Add isPublished function --- .gitignore | 2 ++ README.md | 1 + index.js | 1 + lib/isPublished/README.md | 20 ++++++++++++++++++++ lib/isPublished/index.js | 15 +++++++++++++++ lib/isPublished/index.test.js | 23 +++++++++++++++++++++++ 6 files changed, 62 insertions(+) create mode 100644 lib/isPublished/README.md create mode 100644 lib/isPublished/index.js create mode 100644 lib/isPublished/index.test.js diff --git a/.gitignore b/.gitignore index 00cbbdf..6ab1efe 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,5 @@ typings/ # dotenv environment variables file .env +# Webstorm +.idea diff --git a/README.md b/README.md index 4a0ad2b..8e60d18 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ npm install --save clayutils * **isDefaultComponent** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isDefaultComponent) * **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) * **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 26afce5..47b01e1 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ module.exports.getPrefix = require('./lib/getPrefix'); module.exports.isComponent = require('./lib/isComponent'); 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.isUser = require('./lib/isUser'); module.exports.replaceVersion = require('./lib/replaceVersion'); diff --git a/lib/isPublished/README.md b/lib/isPublished/README.md new file mode 100644 index 0000000..ad79ff4 --- /dev/null +++ b/lib/isPublished/README.md @@ -0,0 +1,20 @@ +### isPublished + +Check if the uri ends with '@published' + +#### Params + +* `uri` _string_ + +**Returns** _boolean_ + +#### Example + +```js +isPublished('nymag.com/press/_components/base/instances/foobarbaz@published') +//=> true + +isPublished('nymag.com/press/_components/base/instances/foobarbaz') +//=> false + +``` diff --git a/lib/isPublished/index.js b/lib/isPublished/index.js new file mode 100644 index 0000000..feee4ef --- /dev/null +++ b/lib/isPublished/index.js @@ -0,0 +1,15 @@ +'use strict'; + +const isUriStringCheck = require('../strCheck'), + validationRegex = /.+@published$/; + +/** + * First test if argument is a String. If true, test if the string ends with '@published'. + * Otherwise, throw an error. + * @param {string} uri + * @return {Boolean} + */ +module.exports = function (uri) { + isUriStringCheck.strCheck(uri); + return validationRegex.test(uri); +}; diff --git a/lib/isPublished/index.test.js b/lib/isPublished/index.test.js new file mode 100644 index 0000000..aa71515 --- /dev/null +++ b/lib/isPublished/index.test.js @@ -0,0 +1,23 @@ +'use strict'; + +const name = __filename.split('/').pop().split('.').shift(), + fn = require('./' + name), + expect = require('chai').expect; + +describe('isPublished', () => { + it('returns true if the reference is published', () => { + expect(fn('domain.com/_components/foo/instances/bar@published')).to.equal(true); + }); + + it('returns false if the reference is not published', () => { + expect(fn('domain.com/_components/foo/instances/bar')).to.equal(false); + }); + + 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); + }); +});