diff --git a/dist/olHelpers/SortedFeatures.d.ts b/dist/olHelpers/SortedFeatures.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..d89af204c6b67074402e959eec35b0bc392fff4e --- /dev/null +++ b/dist/olHelpers/SortedFeatures.d.ts @@ -0,0 +1,24 @@ +import { ol } from 'custom-ol'; +/** + * take an array of features and sort by a given property name + */ +declare class SortedFeatures { + sortedFeatures: Array<ol.Feature>; + propertyName: string; + _propertyType: string; + /** + * + * @param {Array<ol.Feature>} features array of ol features + * @param {string} propertyName - the property name to use for lookup + */ + constructor(features: any, propertyName: any); + /** + * 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 + * @returns {ol.Feature|undefined} the feature matching the lookup + */ + getFeature(propertyValue: any, exactMatch: any, sortedFeatures: any): any; +} +export default SortedFeatures; diff --git a/dist/olHelpers/SortedFeatures.js b/dist/olHelpers/SortedFeatures.js new file mode 100644 index 0000000000000000000000000000000000000000..132cc8bcd43c287693571439d9cba7274eceece1 --- /dev/null +++ b/dist/olHelpers/SortedFeatures.js @@ -0,0 +1,119 @@ +"use strict"; +/** + * Created by gavorhes on 12/23/2015. + */ +var provide_1 = require('../util/provide'); +var nm = provide_1.default('olHelpers'); +/** + * take an array of features and sort by a given property name + */ +var SortedFeatures = (function () { + /** + * + * @param {Array<ol.Feature>} features array of ol features + * @param {string} propertyName - the property name to use for lookup + */ + function SortedFeatures(features, propertyName) { + this.sortedFeatures = features; + this.propertyName = propertyName; + if (this.sortedFeatures.length > 0) { + this._propertyType = typeof this.sortedFeatures[0].getProperties()[this.propertyName]; + var __this_1 = this; + this.sortedFeatures.sort(function (a, b) { + if (__this_1._propertyType == 'number') { + var aMinusB = a['getProperties']()[__this_1.propertyName] - b['getProperties']()[__this_1.propertyName]; + if (aMinusB == 0) { + return 0; + } + else { + return aMinusB > 0 ? 1 : -1; + } + } + else if (__this_1._propertyType == 'string') { + var propA = a['getProperties']()[__this_1.propertyName] || ''; + var propB = b['getProperties']()[__this_1.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 + * @returns {ol.Feature|undefined} the feature matching the lookup + */ + SortedFeatures.prototype.getFeature = function (propertyValue, exactMatch, sortedFeatures) { + if (typeof sortedFeatures == 'undefined') { + sortedFeatures = this.sortedFeatures; + } + if (typeof exactMatch !== 'boolean') { + exactMatch = false; + } + 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]; + } + } + var lowProp = sortedFeatures[0].getProperties()[this.propertyName]; + var 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]; + } + } + var midIndex = Math.floor(sortedFeatures.length / 2); + var midFeature = sortedFeatures[midIndex]; + var 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)); + } + }; + return SortedFeatures; +}()); +nm.SortedFeatures = SortedFeatures; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = SortedFeatures; +//# sourceMappingURL=SortedFeatures.js.map \ No newline at end of file diff --git a/dist/olHelpers/SortedFeatures.js.map b/dist/olHelpers/SortedFeatures.js.map new file mode 100644 index 0000000000000000000000000000000000000000..6797239e69a6e965fef700ca20722f3de3270fd2 --- /dev/null +++ b/dist/olHelpers/SortedFeatures.js.map @@ -0,0 +1 @@ +{"version":3,"file":"SortedFeatures.js","sourceRoot":"","sources":["../../src-ts/olHelpers/SortedFeatures.ts"],"names":[],"mappings":";AAAA;;GAEG;AACH,wBAAoB,iBAAiB,CAAC,CAAA;AAEtC,IAAI,EAAE,GAAG,iBAAO,CAAC,WAAW,CAAC,CAAC;AAE9B;;GAEG;AACH;IAKI;;;;OAIG;IACH,wBAAY,QAAQ,EAAE,YAAY;QAC9B,IAAI,CAAC,cAAc,GAAG,QAAQ,CAAC;QAC/B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QAEjC,EAAE,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;YACjC,IAAI,CAAC,aAAa,GAAG,OAAO,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;YAEtF,IAAI,QAAM,GAAG,IAAI,CAAC;YAClB,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;gBACnC,EAAE,CAAC,CAAC,QAAM,CAAC,aAAa,IAAI,QAAQ,CAAC,CAAA,CAAC;oBAClC,IAAI,OAAO,GAAG,CAAC,CAAC,eAAe,CAAC,EAAE,CAAC,QAAM,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,eAAe,CAAC,EAAE,CAAC,QAAM,CAAC,YAAY,CAAC,CAAC;oBACpG,EAAE,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,CAAA,CAAC;wBACd,MAAM,CAAC,CAAC,CA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ers/SortedFeatures.js b/src-ts/olHelpers/SortedFeatures.ts similarity index 74% rename from src-ts/olHelpers/SortedFeatures.js rename to src-ts/olHelpers/SortedFeatures.ts index bba3f45a8a08b38a6b3c5f5df1febdde14e7dfcb..adb814f6d440d156c8a45cfd10945d741aa341ab 100644 --- a/src-ts/olHelpers/SortedFeatures.js +++ b/src-ts/olHelpers/SortedFeatures.ts @@ -2,12 +2,16 @@ * Created by gavorhes on 12/23/2015. */ import provide from '../util/provide'; +import {ol} from 'custom-ol' let nm = provide('olHelpers'); /** * take an array of features and sort by a given property name */ class SortedFeatures { + sortedFeatures: Array<ol.Feature>; + propertyName: string; + _propertyType: string; /** * @@ -21,12 +25,26 @@ class SortedFeatures { if (this.sortedFeatures.length > 0) { this._propertyType = typeof this.sortedFeatures[0].getProperties()[this.propertyName]; - let _this = this; - this.sortedFeatures.sort(function (a, b) { - if (_this._propertyType == 'number'){ - return (a['getProperties']()[_this.propertyName] - b['getProperties']()[_this.propertyName]); - } else if (_this._propertyType == 'string'){ - return (a['getProperties']()[_this.propertyName] > b['getProperties']()[_this.propertyName]); + let __this = this; + this.sortedFeatures.sort(function (a, b) : 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; + } } }); }