Newer
Older
/**
* Created by gavorhes on 12/23/2015.
*/
import provide from '../util/provide';
let nm = provide('olHelpers');
/**
* take an array of features and sort by a given property name
*/
* @param {string} propertyName - the property name to use for lookup
*/
this.sortedFeatures = features;
this.propertyName = propertyName;
// if (this.sortedFeatures.length > 0) {
// this._propertyType = typeof this.sortedFeatures[0].getProperties()[this.propertyName];
//
// // let __this = this;
// this.sortedFeatures.sort( (a: Feature, b: Feature) : number =>{
// if (this._propertyType == 'number'){
// let aMinusB = a.getProperties()[this.propertyName] - b.getProperties()[this.propertyName];
// if (aMinusB == 0){
// return 0;
// } else {
// return aMinusB > 0 ? 1 : -1;
// }
// } else if (this._propertyType == 'string'){
// let propA = a.getProperties()[this.propertyName] || '';
// let propB = b.getProperties()[this.propertyName] || '';
// propA = propA.toString().trim();
// propB = propB.toString().trim();
//
// if (propA == propB){
// return 0;
// } else {
// return propA > propB ? 1 : 0;
// }
// }
// });
// }
}
/**
* recursive search to find the value
* @param {number|string} propertyValue - the property value to search for
* @param {boolean} [exactMatch=false] if only an exact match should be returned
* @param {Array} [sortedFeatures=this.sortedFeatures] - the candidate features
getFeature(propertyValue: number|string, exactMatch: boolean = false, sortedFeatures? :Array<Feature>): Feature {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if (typeof sortedFeatures == 'undefined'){
sortedFeatures = this.sortedFeatures;
}
if (sortedFeatures.length == 0){
return undefined;
}
if (sortedFeatures.length == 1){
if (exactMatch){
if (sortedFeatures[0].getProperties()[this.propertyName] == propertyValue){
return sortedFeatures[0];
} else {
return undefined;
}
} else {
return sortedFeatures[0];
}
}
let lowProp = sortedFeatures[0].getProperties()[this.propertyName];
let highProp = sortedFeatures[sortedFeatures.length - 1].getProperties()[this.propertyName];
if (exactMatch){
if (lowProp == propertyValue){
return sortedFeatures[0];
} else if (propertyValue < lowProp){
return undefined;
} else if (highProp == propertyValue){
return sortedFeatures[sortedFeatures.length - 1];
} else if (propertyValue > highProp){
return undefined;
}
} else {
if (propertyValue <= lowProp){
return sortedFeatures[0];
} else if (propertyValue >= highProp){
return sortedFeatures[sortedFeatures.length - 1];
}
}
let midIndex = Math.floor(sortedFeatures.length / 2);
let midFeature = sortedFeatures[midIndex];
let midProperty = midFeature.getProperties()[this.propertyName];
if (midProperty === propertyValue){
return midFeature;
}
if (propertyValue < midProperty){
return this.getFeature(propertyValue, exactMatch, sortedFeatures.slice(0, midIndex));
} else {
return this.getFeature(propertyValue, exactMatch, sortedFeatures.slice(midIndex));
}
}
}
nm.SortedFeatures = SortedFeatures;
export default SortedFeatures;