Skip to content

Commit

Permalink
utils for layout and page metadata (#11)
Browse files Browse the repository at this point in the history
* add isDefaultLayout

* get page instance without /meta

* get layout instance without /meta

* add isPageMeta and isLayoutMeta
  • Loading branch information
nelsonpecora authored Jul 5, 2018
1 parent 728cf44 commit 55cfc0a
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ npm install --save clayutils
* **getPrefix** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getPrefix)
* **isComponent** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isComponent)
* **isDefaultComponent** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isDefaultComponent)
* **isDefaultLayout** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isDefaultLayout)
* **isLayout** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isLayout)
* **isLayoutMeta** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isLayoutMeta)
* **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)
* **isPageMeta** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isPageMeta)
* **isPublished** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isPublished)
* **isUri** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isUri)
* **isUser** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isUser)
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ module.exports.getListInstance = require('./lib/getListInstance');
module.exports.getPrefix = require('./lib/getPrefix');
module.exports.isComponent = require('./lib/isComponent');
module.exports.isLayout = require('./lib/isLayout');
module.exports.isLayoutMeta = require('./lib/isLayoutMeta');
module.exports.isDefaultComponent = require('./lib/isDefaultComponent');
module.exports.isDefaultLayout = require('./lib/isDefaultLayout');
module.exports.isPage = require('./lib/isPage');
module.exports.isPageMeta = require('./lib/isPageMeta');
module.exports.isPublished = require('./lib/isPublished');
module.exports.isList = require('./lib/isList');
module.exports.isUri = require('./lib/isUri');
Expand Down
2 changes: 1 addition & 1 deletion lib/getLayoutInstance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const isUriStringCheck = require('../strCheck');
*/
module.exports = function (uri) {
isUriStringCheck.strCheck(uri);
const result = /\/_layouts\/.+?\/instances\/([^\.@]+)/.exec(uri);
const result = /\/_layouts\/.+?\/instances\/([^\.\/@]+)/.exec(uri);

return result && result[1];
};
6 changes: 5 additions & 1 deletion lib/getLayoutInstance/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe('getLayoutInstance', () => {
});

it('gets instance from full uri', function () {
expect(fn('nymag.com/press/_layouts/base/instances/foobarbaz@published')).to.equal('foobarbaz');
expect(fn('domain.com/sitepath/_layouts/base/instances/foobarbaz@published')).to.equal('foobarbaz');
});

it('gets instance without meta', function () {
expect(fn('domain.com/_layouts/foobar/instances/baz/meta')).to.equal('baz');
});

it('CANNOT get instance from default uri', function () {
Expand Down
2 changes: 1 addition & 1 deletion lib/getPageInstance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const isUriStringCheck = require('../strCheck');
*/
module.exports = function (uri) {
isUriStringCheck.strCheck(uri);
const result = /\/_pages\/(.*)/.exec(uri);
const result = /\/_pages\/([^\.\/]+)/.exec(uri);

return result && result[1];
};
6 changes: 5 additions & 1 deletion lib/getPageInstance/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('getPageInstance', () => {
});

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

it('gets instance from uri with meta', function () {
expect(fn('domain.com/_pages/foobar/meta')).to.equal('foobar');
});

it('CANNOT get instance from a non-page uri', function () {
Expand Down
17 changes: 17 additions & 0 deletions lib/isDefaultLayout/README.md
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

```
15 changes: 15 additions & 0 deletions lib/isDefaultLayout/index.js
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\-]+$/);
};
29 changes: 29 additions & 0 deletions lib/isDefaultLayout/index.test.js
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);
});
});
17 changes: 17 additions & 0 deletions lib/isLayoutMeta/README.md
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

```
16 changes: 16 additions & 0 deletions lib/isLayoutMeta/index.js
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);
};
33 changes: 33 additions & 0 deletions lib/isLayoutMeta/index.test.js
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);
});
});
17 changes: 17 additions & 0 deletions lib/isPageMeta/README.md
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

```
15 changes: 15 additions & 0 deletions lib/isPageMeta/index.js
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);
};
29 changes: 29 additions & 0 deletions lib/isPageMeta/index.test.js
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);
});
});

0 comments on commit 55cfc0a

Please sign in to comment.