Skip to content

Add startsWith and endsWith for URLs. #6

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

Merged
merged 4 commits into from
Dec 22, 2014
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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Lead Maintainer - [Eran Hammer](https://github.com/hueniverse)
- [`null()`](#null)
- [`undefined()`](#undefined)
- [`include(values)`](#includevalues)
- [`startWith(value)`](#startwithvalue)
- [`endWith(value)`](#startwithvalue)
- [`exist()`](#exist)
- [`empty()`](#empty)
- [`length(size)`](#lengthsize)
Expand Down Expand Up @@ -352,6 +354,38 @@ expect({ a: 1, b: 2, c: 3 }).to.only.include({ a: 1, b: 2, c: 3 });
expect({ a: [1], b: [2], c: [3] }).to.deep.include({ a: [1], c: [3] });
```

#### `startWith(value)`

Aliases: `startsWith()`,

Asserts that the reference value (a string) starts with the provided value where:
- `value` - a string.

Note that this assertion is case sensitive.

```js
var Code = require('code');
var expect = Code.expect;

expect('https://example.org/secure').to.startWith('https://');
```

#### `endWith(value)`

Aliases: `endsWith()`,

Asserts that the reference value (a string) ends with the provided value where:
- `value` - a string.

Note that this assertion is case sensitive.

```js
var Code = require('code');
var expect = Code.expect;

expect('http://example.org/relative').to.endWith('/relative');
```

#### `exist()`

Aliases: `exists`
Expand Down
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ internals.addMethod(['include', 'includes', 'contain', 'contains'], function (va
return this.assert(Hoek.contain(this._ref, value, this._flags), 'include ' + internals.display(value));
});

internals.addMethod(['endWith', 'endsWith'], function (value) {

internals.assert(this, typeof this._ref === 'string' && typeof value === 'string', 'Can only assert endsWith on a string, with a string');

var comparator = this._ref.slice(-value.length);
return this.assert(comparator === value, 'endWith ' + internals.display(value));
});

internals.addMethod(['startWith', 'startsWith'], function (value) {

internals.assert(this, typeof this._ref === 'string' && typeof value === 'string', 'Can only assert startsWith on a string, with a string');

var comparator = this._ref.slice(0, value.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you assert that the data type is a string or possibly perform a coercion before calling slice()

return this.assert(comparator === value, 'startWith ' + internals.display(value));
});


internals.addMethod(['exist', 'exists'], function () {

Expand Down
99 changes: 99 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,105 @@ describe('expect()', function () {
});
});

describe('endWith()', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably add some tests for other data types.


it('validates strings', function (done) {

var exception = false;
try {
Code.expect('http://xyz.abc/def').to.endWith('abc/def');
Code.expect('abcdefgh').not.to.endWith('abc');
Code.expect('foobar').not.to.endWith('not-long-enough');
}
catch (err) {
exception = err;
}

Hoek.assert(!exception, exception);
done();
});

it('does not validate arrays', function (done) {

var exception = false;
try {
Code.expect(['a', 'b', 'c']).to.endWith('abcdef');
}
catch (err) {
exception = err;
}

Hoek.assert(exception.message === 'Can only assert endsWith on a string, with a string', exception);
done();
});

it('does not validate using arrays', function (done) {

var exception = false;
try {
Code.expect('abcdef').to.endWith(['a', 'b', 'c']);
}
catch (err) {
exception = err;
}

Hoek.assert(exception.message === 'Can only assert endsWith on a string, with a string', exception);
done();
});

});

describe('startWith()', function () {

it('validates strings', function (done) {

var exception = false;
try {
Code.expect('http://xyz.abc/def').to.startWith('http://');
Code.expect('eeeaaaeee').to.startWith('eee');
Code.expect('eeeaaaeee').not.to.startWith('aaa');
Code.expect('http://xyz.abc/def').not.to.startWith('https://');
Code.expect('abcdefgh').not.to.startWith('fgh');
Code.expect('foobar').not.to.startWith('not-long-enough');
}
catch (err) {
exception = err;
}

Hoek.assert(!exception, exception);
done();
});

it('does not validate arrays', function (done) {

var exception = false;
try {
Code.expect(['a', 'b', 'c']).to.startWith('abcdef');
}
catch (err) {
exception = err;
}

Hoek.assert(exception.message === 'Can only assert startsWith on a string, with a string', exception);
done();
});

it('does not validate using arrays', function (done) {

var exception = false;
try {
Code.expect('abcdef').to.startWith(['a', 'b', 'c']);
}
catch (err) {
exception = err;
}

Hoek.assert(exception.message === 'Can only assert startsWith on a string, with a string', exception);
done();
});

});

describe('exist()', function () {

it('validates assertion', function (done) {
Expand Down