diff --git a/README.md b/README.md index f1a76af..359d3ba 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/index.js b/index.js index eb368ce..da78d3b 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/lib/getLayoutInstance/index.js b/lib/getLayoutInstance/index.js index 00f2048..76a5b25 100644 --- a/lib/getLayoutInstance/index.js +++ b/lib/getLayoutInstance/index.js @@ -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]; }; diff --git a/lib/getLayoutInstance/index.test.js b/lib/getLayoutInstance/index.test.js index 26d0510..1d2b066 100644 --- a/lib/getLayoutInstance/index.test.js +++ b/lib/getLayoutInstance/index.test.js @@ -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 () { diff --git a/lib/getPageInstance/index.js b/lib/getPageInstance/index.js index f4fa289..cf3f86f 100644 --- a/lib/getPageInstance/index.js +++ b/lib/getPageInstance/index.js @@ -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]; }; diff --git a/lib/getPageInstance/index.test.js b/lib/getPageInstance/index.test.js index 5091ae3..94e020f 100644 --- a/lib/getPageInstance/index.test.js +++ b/lib/getPageInstance/index.test.js @@ -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 () { diff --git a/lib/isDefaultLayout/README.md b/lib/isDefaultLayout/README.md new file mode 100644 index 0000000..65ad447 --- /dev/null +++ b/lib/isDefaultLayout/README.md @@ -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 + +``` diff --git a/lib/isDefaultLayout/index.js b/lib/isDefaultLayout/index.js new file mode 100644 index 0000000..9aaee30 --- /dev/null +++ b/lib/isDefaultLayout/index.js @@ -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\-]+$/); +}; diff --git a/lib/isDefaultLayout/index.test.js b/lib/isDefaultLayout/index.test.js new file mode 100644 index 0000000..db73d07 --- /dev/null +++ b/lib/isDefaultLayout/index.test.js @@ -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); + }); +}); diff --git a/lib/isLayoutMeta/README.md b/lib/isLayoutMeta/README.md new file mode 100644 index 0000000..c9690a5 --- /dev/null +++ b/lib/isLayoutMeta/README.md @@ -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 + +``` diff --git a/lib/isLayoutMeta/index.js b/lib/isLayoutMeta/index.js new file mode 100644 index 0000000..2e069ac --- /dev/null +++ b/lib/isLayoutMeta/index.js @@ -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); +}; diff --git a/lib/isLayoutMeta/index.test.js b/lib/isLayoutMeta/index.test.js new file mode 100644 index 0000000..ce4e0ae --- /dev/null +++ b/lib/isLayoutMeta/index.test.js @@ -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); + }); +}); diff --git a/lib/isPageMeta/README.md b/lib/isPageMeta/README.md new file mode 100644 index 0000000..305110c --- /dev/null +++ b/lib/isPageMeta/README.md @@ -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 + +``` diff --git a/lib/isPageMeta/index.js b/lib/isPageMeta/index.js new file mode 100644 index 0000000..0614858 --- /dev/null +++ b/lib/isPageMeta/index.js @@ -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); +}; diff --git a/lib/isPageMeta/index.test.js b/lib/isPageMeta/index.test.js new file mode 100644 index 0000000..d1154c8 --- /dev/null +++ b/lib/isPageMeta/index.test.js @@ -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); + }); +});