Comparing version 1.2.1 to 1.3.0
@@ -13,5 +13,5 @@ # How to contribute | ||
* Fork the repository on GitHub. | ||
* Fix the issue ensuring that your code follows the [style guide](https://github.com/hapijs/hapi/blob/master/docs/Style.md). | ||
* Fix the issue ensuring that your code follows the [style guide](https://github.com/hapijs/contrib/blob/master/Style.md). | ||
* Add tests for your new code ensuring that you have 100% code coverage (we can help you reach 100% but will not merge without it). | ||
* Run `npm test` to generate a report of test coverage | ||
* [Pull requests](http://help.github.com/send-pull-requests/) should be made to the [master branch](https://github.com/hapijs/code/tree/master). |
@@ -60,2 +60,3 @@ // Load modules | ||
if (this._flags.not ? !result : result) { | ||
this._flags = {}; | ||
return this; | ||
@@ -123,3 +124,19 @@ } | ||
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); | ||
return this.assert(comparator === value, 'startWith ' + internals.display(value)); | ||
}); | ||
internals.addMethod(['exist', 'exists'], function () { | ||
@@ -126,0 +143,0 @@ |
{ | ||
"name": "code", | ||
"description": "assertion library", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"repository": "git://github.com/hapijs/code", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -5,5 +5,6 @@ #code | ||
[![Current Version](https://img.shields.io/npm/v/code.svg)](https://www.npmjs.org/package/code) | ||
[![Build Status](https://secure.travis-ci.org/hapijs/code.png)](http://travis-ci.org/hapijs/code) | ||
Lead Maintainer - [Eran Hammer](https://github.com/hueniverse) | ||
Lead Maintainer - [Colin Ihrig](https://github.com/cjihrig) | ||
@@ -35,2 +36,4 @@ ## Table of Contents | ||
- [`include(values)`](#includevalues) | ||
- [`startWith(value)`](#startwithvalue) | ||
- [`endWith(value)`](#startwithvalue) | ||
- [`exist()`](#exist) | ||
@@ -357,2 +360,34 @@ - [`empty()`](#empty) | ||
#### `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()` | ||
@@ -359,0 +394,0 @@ |
@@ -179,2 +179,23 @@ // Load modules | ||
it('resets flags between chained assertions', function (done) { | ||
var exception = false; | ||
try { | ||
Code.expect('abc').to.contain('a').and.to.not.contain('d'); | ||
Code.expect('abc').to.not.contain('d').and.to.contain('a'); | ||
Code.expect('abc').to.not.contain('d').and.to.not.contain('e'); | ||
Code.expect('abc').to.contain('a').and.to.not.contain('d').and.to.contain('c').to.not.contain('f'); | ||
Code.expect(function () {}).to.not.throw().and.to.be.a.function(); | ||
Code.expect(10).to.not.be.about(8, 1).and.to.be.about(9, 1); | ||
Code.expect(10).to.be.about(9, 1).and.to.not.be.about(8, 1); | ||
} | ||
catch (err) { | ||
exception = err; | ||
} | ||
Hoek.assert(!exception, exception); | ||
done(); | ||
}); | ||
describe('assertion', function () { | ||
@@ -715,2 +736,101 @@ | ||
describe('endWith()', function () { | ||
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 () { | ||
@@ -717,0 +837,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81774
1636
642
1