Comparing version 1.0.0 to 1.1.0
@@ -5,2 +5,6 @@ # Change Log | ||
## [1.1.0] - 2016-08-21 | ||
## Added | ||
- Options `bounds`, `strict`, `types` | ||
## [1.0.0] - 2016-08-19 | ||
@@ -16,2 +20,3 @@ ## Changed | ||
[1.1.0]: https://github.com/digidem/osm2json/compare/v1.0.0...v1.1.0 | ||
[1.0.0]: https://github.com/digidem/osm2json/compare/v0.0.1...v1.0.0 |
@@ -11,4 +11,2 @@ /** | ||
var STRICT = true | ||
// These attributes are "id-like" and will be coerced to Number if opts.coerceIds === true | ||
@@ -51,7 +49,14 @@ var ID_ATTRIBUTES = ['id', 'uid', 'version', 'changeset', 'ref'] | ||
function is(list, value) { | ||
var DEFAULTS = { | ||
coerceIds: true, | ||
bounds: true, | ||
strict: false, | ||
types: VALID_NODES.slice(0, -1) | ||
} | ||
function is (list, value) { | ||
return list.indexOf(value) > -1 | ||
} | ||
function isValidChild(name, childname) { | ||
function isValidChild (name, childname) { | ||
return WHITELISTS[name] && | ||
@@ -62,7 +67,7 @@ WHITELISTS[name].children && | ||
function isValidAttribute(name, attr) { | ||
function isValidAttribute (name, attr) { | ||
return WHITELISTS[name] && WHITELISTS[name].attributes.indexOf(attr) > -1 | ||
} | ||
function parseNumber(str) { | ||
function parseNumber (str) { | ||
if (!isNaN(str) && str.length) { | ||
@@ -74,3 +79,3 @@ return str % 1 === 0 ? parseInt(str, 10) : parseFloat(str) | ||
function parseBoolean(str) { | ||
function parseBoolean (str) { | ||
if (/^(?:true|false)$/i.test(str)) { | ||
@@ -84,11 +89,13 @@ return str.toLowerCase() === 'true' | ||
function Osm2Json(opts) { | ||
function Osm2Json (opts) { | ||
if (!(this instanceof Osm2Json)) return new Osm2Json(opts) | ||
this.opts = opts || {} | ||
this.opts.coerceIds = this.opts.coerceIds !== false | ||
this.parser = sax.parser(STRICT) | ||
this.opts = Object.assign({}, DEFAULTS, opts) | ||
this.parser = sax.parser(this.opts.strict, { | ||
lowercase: !this.opts.strict | ||
}) | ||
if (this.opts.bounds) this.opts.types.push('bounds') | ||
this.parser.onerror = this.onError.bind(this) | ||
this.parser.onopentag = this.onOpenTag.bind(this) | ||
this.parser.onclosetag = this.onCloseTag.bind(this) | ||
Transform.call(this, { readableObjectMode : true }) | ||
Transform.call(this, { readableObjectMode: true }) | ||
} | ||
@@ -98,3 +105,3 @@ | ||
Osm2Json.prototype._transform = function(chunk, enc, done) { | ||
Osm2Json.prototype._transform = function (chunk, enc, done) { | ||
if (this.error) return done(this.error) | ||
@@ -105,3 +112,3 @@ this.parser.write(chunk.toString()) | ||
Osm2Json.prototype.onError = function(err) { | ||
Osm2Json.prototype.onError = function (err) { | ||
err.message = 'Invalid XML at line #' + this.parser.line + | ||
@@ -113,13 +120,14 @@ ', column #' + this.parser.column + ':\n' + | ||
Osm2Json.prototype.onOpenTag = function(node) { | ||
Osm2Json.prototype.onOpenTag = function (node) { | ||
if (this.error) return | ||
if (!this.root && is(VALID_ROOTS, node.name)) { | ||
this.root = node.name | ||
} else if (this.root === 'osmChange' && !this.currentAction && is(VALID_ACTIONS, node.name)) { | ||
} else if ((!this.opts.strict || this.root === 'osmChange') && | ||
!this.currentAction && is(VALID_ACTIONS, node.name)) { | ||
this.currentAction = node.name | ||
} else if (!this.currentNode && is(VALID_NODES, node.name)) { | ||
} else if (!this.currentNode && is(this.opts.types, node.name)) { | ||
this.processNode(node) | ||
} else if (this.currentNode && isValidChild(this.currentNode.type, node.name)) { | ||
this.processChild(node) | ||
} else { | ||
} else if (this.opts.strict && !is(VALID_NODES, node.name)) { | ||
this.onError(new Error('invalid tag <' + node.name + '>')) | ||
@@ -129,6 +137,6 @@ } | ||
Osm2Json.prototype.onCloseTag = function(name) { | ||
if (this.root === 'osmChange' && is(VALID_ACTIONS, name)) { | ||
Osm2Json.prototype.onCloseTag = function (name) { | ||
if ((!this.opts.strict || this.root === 'osmChange') && is(VALID_ACTIONS, name)) { | ||
this.currentAction = null | ||
} else if (is(VALID_NODES, name)) { | ||
} else if (is(this.opts.types, name)) { | ||
this.push(this.currentNode) | ||
@@ -139,3 +147,3 @@ this.currentNode = null | ||
Osm2Json.prototype.processNode = function(node) { | ||
Osm2Json.prototype.processNode = function (node) { | ||
this.currentNode = {} | ||
@@ -152,3 +160,3 @@ this.currentNode.type = node.name | ||
Osm2Json.prototype.processChild = function(node) { | ||
Osm2Json.prototype.processChild = function (node) { | ||
var currentNode = this.currentNode | ||
@@ -186,3 +194,3 @@ var attr = node.attributes | ||
Osm2Json.prototype.coerce = function(attrName, value) { | ||
Osm2Json.prototype.coerce = function (attrName, value) { | ||
var shouldCoerceToNumber = is(NUMBER_ATTRIBUTES, attrName) | ||
@@ -189,0 +197,0 @@ if (this.opts.coerceIds) { |
{ | ||
"name": "osm2json", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Converts an OSM XML file to OSM JSON objects as a transform stream", | ||
"main": "lib/osm2json.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "standard && node test/*.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
# osm2json | ||
[![npm](https://img.shields.io/npm/v/osm2json.svg?maxAge=2592000)](https://www.npmjs.com/package/osm2json) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
@@ -94,9 +95,17 @@ > Streaming parser from OSM XML to OSM JSON objects | ||
* `opts.coerceIds` - coerce ids to `Number` (defaults to *true*) | ||
* `opts.coerceIds` - coerce id-type fields (`id, uid, version, changeset, ref`) to `Number` (default `true`) - useful for [osm-p2p-db](https://github.com/digidem/osm-p2p-db) where ids can be strings. | ||
* `opts.bounds` - Also parse bounds (default `true`) | ||
* `opts.types` - An array of element types you are interested in, e.g. `opts.types = ['node']` (default `['node', 'way', 'relation', 'changeset']`) | ||
* `opts.strict` - Be a jerk about XML (default `false`). In strict mode will throw an error if: | ||
- XML is badly formatted | ||
- Case of element names differs from spec | ||
- Root node is not one of `osm`, `osmChange` | ||
- An action element (`create, modify, delete`) appears when the root is not `osmChange` | ||
- Any element in the XML which is not one of `create, modify, delete, node, way, relation, changeset, bounds, nd, tag, member` | ||
The readable side of the stream is in `objectMode`. | ||
Any attribute that is not a valid OSM XML attribute will be ignored (see [`WHITELISTS`](https://github.com/digidem/osm2json/blob/master/lib/osm2json.js#L27-L48)). `tag`, `member`, of `nd` elements without the required attributes will throw an error. The readable side of the stream is in `objectMode`. | ||
## Contribute | ||
PRs welcome. Right now this could do with some tests. If you are feeling ambitious, this could be sped up by using [node-expat](https://github.com/astro/node-expat) on node. The interface is similar to sax-js and it should be possible to wrap this to use sax-js on the browser and node-expat on the server using the [browserify `browser` field](https://github.com/substack/browserify-handbook#browser-field) | ||
PRs welcome. Please follow [JS Standard Style](http://standardjs.com/). Right now this could do with some tests. If you are feeling ambitious, this could be sped up by using [node-expat](https://github.com/astro/node-expat) on node. The interface is similar to sax-js and it should be possible to wrap this to use sax-js on the browser and node-expat on the server using the [browserify `browser` field](https://github.com/substack/browserify-handbook#browser-field) | ||
@@ -103,0 +112,0 @@ ## License |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
21357
11
497
1
114
2