diff --git a/geojson.js b/geojson.js index 8550a87..6bf8391 100644 --- a/geojson.js +++ b/geojson.js @@ -180,6 +180,9 @@ for(var gtype in params.geom) { var val = params.geom[gtype]; + var coordinates = []; + var itemClone; + var paths; // Geometry parameter specified as: {Point: 'coords'} if(typeof val === 'string' && item.hasOwnProperty(val)) { @@ -225,12 +228,11 @@ // Geometry parameter specified as: {Point: ['container.lat', 'container.lng', 'container.alt']} else if(Array.isArray(val) && isNested(val[0]) && isNested(val[1]) && isNested(val[2])){ - var coordinates = []; for (var i = 0; i < val.length; i++) { // i.e. 0 and 1 - var paths = val[i].split('.'); - var itemClone = item; + paths = val[i].split('.'); + itemClone = item; for (var j = 0; j < paths.length; j++) { - if (!itemClone.hasOwnProperty(paths[j])) { + if (itemClone == undefined || !itemClone.hasOwnProperty(paths[j])) { return false; } itemClone = itemClone[paths[j]]; // Iterate deeper into the object @@ -243,17 +245,16 @@ // Geometry parameter specified as: {Point: ['container.lat', 'container.lng']} else if(Array.isArray(val) && isNested(val[0]) && isNested(val[1])){ - var coordinates = []; - for (var i = 0; i < val.length; i++) { // i.e. 0 and 1 - var paths = val[i].split('.'); - var itemClone = item; - for (var j = 0; j < paths.length; j++) { - if (!itemClone.hasOwnProperty(paths[j])) { + for (var k = 0; k < val.length; k++) { // i.e. 0 and 1 + paths = val[k].split('.'); + itemClone = item; + for (var l = 0; l < paths.length; l++) { + if (itemClone == undefined || !itemClone.hasOwnProperty(paths[l])) { return false; } - itemClone = itemClone[paths[j]]; // Iterate deeper into the object + itemClone = itemClone[paths[l]]; // Iterate deeper into the object } - coordinates[i] = itemClone; + coordinates[k] = itemClone; } geom.type = gtype; geom.coordinates = [Number(coordinates[1]), Number(coordinates[0])]; diff --git a/geojson.min.js b/geojson.min.js index e727334..c000d1b 100644 --- a/geojson.min.js +++ b/geojson.min.js @@ -1,3 +1,4 @@ // geojson.js - v0.4.1 -// (c) 2016 Casey Thomas, MIT License -!function(a){function b(){var a=1<=arguments.length?[].slice.call(arguments,0):[],b=a.shift(),c=a.shift();Error.apply(this,a),this.message=this.message||"Invalid Geometry: item: "+JSON.stringify(b)+", params: "+JSON.stringify(c)}function c(a,b){var c=a||{};for(var d in b)b.hasOwnProperty(d)&&!c[d]&&(c[d]=b[d]);return c}function d(a,b){if(b.crs&&e(b.crs)&&(b.isPostgres?a.geometry.crs=b.crs:a.crs=b.crs),b.bbox&&(a.bbox=b.bbox),b.extraGlobal){a.properties={};for(var c in b.extraGlobal)a.properties[c]=b.extraGlobal[c]}}function e(a){if("name"===a.type){if(a.properties&&a.properties.name)return!0;throw new Error('Invalid CRS. Properties must contain "name" key')}if("link"===a.type){if(a.properties&&a.properties.href&&a.properties.type)return!0;throw new Error('Invalid CRS. Properties must contain "href" and "type" key')}throw new Error('Invald CRS. Type attribute must be "name" or "link"')}function f(a){a.geom={};for(var b in a)a.hasOwnProperty(b)&&-1!==m.indexOf(b)&&(a.geom[b]=a[b],delete a[b]);g(a.geom)}function g(a){for(var b in a)a.hasOwnProperty(b)&&("string"==typeof a[b]?n.push(a[b]):"object"==typeof a[b]&&(n.push(a[b][0]),n.push(a[b][1])));if(0===n.length)throw new Error("No geometry attributes specified")}function h(a){var b=a.item,c=a.params,d=a.propFunc,e={type:"Feature"};return e.geometry=j(b,c),e.properties=d.call(b),e}function i(a){return/^.+\..+$/.test(a)}function j(c,d){var e={};for(var f in d.geom){var g=d.geom[f];if("string"==typeof g&&c.hasOwnProperty(g))"GeoJSON"===f?e=c[g]:(e.type=f,e.coordinates=c[g]);else if("object"!=typeof g||Array.isArray(g))if(Array.isArray(g)&&c.hasOwnProperty(g[0])&&c.hasOwnProperty(g[1]))e.type=f,e.coordinates=[Number(c[g[1]]),Number(c[g[0]])];else if(Array.isArray(g)&&i(g[0])&&i(g[1])){for(var h=[],k=0;k