https://github.com/ppvg/svg-numbers
Parse SVG coordinates in JavaScript.
https://github.com/ppvg/svg-numbers
Last synced: about 1 month ago
JSON representation
Parse SVG coordinates in JavaScript.
- Host: GitHub
- URL: https://github.com/ppvg/svg-numbers
- Owner: ppvg
- License: mit
- Created: 2013-11-25T14:04:42.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-11-30T11:07:16.000Z (over 11 years ago)
- Last Synced: 2025-03-25T13:01:56.361Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 156 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# svg-numbers
Parser and serializer for lists of coordinates in SVG documents, such as [the `points` attribute of a `` element](http://www.w3.org/TR/SVG/shapes.html#PointsBNF).
[](http://badge.fury.io/js/svg-numbers) [](https://travis-ci.org/PPvG/svg-numbers)
## Installation
With [npm](https://npmjs.org/): `$ npm install svg-numbers`
## Usage
### Parsing
var parse = require('svg-numbers').parse
var numbers = parse('10, 15.20.8 -11,15-25+75 4.2e3')
console.log(numbers)
// [ 10, 15.2, .8, -11, 15, -25, 75, 4200 ]### Serializing
var serialize = require('svg-numbers').serialize
var numbers = [10, 4.2, .333, -8]
var str = serialize(numbers)
console.log(str)
// '10,4.2.333-8'Separators (commas and/or spaces) are left out wherever the SVG recommendation allows it. If you'd rather have separators everywhere, just use `Array.prototype.join`:
var str = numbers.join(', ')
console.log(str)
// '10, 4.2, .333, -8'### Catching syntax errors
If a syntax error is found, an error is thrown. The valid coordinates up to and until the syntax error are available as `error.partial`:
try {
var numbers = parse('10, 20, , 30, 40')
} catch (error) {
console.log(error.partial)
// [ 10, 20 ]
}(The W3C SVG recommendation has something to say about [error processing](http://www.w3.org/TR/SVG/implnote.html#ErrorProcessing).)
## Running the tests
$ git clone https://github.com/PPvG/svg-numbers
$ cd svg-numbers
$ npm install
$ npm test
## Browser support
Use [browserify](http://browserify.org/). All major browsers are supported:
[](https://ci.testling.com/PPvG/svg-numbers)
## License
[MIT](https://raw.github.com/PPvG/svg-numbers/master/LICENSE)