Skip to content

Commit

Permalink
Merge pull request #26 from nymag/grab-page-instance
Browse files Browse the repository at this point in the history
getPageInstance util
  • Loading branch information
Allyson Young authored Apr 28, 2017
2 parents c9265d7 + b386b15 commit a1f50e6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ npm install --save clay-utils
* **getComponentInstance** [(code|tests|docs)](https://github.com/nymag/clay-utils/tree/master/lib/getComponentInstance)
* **getComponentName** [(code|tests|docs)](https://github.com/nymag/clay-utils/tree/master/lib/getComponentName)
* **getComponentVersion** [(code|tests|docs)](https://github.com/nymag/clay-utils/tree/master/lib/getComponentVersion)
* **getPageInstance** [(code|tests|docs)](https://github.com/nymag/clay-utils/tree/master/lib/getPageInstance)
* **isComponent** [(code|tests|docs)](https://github.com/nymag/clay-utils/tree/master/lib/isComponent)
* **isDefaultComponent** [(code|tests|docs)](https://github.com/nymag/clay-utils/tree/master/lib/isDefaultComponent)
* **replaceVersion** [(code|tests|docs)](https://github.com/nymag/clay-utils/tree/master/lib/replaceVersion)
17 changes: 17 additions & 0 deletions lib/getPageInstance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### getPageInstance

Get page instance from uri

#### Params

* `uri` _string_

**Returns** _string|null_

#### Example

```js
getPageInstance('nymag.com/scienceofus/pages/foobarbaz@published')
//=> 'foobarbaz@published'

```
17 changes: 17 additions & 0 deletions lib/getPageInstance/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const isUriStringCheck = require('../strCheck');

/**
* First test if argument passed in is a String. If true, get page instance
* from uri that includes page version. Otherwise, throw an error.
* @example /pages/cj21ud3rt00wmqpyefc944hez@published returns cj21ud3rt00wmqpyefc944hez@published
* @param {string} uri
* @return {string|null}
*/
module.exports = function (uri) {
isUriStringCheck.strCheck(uri);
const result = /\/pages\/(.*)/.exec(uri);

return result && result[1];
};
31 changes: 31 additions & 0 deletions lib/getPageInstance/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const name = __filename.split('/').pop().split('.').shift(),
fn = require('./' + name),
expect = require('chai').expect;

describe('getPageInstance', () => {
it('gets instance from uri', function () {
expect(fn('/pages/foobar')).to.equal('foobar');
});

it('gets instance from uri with version', function () {
expect(fn('/pages/foobar@published')).to.equal('foobar@published');
});

it('gets instance from full uri', function () {
expect(fn('nymag.com/scienceofus/pages/foobarbaz@published')).to.equal('foobarbaz@published');
});

it('CANNOT get instance from a non-page uri', function () {
expect(fn('/components/base/instances/0')).to.not.equal('0');
});

it('throws an error if argument passed in is not a String', () => {
const nonStringArgument = function () {
return fn([1, 2, 3, 4]);
};

expect(nonStringArgument).to.throw(Error);
});
});

0 comments on commit a1f50e6

Please sign in to comment.