Skip to content

Commit

Permalink
Use assert and unassertify for enforcing internal invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 24, 2015
1 parent c222bd7 commit 1317cb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Hi, and thanks in advance for contributing to Mapbox GL. Here's how we work. Please follow these conventions when submitting an issue or pull request.

## Issues

When reporting a bug, if possible please include **executable code** that demonstrates the issue you have encountered. A good way to do so is to attach a link to an example on [JSFiddle](https://jsfiddle.net/).

## Code conventions

* Our code conventions are mostly enforced with eslint, which will be run as part of `npm test`.
* In internal / private methods, we check preconditions with `assert`, helping us catch mistakes within the library. For performance, these checks are removed from the production build with [unassertify](https://www.npmjs.com/package/unassertify).
* In external / public methods, we check preconditions where appropriate and emit an error. "Emit" can mean throwing an `Error`, passing an `Error` as a first callback argument, or emitting an `error` event, as appropriate for the context. These checks remain present in production builds, helping downstream authors avoid common mistakes.

## Documentation conventions

See [docs/README.md](https://github.com/mapbox/mapbox-gl-js/blob/master/docs/README.md).
13 changes: 9 additions & 4 deletions js/source/tile_coord.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';

var assert = require('assert');

module.exports = TileCoord;

function TileCoord(z, x, y, w) {
if (isNaN(w)) w = 0;
assert(!isNaN(z));
assert(!isNaN(x));
assert(!isNaN(y));
assert(z >= 0);
assert(x >= 0);
assert(y >= 0);

if (isNaN(z) || isNaN(x) || isNaN(y) || z < 0 || x < 0 || y < 0) {
throw new Error('Invalid TileCoord object: (' + z + ', ' + x + ', ' + y + ', ' + w + ')');
}
if (isNaN(w)) w = 0;

this.z = +z;
this.x = +x;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"rbush": "^1.4.0",
"request": "^2.39.0",
"resolve-url": "^0.2.1",
"unassertify": "^1.0.2",
"unitbezier": "^0.0.0",
"vector-tile": "^1.1.3",
"webworkify": "^1.0.2"
Expand Down Expand Up @@ -68,7 +69,7 @@
"test": "npm run lint && prova test/js/*/*.js",
"test-suite": "node test/render.test.js || true",
"build": "browserify -d js/mapbox-gl.js --standalone mapboxgl > dist/mapbox-gl-dev.js && npm run docs",
"production": "browserify js/mapbox-gl.js -d -p [minifyify --map mapbox-gl.js.map --output dist/mapbox-gl.js.map] --standalone mapboxgl > dist/mapbox-gl.js",
"production": "browserify js/mapbox-gl.js -d -t unassertify -p [minifyify --map mapbox-gl.js.map --output dist/mapbox-gl.js.map] --standalone mapboxgl > dist/mapbox-gl.js",
"prepublish": "npm run build && npm run production",
"cov": "istanbul cover prova test/js/*/*.js test/render.test.js -x js/lib/debugtext.js",
"docs": "node docs/_generate/generate.js"
Expand Down

0 comments on commit 1317cb7

Please sign in to comment.