-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from nymag/grab-page-instance
getPageInstance util
- Loading branch information
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |