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..daa2ab1 --- /dev/null +++ b/lib/isPublished/README.md @@ -0,0 +1,20 @@ +### isPublished + +Check if '@published' is in the uri + +#### 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..3b5dfda --- /dev/null +++ b/lib/isPublished/index.js @@ -0,0 +1,14 @@ +'use strict'; + +const isUriStringCheck = require('../strCheck'); + +/** + * First test if argument is a String. If true, test if '@published' is in the string. + * Otherwise, throw an error. + * @param {string} uri + * @return {Boolean} + */ +module.exports = function (uri) { + isUriStringCheck.strCheck(uri); + return uri.indexOf('@published') !== -1; +}; 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); + }); +});