Comparing version 0.0.8 to 0.1.0
(function(GeoJSON) { | ||
GeoJSON.version = '0.0.8'; | ||
GeoJSON.version = '0.1.0'; | ||
// Allow user to specify default parameters | ||
GeoJSON.defaults = {}; | ||
// The one and only public function. | ||
// Converts an array of objects into a GeoJSON feature collection | ||
GeoJSON.parse = function(objects, params) { | ||
if(objects.length === 0) { throw new Error('No data found'); } | ||
var geojson = {"type": "FeatureCollection", "features": []}, | ||
settings = applyDefaults(params, this.defaults); | ||
geomAttrs.length = 0; // Reset the list of geometry fields | ||
setGeom(settings); | ||
@@ -24,2 +30,5 @@ | ||
// Adds default settings to user-specified params | ||
// Does not overwrite any settings--only adds defaults | ||
// the the user did not specify | ||
function applyDefaults(params, defaults) { | ||
@@ -37,2 +46,4 @@ var settings = params || {}; | ||
// Adds the optional GeoJSON properties crs and bbox | ||
// if they have been specified | ||
function addOptionals(geojson, settings){ | ||
@@ -52,2 +63,4 @@ if(settings.crs) { | ||
// Moves the user-specified geometry parameters | ||
// under the `geom` key in param for easier access | ||
function setGeom(params) { | ||
@@ -66,2 +79,6 @@ params.geom = {}; | ||
// Adds fields which contain geometry data | ||
// to geomAttrs. This list is used when adding | ||
// properties to the features so that no geometry | ||
// fields are added the properties key | ||
function setGeomAttrList(params) { | ||
@@ -78,4 +95,8 @@ for(var param in params) { | ||
} | ||
if(geomAttrs.length === 0) { throw new Error('No geometry attributes specified'); } | ||
} | ||
// Creates a feature object to be added | ||
// to the GeoJSON features array | ||
function getFeature(item, params) { | ||
@@ -90,2 +111,4 @@ var feature = { "type": "Feature" }; | ||
// Assembles the `geometry` property | ||
// for the feature output | ||
function buildGeom(item, params) { | ||
@@ -100,3 +123,3 @@ var geom = {}; | ||
if (typeof params.geom[gtype] === 'string') { | ||
if(typeof params.geom[gtype] === 'string') { | ||
geom.coordinates = item[params.geom[gtype]]; | ||
@@ -114,7 +137,10 @@ } else { // Point with geom stored in two attributes | ||
// Assemblies the `properties` property | ||
// for the feature ouput | ||
function buildProps(item, params) { | ||
var properties = {}; | ||
var properties = {}, | ||
attr; | ||
if (!params.exclude && !params.include) { | ||
for(var attr in item) { | ||
if(!params.exclude && !params.include) { | ||
for(attr in item) { | ||
if(item.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1)) { | ||
@@ -124,8 +150,8 @@ properties[attr] = item[attr]; | ||
} | ||
} else if (params.include) { | ||
} else if(params.include) { | ||
params.include.forEach(function(attr){ | ||
properties[attr] = item[attr]; | ||
}); | ||
} else if (params.exclude) { | ||
for(var attr in item) { | ||
} else if(params.exclude) { | ||
for(attr in item) { | ||
if(item.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1) && (params.exclude.indexOf(attr) === -1)) { | ||
@@ -137,5 +163,19 @@ properties[attr] = item[attr]; | ||
if(params.extra) { addExtra(properties, params.extra); } | ||
return properties; | ||
} | ||
// Adds data contained in the `extra` | ||
// parameter if it has been specified | ||
function addExtra(properties, extra) { | ||
for(var key in extra){ | ||
if(extra.hasOwnProperty(key)) { | ||
properties[key] = extra[key]; | ||
} | ||
} | ||
return properties; | ||
} | ||
}(typeof module == 'object' ? module.exports : window.GeoJSON = {})); |
@@ -5,3 +5,3 @@ { | ||
"author": "Casey Thomas <c@cpt.ph>", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"main": "./geojson", | ||
@@ -16,3 +16,4 @@ "repository": { | ||
"mocha": "*", | ||
"expect.js": "*" | ||
"expect.js": "*", | ||
"grunt": ">=0.3.16" | ||
}, | ||
@@ -19,0 +20,0 @@ "engines": { |
@@ -9,3 +9,3 @@ # geojson.js | ||
In the browser, include `geojson.js`. For example: `<script type="text/javascript" src="js/geojson.js"></script>` | ||
In the browser, include `geojson.min.js`. For example: `<script type="text/javascript" src="js/geojson.min.js"></script>` | ||
@@ -42,3 +42,3 @@ ## Getting Started | ||
category: 'Office', | ||
street: 'South' | ||
street: 'South', | ||
lat: 39.123, | ||
@@ -53,3 +53,4 @@ lng: -74.534 | ||
{ "type": "FeatureCollection", | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
@@ -84,3 +85,4 @@ { "type": "Feature", | ||
{ "type": "FeatureCollection", | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
@@ -267,2 +269,33 @@ { "type": "Feature", | ||
You can add arbitrary properties to features using the `extra` param. The value for `extra` must be an object.For example, using the original sample data: | ||
GeoJSON.parse(data, { | ||
Point: ['lat', 'lng'], | ||
extra: { | ||
style: { | ||
"color": "#ff7800", | ||
"weight": 5, | ||
"opacity": 0.65 | ||
} | ||
} | ||
}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"name": "Location A", | ||
"category": "Store", | ||
"style": { | ||
"color": "#ff7800", | ||
"weight": 5, | ||
"opacity": 0.65 | ||
} | ||
} | ||
}, | ||
... | ||
} | ||
## Tests | ||
@@ -269,0 +302,0 @@ |
@@ -194,2 +194,4 @@ var GeoJSON = require('../geojson'); | ||
}); | ||
GeoJSON.defaults = {}; | ||
}); | ||
@@ -210,3 +212,39 @@ | ||
it("should add extra attributes if extra param is set", function() { | ||
var output = GeoJSON.parse(data, {Point: ['lat', 'lng'], extra: { 'foo':'bar', 'bar':'foo'}}); | ||
output.features.forEach(function(feature){ | ||
expect(feature.properties.foo).to.be('bar'); | ||
expect(feature.properties.bar).to.be('foo'); | ||
}); | ||
var output2 = GeoJSON.parse(data, { | ||
Point: ['lat', 'lng'], | ||
extra: { | ||
style: { | ||
"color": "#ff7800", | ||
"weight": 5, | ||
"opacity": 0.65 | ||
} | ||
} | ||
}); | ||
output2.features.forEach(function(feature) { | ||
expect(feature.properties.style.color).to.be('#ff7800'); | ||
expect(feature.properties.style.weight).to.be(5); | ||
expect(feature.properties.style.opacity).to.be(0.65); | ||
}); | ||
}); | ||
it("should throw an error if the objects parameter is empty", function(){ | ||
var data = []; | ||
expect(function(){ GeoJSON.parse(data); }).to.throwException(/No data found/); | ||
}); | ||
it("should throw an error if no geometry attributes have been specified", function() { | ||
expect(function(){ GeoJSON.parse(data); }).to.throwException(/No geometry attributes specified/); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
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
35878
9
365
300
0
3