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 1 commit
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
Prev Previous commit
Next Next commit
Improved validation as per suggestions from @cjihrig
  • Loading branch information
Antony Jones committed Dec 15, 2014
commit 56e2323de3232d99b963ff175e3be9105a38bed4
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Lead Maintainer - [Eran Hammer](https://github.com/hueniverse)
- [`null()`](#null)
- [`undefined()`](#undefined)
- [`include(values)`](#includevalues)
- [`startWith(value)`](#startWith)
- [`endWith(value)`](#endWith)
- [`startWith(value)`](#startwithvalue)
- [`endWith(value)`](#startwithvalue)
- [`exist()`](#exist)
- [`empty()`](#empty)
- [`length(size)`](#lengthsize)
Expand Down
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ internals.addMethod(['include', 'includes', 'contain', 'contains'], function (va

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(-Math.abs(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.

Is Math.abs necessary in this context? It does add some overhead and I don't think we really need it.

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));
});
Expand Down
56 changes: 56 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,34 @@ describe('expect()', function () {
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 () {
Expand All @@ -752,6 +780,34 @@ describe('expect()', function () {
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 () {
Expand Down