diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..68f4f9a --- /dev/null +++ b/.eslintrc @@ -0,0 +1,41 @@ +{ + "rules": { + "indent": [2, 2, {"SwitchCase": 1}], + "quotes": [2, "single", "avoid-escape"], + "linebreak-style": [2, "unix"], + "semi": [2, "never"], + "eqeqeq": [2, "allow-null"], + "no-extend-native": 2, + "curly": [2, "all"], + "strict": [2, "global"], + "space-before-function-paren": [2, "never"], + "space-unary-ops": [2, {"words": true, "nonwords": false}], + "space-infix-ops": [2, {"int32Hint": false}], + "keyword-spacing": 2, + "spaced-comment": [2, "always", { "exceptions": ["-", "+"] }], + "no-inline-comments": 2, + "func-style": [2, "expression"], + "callback-return": [2, ["done", "next"]], + "no-path-concat": 2, + "no-use-before-define": 2, + "array-bracket-spacing": [2, "never"], + "object-curly-spacing": [2, "never"], + "key-spacing": [2, {"beforeColon": false, "afterColon": true}], + "comma-spacing": [2, {"before": false, "after": true}], + "brace-style": [2, "stroustrup", {"allowSingleLine": true}], + "handle-callback-err": [2, "^(e|err|error)$" ], + "one-var": [2, "never"], + "one-var-declaration-per-line": [2, "always"], + "newline-after-var": 2, + "newline-before-return": 2, + "no-else-return": 2, + "no-unexpected-multiline": 2, + "eol-last": 2 + }, + "env": { + "node": true, + "browser": true, + "mocha": true + }, + "extends": "eslint:recommended" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a852cfc --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules +coverage +integration/public/bundle.js +example/*.svg diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f3a4453 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +sudo: false +language: node_js +node_js: +- '0.10' +- '4' +- node +matrix: + include: + - node_js: node + env: TEST_BROWSERS=true +script: npm run-script ci +deploy: + provider: npm + email: mike@cousins.io + api_key: + secure: vBZRpIaY4dy2nV6evcERLPMycsQ4u2ZfTQZhed8zWWbKXlLDYufYzdklzBhWbozj8jCyqnwm1uLriVXTafYS84zIliRssjMj6UC5irG9EUsTZFTBDDoPtywYpbSWQMj2YjBGiDu5b+iuTgcuD2VdyFxP7fMST0PjXscuD/jWQgRtcLaN+NmJh1oQyd1Mr7F7WcP8xqq1f68UmpKgdrAsdITWfbXGKsI8bDZ4aAHbrjnbhnxIZuXJmgr/QUg97MDl156B8ptpqi3ivykoFZs6Rlhpgxaf6p6knvuy8CwOoAirNe+c/uWl6W+OmtZyX43uHPlNvZrWG/F6/nNFNBYdiLJlw3wy/d8p18kw00ALeYfshOhdqjXoq+HdpeA2Do6cJpJtlnCuJHOd6nQyGbkMtygdfBfBnj4UgFA3zP9dqNcLEoF3Ny+1emo1PgrOa1fzkX9U0/SMUIZt3QKcH6io5duStMAt5kQuhjlylJ52/9tjqafY+Xghoo+f3hbFhs5D9T8+KXviqY4wHEJPVZ6k1yMa45SLjNX+xzBHj5KVwVkZGPrjtAa2Z4PYCMQ21T5f8ugo2CZYVrirgTV5MszQXJPvePEIxCxs2Rk3SUhaCiacKpzM8MzRsqAfc3YwIOp07iE0cLmTYQYTMrITrnkwJw5kTS6UXFMWm0xDdjrGUo0= + on: + node: node + condition: "$TEST_BROWSERS = true" + tags: true + all_branches: true + repo: tracespace/viewbox +env: + global: + - secure: ZDLrgNyY0+8JbQy/4J+8644/cSrKd1xpkIVCV7btXCF1A5/1qatHNr0HZlo7HLRu5CUAaySnhhrrtO1QNWEh9E4aKKul/CVV1mODrz3qC+dLCYBXFKdnUkKyTJmYMos33IlZEIsKcJn6CUS5zVS77jN4Ey3ywwDZHaa0RhGAOK8aYHrzBPTuuB8S6s0oNVrKNwROa/8Y2JV0vJTmdLc9xVZNMxQiqj1Zq0eEE/roaNbVC4rg6Z9akgoBi737V+zSQCxruS3Y5Xz3a9E0Het/poqI1i+3mZPQs2R+lkR4kUFwb4qJOlcwt4zP4GDU0kug6vz9FmRoHRIXiNtrZYepeSyV0AGQUTpdebUigpxORZQx1l2VMfYtOtz2rrpNHxyEsTomN6P5/NOI3vBYTzEKbk7T93TcMWRmu9l7FlEcx518Sbf4O3UmMRmUytpfzGWDmoa/gYKiAR4D2TWyaByDnEPJkh9g4Oepvad3d8mimB5JOE6OMcdKVoT4Ko+FQEwji9SslKYXImjnxa4hQCUSx8WHnhz/iDJcGQ6Sc+Ss3TLCwXbIAbk1csMz/KgD26rFDIYE323rDXpDphua66CIbtI6Z0doqNXqhHSWG/J3QL1vHWHDXLUsZrsjgbZpA0KWd3RkJUVCTLat8CmavjMRqk2Bs3JLjXmzkjxcvknQoRU= + - secure: Wv1fyPuFoNV1MOuqDKW8ig+aKYqR/g6dsBHVnPaA2bzf5BHIwapMPLe1DZSIkDedmvtTT/JY6j90TP49seBV10u3hD6k1Vdmkp3NyOZFu4l6n0OogBylfSou7be24KEC4acsVYr63PZ9YjlZse1WJVarLUbYAWwn3/YAvPl5ni2g2emcJeztV8t0xRZH4s1Rwt/hKQ0qX1PIvQhtH1J0TRi3ZOfO0BAcLo6EyAJWxCuVBLGsClnv4vm/aa2enwN2wK4hvzjuBNNRKrRd5HdBTEwe1pCS30UIo+9NRyX2vaCthDga+9PsOTGHxA3AKOKaAtWwBq9Gsy/A/Ci4uPVeqBY2yRN/4LXeCM8zgqH1rdFEAjV5CKOK+swsYuq5vB1/u1nhpyUgL2TIq0TTq6DscKGsoj5tKo9Cra6YyUuKRQdkNKTgaLWZzjJ6C5nSNwJiMV/hwnolpiJ0/dcONtY3ZwiHGCYRZLEgFtKvlc3FQWmR4rvWNVmr5XBfHUQp2oECQen2Klp3KZKnRKCTQ46ybSPazIniB92D04ijHp8miupDCKW3dUmZkq2ndAuZOg7Y64TbCqK8vajnufca2zS4da+Vtf+tXJwAKRrdZnc9eCUKB2uriaJglY6uBl1gHZQ2TTu1RM19C8VvL8mjY8NqHlVJ1xhuFHNnYA16Lz0qZEM= diff --git a/.zuul.yml b/.zuul.yml new file mode 100644 index 0000000..9231c47 --- /dev/null +++ b/.zuul.yml @@ -0,0 +1,12 @@ +ui: mocha-bdd +browsers: + - name: chrome + version: -1..latest + - name: safari + version: -1..latest + - name: firefox + version: -1..latest + - name: ie + version: -1..latest +browserify: + - plugin: proxyquire-universal diff --git a/API.md b/API.md new file mode 100644 index 0000000..35e2d2e --- /dev/null +++ b/API.md @@ -0,0 +1,74 @@ +# TOC + - [viewbox](#viewbox) + + + +# viewbox +should be able to create a new empty viewbox. + +```js +var result = viewbox.create() +expect(result).to.eql([]) +``` + +should be able to add two viewboxes. + +```js +var box1 = [0, -1, 4, 5] +var box2 = [-3, 1, 2, 6] +var result = viewbox.add(box1, box2) +expect(result).to.eql([-3, -1, 7, 8]) +``` + +should be able to add a viewbox to a new viewbox. + +```js +var box1 = viewbox.create() +var box2 = [-3, 1, 2, 6] +var result = viewbox.add(box1, box2) +expect(result).to.eql([-3, 1, 2, 6]) +``` + +should be able to add a new viewbox to a viewbox. + +```js +var box1 = [0, -1, 4, 5] +var box2 = viewbox.create() +var result = viewbox.add(box1, box2) +expect(result).to.eql([0, -1, 4, 5]) +``` + +should be able to scale a viewbox. + +```js +var box = [-1.5, 0.5, 1, 3] +var result = viewbox.scale(box, 2) +expect(result).to.eql([-3, 1, 2, 6]) +``` + +should be able to generate attributes for an SVG rectangle. + +```js +var box = [0, -1, 4, 5] +var result = viewbox.rect(box) +expect(result).to.eql({ + x: 0, + y: -1, + width: 4, + height: 5 +}) +``` + +should output a 0 size rectangle for a new viewbox. + +```js +var box = viewbox.create() +var result = viewbox.rect(box) +expect(result).to.eql({ + x: 0, + y: 0, + width: 0, + height: 0 +}) +``` + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..19df83f --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2016 by Mike Cousins, shared under the terms of the MIT License. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a5ed075 --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# viewbox + +[![npm](https://img.shields.io/npm/v/viewbox.svg?style=flat-square)](https://www.npmjs.com/package/viewbox) +[![Travis](https://img.shields.io/travis/tracespace/viewbox.svg?style=flat-square)](https://travis-ci.org/tracespace/viewbox) +[![Coveralls](https://img.shields.io/coveralls/tracespace/viewbox.svg?style=flat-square)](https://coveralls.io/github/tracespace/viewbox) +[![David](https://img.shields.io/david/tracespace/viewbox.svg?style=flat-square)](https://david-dm.org/tracespace/viewbox) +[![David](https://img.shields.io/david/dev/tracespace/viewbox.svg?style=flat-square)](https://david-dm.org/tracespace/viewbox#info=devDependencies) + +Utility library for dealing with SVG viewboxes as plain arrays. + +Install with: + +``` +$ npm install --save viewbox +``` + +## usage + +``` javascript +var viewbox = require('viewbox') +``` + +See [API.md](API.md) for usage. + +## developing and contributing + +Clone and then `$ npm install`. Please accompany all PRs with applicable tests. Please test your code in browsers, as Travis CI cannot run browser tests for PRs. + +### unit testing + +This module uses [Mocha](http://mochajs.org/) and [Chai](http://chaijs.com/) for unit testing, [Istanbul](https://github.com/gotwarlost/istanbul) for coverage, and [ESLint](http://eslint.org/) for linting. + +* `$ npm test` - run the tests, calculate coverage, and lint +* `$ npm run test:watch` - run the tests on code changes (does not lint nor cover) +* `$ npm run lint` - lint the code (will be run as a pre-commit script) + +### browser testing + +Browser tests are run with [Zuul](https://github.com/defunctzombie/zuul) and [Sauce Labs](https://saucelabs.com/opensauce/) on the latest two versions of Chrome, Firefox, Safari, and Internet Explorer, as well as the latest version of Edge. + +* `$ npm run test:browser` - run the unit tests in a local browser +* `$ npm run test:sauce` - run the units tests in several browsers using Open Sauce (Sauce Labs account and local [.zuulrc](https://github.com/defunctzombie/zuul/wiki/Zuulrc) required) diff --git a/index.js b/index.js new file mode 100644 index 0000000..dc4a1af --- /dev/null +++ b/index.js @@ -0,0 +1,52 @@ +// viewbox helper library +'use strict' + +module.exports = { + create: function createViewbox() { + return [] + }, + + add: function addViewboxes(box, addend) { + if (!box.length) { + return addend + } + + if (!addend.length) { + return box + } + + var xMin = Math.min(box[0], addend[0]) + var yMin = Math.min(box[1], addend[1]) + var xMax = Math.max((box[0] + box[2]), (addend[0] + addend[2])) + var yMax = Math.max((box[1] + box[3]), (addend[1] + addend[3])) + + return [xMin, yMin, (xMax - xMin), (yMax - yMin)] + }, + + scale: function scaleViewboxes(box, scale) { + return box.map(function(component) { + return component * scale + }) + }, + + rect: function viewboxRect(box) { + box = box && box.length + ? box + : [0, 0, 0, 0] + + return { + x: box[0], + y: box[1], + width: box[2], + height: box[3] + } + }, + + asString: function(box) { + box = box && box.length + ? box + : [0, 0, 0, 0] + + return box.join(' ') + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..156c3e8 --- /dev/null +++ b/package.json @@ -0,0 +1,48 @@ +{ + "name": "viewbox", + "version": "0.0.0", + "description": "Library for dealing with SVG viewboxes as plain arrays", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "lint": "eslint *.js", + "test": "istanbul cover _mocha", + "posttest": "npm run lint", + "doc": "mocha --reporter markdown > API.md", + "preversion": "npm run doc && git add API.md", + "test:watch": "mocha --reporter dot --watch", + "test:browser": "zuul --local -- test.js", + "test:sauce": "zuul -- test.js", + "ci": "npm test && if [ \"${TEST_BROWSERS}\" = \"true\" ]; then npm run ci:browser; fi", + "ci:browser": "if [ \"${TRAVIS_PULL_REQUEST}\" = \"false\" ]; then npm run test:sauce; fi", + "postci": "coveralls < ./coverage/lcov.info" + }, + "repository": { + "type": "git", + "url": "git://github.com/tracespace/viewbox.git" + }, + "keywords": [ + "svg", + "viewbox", + "geometry" + ], + "contributors": [ + "Mike Cousins (http://cousins.io/)" + ], + "license": "MIT", + "bugs": { + "url": "https://github.com/tracespace/viewbox/issues" + }, + "homepage": "https://github.com/tracespace/viewbox", + "devDependencies": { + "chai": "^3.5.0", + "coveralls": "^2.11.9", + "eslint": "^2.13.1", + "istanbul": "^0.4.4", + "mocha": "^2.5.3", + "zuul": "^3.10.1" + }, + "dependencies": {} +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..707072e --- /dev/null +++ b/test.js @@ -0,0 +1,100 @@ +// tests for viewbox helper library +'use strict' + +var expect = require('chai').expect + +var viewbox = require('.') + +describe('viewbox', function() { + it('should be able to create a new viewbox', function() { + var result = viewbox.create() + + expect(result).to.eql([]) + }) + + it('should be able to add two viewboxes', function() { + var box1 = [0, -1, 4, 5] + var box2 = [-3, 1, 2, 6] + var result = viewbox.add(box1, box2) + + expect(result).to.eql([-3, -1, 7, 8]) + }) + + it('should be able to add a viewbox to a new viewbox', function() { + var box1 = viewbox.create() + var box2 = [-3, 1, 2, 6] + var result = viewbox.add(box1, box2) + + expect(result).to.eql([-3, 1, 2, 6]) + }) + + it('should be able to add a new viewbox to a viewbox', function() { + var box1 = [0, -1, 4, 5] + var box2 = viewbox.create() + var result = viewbox.add(box1, box2) + + expect(result).to.eql([0, -1, 4, 5]) + }) + + it('should be able to scale a viewbox', function() { + var box = [-1.5, 0.5, 1, 3] + var result = viewbox.scale(box, 2) + + expect(result).to.eql([-3, 1, 2, 6]) + }) + + it('should be able to generate attributes for an SVG rectangle', function() { + var box = [0, -1, 4, 5] + var result = viewbox.rect(box) + + expect(result).to.eql({ + x: 0, + y: -1, + width: 4, + height: 5 + }) + }) + + it('should output a 0 size rectangle for a new viewbox', function() { + var box = viewbox.create() + var result = viewbox.rect(box) + + expect(result).to.eql({ + x: 0, + y: 0, + width: 0, + height: 0 + }) + }) + + it('should output a 0 size rectangle for something falsey', function() { + var result = viewbox.rect() + + expect(result).to.eql({ + x: 0, + y: 0, + width: 0, + height: 0 + }) + }) + + it('should be able to generate a viewbox string', function() { + var box = [1, 2, 3, 4] + var result = viewbox.asString(box) + + expect(result).to.equal('1 2 3 4') + }) + + it('should output a new viewbox as a 0 size viewbox string', function() { + var box = viewbox.create() + var result = viewbox.asString(box) + + expect(result).to.equal('0 0 0 0') + }) + + it('should output something falsey as a 0 size viewbox string', function() { + var result = viewbox.asString() + + expect(result).to.equal('0 0 0 0') + }) +})