Skip to content

Commit

Permalink
Place segmentIndex at end of callback
Browse files Browse the repository at this point in the history
Ref: #968
  • Loading branch information
DenisCarriere committed Sep 26, 2017
1 parent 77b97ac commit 4be4b1d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/turf-meta/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function flattenEach<Geom extends GeometryObject>(
*/
export function segmentReduce<Reducer extends any>(
geojson: Feature<any> | FeatureCollection<any> | GeometryObject | GeometryCollection | FeatureGeometryCollection,
callback: (previousValue?: Reducer, currentSegment?: Feature<LineString>, featureIndex?: number, featureSubIndex?: number) => Reducer,
callback: (previousValue?: Reducer, currentSegment?: Feature<LineString>, featureIndex?: number, featureSubIndex?: number, segmentIndex?: number) => Reducer,
initialValue?: Reducer
): Reducer;

Expand All @@ -121,7 +121,7 @@ export function segmentReduce<Reducer extends any>(
*/
export function segmentEach(
geojson: Feature<any> | FeatureCollection<any> | GeometryObject | GeometryCollection | FeatureGeometryCollection,
allback: (currentSegment?: Feature<LineString>, featureIndex?: number, featureSubIndex?: number) => void
callback: (currentSegment?: Feature<LineString>, featureIndex?: number, featureSubIndex?: number, segmentIndex?: number) => void
): void;

/**
Expand Down
18 changes: 9 additions & 9 deletions packages/turf-meta/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,9 @@ export function flattenReduce(geojson, callback, initialValue) {
*
* @callback segmentEachCallback
* @param {Feature<LineString>} currentSegment The current segment being processed.
* @param {number} segmentIndex The segmentIndex currently being processed, starts at index 0.
* @param {number} featureIndex The featureIndex currently being processed, starts at index 0.
* @param {number} featureSubIndex The featureSubIndex currently being processed, starts at index 0.
* @param {number} segmentIndex The segmentIndex currently being processed, starts at index 0.
* @returns {void}
*/

Expand All @@ -724,11 +724,11 @@ export function flattenReduce(geojson, callback, initialValue) {
* var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]);
*
* // Iterate over GeoJSON by 2-vertex segments
* turf.segmentEach(polygon, function (currentSegment, featureIndex, featureSubIndex) {
* turf.segmentEach(polygon, function (currentSegment, featureIndex, featureSubIndex, segmentIndex) {
* //= currentSegment
* //= segmentIndex
* //= featureIndex
* //= featureSubIndex
* //= segmentIndex
* });
*
* // Calculate the total number of segments
Expand All @@ -750,7 +750,7 @@ export function segmentEach(geojson, callback) {
// Generate 2-vertex line segments
coordReduce(feature, function (previousCoords, currentCoord) {
var currentSegment = lineString([previousCoords, currentCoord], feature.properties);
callback(currentSegment, segmentIndex, featureIndex, featureSubIndex);
callback(currentSegment, featureIndex, featureSubIndex, segmentIndex);
segmentIndex++;
return currentCoord;
});
Expand All @@ -775,9 +775,9 @@ export function segmentEach(geojson, callback) {
* @param {*} [previousValue] The accumulated value previously returned in the last invocation
* of the callback, or initialValue, if supplied.
* @param {Feature<LineString>} [currentSegment] The current segment being processed.
* @param {number} segmentIndex The segmentIndex currently being processed, starts at index 0.
* @param {number} featureIndex The featureIndex currently being processed, starts at index 0.
* @param {number} featureSubIndex The featureSubIndex currently being processed, starts at index 0.
* @param {number} segmentIndex The segmentIndex currently being processed, starts at index 0.
*/

/**
Expand All @@ -792,12 +792,12 @@ export function segmentEach(geojson, callback) {
* var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]);
*
* // Iterate over GeoJSON by 2-vertex segments
* turf.segmentReduce(polygon, function (previousSegment, currentSegment, segmentIndex, featureIndex, featureSubIndex) {
* turf.segmentReduce(polygon, function (previousSegment, currentSegment, featureIndex, featureSubIndex, segmentIndex) {
* //= previousSegment
* //= currentSegment
* //= segmentInex
* //= featureIndex
* //= featureSubIndex
* //= segmentInex
* return currentSegment
* });
*
Expand All @@ -811,9 +811,9 @@ export function segmentEach(geojson, callback) {
export function segmentReduce(geojson, callback, initialValue) {
var previousValue = initialValue;
var started = false;
segmentEach(geojson, function (currentSegment, segmentIndex, featureIndex, featureSubIndex) {
segmentEach(geojson, function (currentSegment, featureIndex, featureSubIndex, segmentIndex) {
if (started === false && initialValue === undefined) previousValue = currentSegment;
else previousValue = callback(previousValue, currentSegment, segmentIndex, featureIndex, featureSubIndex);
else previousValue = callback(previousValue, currentSegment, featureIndex, featureSubIndex, segmentIndex);
started = true;
});
return previousValue;
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-meta/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turf/meta",
"version": "4.7.3",
"version": "4.7.4",
"description": "turf meta module",
"main": "index.es5.js",
"module": "index.js",
Expand Down
14 changes: 7 additions & 7 deletions packages/turf-meta/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,34 +466,34 @@ const geojsonSegments = featureCollection([
]);

test('segmentEach -- index & subIndex', t => {
const segmentIndexes = [];
const featureIndexes = [];
const featureSubIndexes = [];
const segmentIndexes = [];
let total = 0;

meta.segmentEach(geojsonSegments, (segment, segmentIndex, featureIndex, featureSubIndex) => {
segmentIndexes.push(segmentIndex);
meta.segmentEach(geojsonSegments, (segment, featureIndex, featureSubIndex, segmentIndex) => {
featureIndexes.push(featureIndex);
featureSubIndexes.push(featureSubIndex);
segmentIndexes.push(segmentIndex);
total++;
});
t.equal(total, 10, 'total');
t.deepEqual(segmentIndexes, [0, 1, 0, 1, 2, 3, 0, 1, 0, 1], 'segmentEach.segmentIndex');
t.deepEqual(featureIndexes, [1, 1, 2, 2, 2, 2, 4, 4, 4, 4], 'segmentEach.featureIndex');
t.deepEqual(featureSubIndexes, [0, 0, 0, 0, 0, 0, 0, 0, 1, 1], 'segmentEach.featureSubIndex');
t.deepEqual(segmentIndexes, [0, 1, 0, 1, 2, 3, 0, 1, 0, 1], 'segmentEach.segmentIndex');
t.end();
});

test('segmentReduce -- index & subIndex', t => {
const segmentIndexes = [];
const featureIndexes = [];
const featureSubIndexes = [];
const segmentIndexes = [];
let total = 0;

meta.segmentReduce(geojsonSegments, (previousValue, segment, segmentIndex, featureIndex, featureSubIndex) => {
segmentIndexes.push(segmentIndex);
meta.segmentReduce(geojsonSegments, (previousValue, segment, featureIndex, featureSubIndex, segmentIndex) => {
featureIndexes.push(featureIndex);
featureSubIndexes.push(featureSubIndex);
segmentIndexes.push(segmentIndex);
total++;
});
t.equal(total, 9, 'total');
Expand Down

0 comments on commit 4be4b1d

Please sign in to comment.