-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils for layout and page metadata (#11)
* add isDefaultLayout * get page instance without /meta * get layout instance without /meta * add isPageMeta and isLayoutMeta
- Loading branch information
1 parent
728cf44
commit 55cfc0a
Showing
15 changed files
with
206 additions
and
4 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
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
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
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 @@ | ||
### isDefaultLayout | ||
|
||
Check if a uri points to a DEFAULT instance of a layout | ||
|
||
#### Params | ||
|
||
* `uri` _string_ | ||
|
||
**Returns** _boolean_ | ||
|
||
#### Example | ||
|
||
```js | ||
isDefaultLayout('nymag.com/press/_layouts/base') | ||
//=> true | ||
|
||
``` |
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,15 @@ | ||
'use strict'; | ||
|
||
const isUriStringCheck = require('../strCheck'); | ||
|
||
/** | ||
* First test if argument passed in is a String. If true, determine if a uri points | ||
* to a DEFAULT instance of a layout. Otherwise, throw an error. | ||
* | ||
* @param {string} uri | ||
* @return {Boolean} | ||
*/ | ||
module.exports = function (uri) { | ||
isUriStringCheck.strCheck(uri); | ||
return !!uri.match(/\/_layouts\/[A-Za-z0-9\-]+$/); | ||
}; |
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,29 @@ | ||
'use strict'; | ||
|
||
const name = __filename.split('/').pop().split('.').shift(), | ||
fn = require('./' + name), | ||
expect = require('chai').expect; | ||
|
||
describe('isDefaultLayout', () => { | ||
it('returns true if default layout reference', () => { | ||
expect(fn('domain.com/_layouts/foo')).to.equal(true); | ||
}); | ||
|
||
it('returns false if layout instance reference', () => { | ||
expect(fn('domain.com/_layouts/foo/instances/bar')).to.equal(false); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
it('returns false if non-layout reference', () => { | ||
expect(fn('domain.com/_users/foo')).to.equal(false); | ||
expect(fn('domain.com/_pages/foo')).to.equal(false); | ||
expect(fn('domain.com/_components/foo')).to.equal(false); | ||
}); | ||
}); |
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 @@ | ||
### isLayoutMeta | ||
|
||
Check if '/_layouts/:name/instances/:id/meta' is in the uri | ||
|
||
#### Params | ||
|
||
* `uri` _string_ | ||
|
||
**Returns** _boolean_ | ||
|
||
#### Example | ||
|
||
```js | ||
isPage('domain.com/_layouts/foo/instances/bar/meta') | ||
//=> true | ||
|
||
``` |
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,16 @@ | ||
'use strict'; | ||
|
||
const isUriStringCheck = require('../strCheck'), | ||
isLayout = require('../isLayout'), | ||
getLayoutInstance = require('../getLayoutInstance'); | ||
|
||
/** | ||
* First test if argument is a String. If true, test if '/_layouts/:name/instances/:id/meta' is in the string. | ||
* Otherwise, throw an error. | ||
* @param {string} uri | ||
* @return {Boolean} | ||
*/ | ||
module.exports = function (uri) { | ||
isUriStringCheck.strCheck(uri); | ||
return isLayout(uri) && !!getLayoutInstance(uri) && !!uri.match(/\/meta$/i); | ||
}; |
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,33 @@ | ||
'use strict'; | ||
|
||
const name = __filename.split('/').pop().split('.').shift(), | ||
fn = require('./' + name), | ||
expect = require('chai').expect; | ||
|
||
describe('isLayoutMeta', () => { | ||
it('returns false if default', () => { | ||
expect(fn('domain.com/_layouts/foo/meta')).to.equal(false); | ||
}); | ||
|
||
it('returns false if no meta', () => { | ||
expect(fn('domain.com/_layouts/foo/instances/bar')).to.equal(false); | ||
}); | ||
|
||
it('returns true if meta', () => { | ||
expect(fn('domain.com/sitepath/_layouts/foo/instances/bar/meta')).to.equal(true); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
it('returns false if non-layout reference', () => { | ||
expect(fn('domain.com/_users/foo')).to.equal(false); | ||
expect(fn('domain.com/_components/foo')).to.equal(false); | ||
expect(fn('domain.com/_pages/foo/meta')).to.equal(false); | ||
}); | ||
}); |
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 @@ | ||
### isPageMeta | ||
|
||
Check if '/_pages/:id/meta' is in the uri | ||
|
||
#### Params | ||
|
||
* `uri` _string_ | ||
|
||
**Returns** _boolean_ | ||
|
||
#### Example | ||
|
||
```js | ||
isPage('domain.com/_pages/foobar/meta') | ||
//=> true | ||
|
||
``` |
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,15 @@ | ||
'use strict'; | ||
|
||
const isUriStringCheck = require('../strCheck'), | ||
isPage = require('../isPage'); | ||
|
||
/** | ||
* First test if argument is a String. If true, test if '/_pages/:id/meta' is in the string. | ||
* Otherwise, throw an error. | ||
* @param {string} uri | ||
* @return {Boolean} | ||
*/ | ||
module.exports = function (uri) { | ||
isUriStringCheck.strCheck(uri); | ||
return isPage(uri) && !!uri.match(/\/meta$/i); | ||
}; |
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,29 @@ | ||
'use strict'; | ||
|
||
const name = __filename.split('/').pop().split('.').shift(), | ||
fn = require('./' + name), | ||
expect = require('chai').expect; | ||
|
||
describe('isPageMeta', () => { | ||
it('returns false if no meta', () => { | ||
expect(fn('domain.com/_pages/foo')).to.equal(false); | ||
}); | ||
|
||
it('returns true if meta', () => { | ||
expect(fn('domain.com/sitepath/_pages/foobarbaz/meta')).to.equal(true); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
it('returns false if non-page reference', () => { | ||
expect(fn('domain.com/_users/foo')).to.equal(false); | ||
expect(fn('domain.com/_components/foo')).to.equal(false); | ||
expect(fn('domain.com/_layouts/foo/instances/bar/meta')).to.equal(false); | ||
}); | ||
}); |