Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout Functions #9

Merged
merged 3 commits into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ npm install --save clayutils
* **getComponentInstance** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getComponentInstance)
* **getComponentName** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getComponentName)
* **getComponentVersion** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getComponentVersion)
* **getLayoutInstance** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getLayoutInstance)
* **getLayoutName** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getLayoutName)
* **getListInstance** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getListInstance)
* **getPageInstance** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getPageInstance)
* **getPageVersion** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/getPageVersion)
* **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)
* **isLayout** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isLayout)
* **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)
* **isPublished** [(code|tests|docs)](https://github.com/clay/clayutils/tree/master/lib/isPublished)
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';
module.exports.getComponentInstance = require('./lib/getComponentInstance');
module.exports.getLayoutInstance = require('./lib/getLayoutInstance');
module.exports.getComponentName = require('./lib/getComponentName');
module.exports.getLayoutName = require('./lib/getLayoutName');
module.exports.getComponentVersion = require('./lib/getComponentVersion');
module.exports.getPageInstance = require('./lib/getPageInstance');
module.exports.getPageVersion = require('./lib/getPageVersion');
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.isDefaultComponent = require('./lib/isDefaultComponent');
module.exports.isPage = require('./lib/isPage');
module.exports.isPublished = require('./lib/isPublished');
Expand Down
17 changes: 17 additions & 0 deletions lib/getLayoutInstance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### getLayoutInstance

Get layout instance from uri

#### Params

* `uri` _string_

**Returns** _string|null_

#### Example

```js
getLayoutInstance('nymag.com/press/_layouts/base/instances/foobarbaz@published')
//=> 'foobarbaz'

```
17 changes: 17 additions & 0 deletions lib/getLayoutInstance/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 layout instance
* from uri without the layout version. Otherwise, throw an error.
* @example /_layouts/text/instances/0@published returns 0
* @param {string} uri
* @return {string|null}
*/
module.exports = function (uri) {
isUriStringCheck.strCheck(uri);
const result = /\/_layouts\/.+?\/instances\/([^\.@]+)/.exec(uri);

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

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

describe('getLayoutInstance', () => {
it('gets instance from uri', function () {
expect(fn('/_layouts/base/instances/0')).to.equal('0');
});

it('gets instance from uri with extension', function () {
expect(fn('/_layouts/base/instances/0.html')).to.equal('0');
});

it('gets instance from uri with version', function () {
expect(fn('/_layouts/base/instances/0@published')).to.equal('0');
});

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

it('CANNOT get instance from default uri', function () {
expect(fn('/_layouts/base')).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);
});
});
17 changes: 17 additions & 0 deletions lib/getLayoutName/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### getLayoutName

Get layout name from uri

#### Params

* `uri` _string_

**Returns** _string|null_

#### Example

```js
getLayoutName('nymag.com/press/_layouts/base/instances/foobarbaz@published')
//=> 'base'

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

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

/**
* First test if argument passed in is a String. If true, get layout name
* from uri. Otherwise throw an error.
* @example /_layouts/base returns base
* @example /_layouts/text/instances/0 returns text
* @example /_layouts/image.html returns image
* @param {string} uri
* @return {string|null}
*/
module.exports = function (uri) {
isUriStringCheck.strCheck(uri);
const result = /_layouts\/(.+?)[\/\.]/.exec(uri) || /_layouts\/(.*)/.exec(uri);

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

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

describe('getLayoutName', () => {
it('gets name from default uri', function () {
expect(fn('/_layouts/base')).to.equal('base');
});

it('gets name from instance uri', function () {
expect(fn('/_layouts/base/instances/0')).to.equal('base');
});

it('gets name from versioned uri', function () {
expect(fn('/_layouts/base/instances/0@published')).to.equal('base');
});

it('gets name from uri with extension', function () {
expect(fn('/_layouts/base.html')).to.equal('base');
expect(fn('/_layouts/base.json')).to.equal('base');
});

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

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);
});
});
17 changes: 17 additions & 0 deletions lib/isLayout/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### isLayout

Check if '/_layouts/' is in the uri

#### Params

* `uri` _string_

**Returns** _boolean_

#### Example

```js
isLayout('nymag.com/press/_layouts/base/instances/foobarbaz@published')
//=> true

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

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

/**
* First test if argument is a String. If true, test if '/_layouts/' is in the string.
* Otherwise, throw an error.
* @param {string} uri
* @return {Boolean}
*/
module.exports = function (uri) {
isUriStringCheck.strCheck(uri);
return uri.toLowerCase().indexOf('/_layouts/') > -1;
};
28 changes: 28 additions & 0 deletions lib/isLayout/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

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

describe('isLayout', () => {
it('returns true if layout reference', () => {
expect(fn('domain.com/_layouts/foo')).to.equal(true);
});

it('returns true if layout instance reference', () => {
expect(fn('domain.com/_layouts/foo/instances/bar')).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/_pages/foo')).to.equal(false);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"chai": "^3.5.0",
"chalk": "^1.1.3",
"coveralls": "^2.13.0",
"coveralls": "^3.0.2",
"eslint": "^3.19.0",
"handlebars": "^4.0.6",
"istanbul": "^0.4.5",
Expand Down