Skip to content

Commit

Permalink
Add isPublished function
Browse files Browse the repository at this point in the history
  • Loading branch information
frederickcxa committed May 2, 2018
1 parent 7dfef0e commit 56ab159
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ typings/
# dotenv environment variables file
.env

# Webstorm
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
20 changes: 20 additions & 0 deletions lib/isPublished/README.md
Original file line number Diff line number Diff line change
@@ -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

```
15 changes: 15 additions & 0 deletions lib/isPublished/index.js
Original file line number Diff line number Diff line change
@@ -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);
};
23 changes: 23 additions & 0 deletions lib/isPublished/index.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 56ab159

Please sign in to comment.