-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for missing modules in @turf/turf
- Loading branch information
1 parent
b9d1cf8
commit 00cc41f
Showing
10 changed files
with
249 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
var featureEach = require('@turf/meta').featureEach; | ||
|
||
/** | ||
* Create Bins | ||
* | ||
* @private | ||
* @param {FeatureCollection} geojson GeoJSON Features | ||
* @param {string|number} property Property values are used to create bins | ||
* @returns {Object} bins with Feature IDs | ||
* @example | ||
* const geojson = turf.featureCollection([ | ||
* turf.point([0, 0], {cluster: 0, foo: 'null'}), | ||
* turf.point([2, 4], {cluster: 1, foo: 'bar'}), | ||
* turf.point([5, 1], {0: 'foo'}), | ||
* turf.point([3, 6], {cluster: 1}), | ||
* ]); | ||
* createBins(geojson, 'cluster'); | ||
* //= { '0': [ 0 ], '1': [ 1, 3 ] } | ||
*/ | ||
function createBins(geojson, property) { | ||
var bins = {}; | ||
|
||
featureEach(geojson, function (feature, i) { | ||
var properties = feature.properties || {}; | ||
if (properties.hasOwnProperty(property)) { | ||
var value = properties[property]; | ||
if (bins.hasOwnProperty(value)) bins[value].push(i); | ||
else bins[value] = [i]; | ||
} | ||
}); | ||
return bins; | ||
} | ||
|
||
/** | ||
* Apply Filter | ||
* | ||
* @private | ||
* @param {*} properties Properties | ||
* @param {*} filter Filter | ||
* @returns {Boolean} applied Filter to properties | ||
*/ | ||
function applyFilter(properties, filter) { | ||
if (properties === undefined) return false; | ||
var filterType = typeof filter; | ||
|
||
// String & Number | ||
if (filterType === 'number' || filterType === 'string') return properties.hasOwnProperty(filter); | ||
// Array | ||
else if (Array.isArray(filter)) { | ||
for (var i = 0; i < filter.length; i++) { | ||
if (!applyFilter(properties, filter[i])) return false; | ||
} | ||
return true; | ||
// Object | ||
} else { | ||
return propertiesContainsFilter(properties, filter); | ||
} | ||
} | ||
|
||
/** | ||
* Properties contains filter (does not apply deepEqual operations) | ||
* | ||
* @private | ||
* @param {*} properties Properties | ||
* @param {Object} filter Filter | ||
* @returns {Boolean} does filter equal Properties | ||
* @example | ||
* propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0}) | ||
* //= true | ||
* propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1}) | ||
* //= false | ||
*/ | ||
function propertiesContainsFilter(properties, filter) { | ||
var keys = Object.keys(filter); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (properties[key] !== filter[key]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Filter Properties | ||
* | ||
* @private | ||
* @param {*} properties Properties | ||
* @param {string[]} keys Used to filter Properties | ||
* @returns {*} filtered Properties | ||
* @example | ||
* filterProperties({foo: 'bar', cluster: 0}, ['cluster']) | ||
* //= {cluster: 0} | ||
*/ | ||
function filterProperties(properties, keys) { | ||
if (!keys) return {}; | ||
if (!keys.length) return {}; | ||
|
||
var newProperties = {}; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (properties.hasOwnProperty(key)) newProperties[key] = properties[key]; | ||
} | ||
return newProperties; | ||
} | ||
|
||
module.exports = { | ||
createBins: createBins, | ||
applyFilter: applyFilter, | ||
propertiesContainsFilter: propertiesContainsFilter, | ||
filterProperties: filterProperties | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.