exaquark-js
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -11,2 +11,4 @@ 'use strict'; | ||
var _distance = require('./utils/distance'); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -192,11 +194,11 @@ | ||
// list of neighbors | ||
this.onNeighborsMessage(data.neighbors); | ||
if (data.neighbors) this.onNeighborsMessage(data.neighbors); | ||
break; | ||
case 'updates': | ||
// moved etc | ||
this.onUpdatesMessage(data.neighbors); | ||
if (data.neighbors) this.onUpdatesMessage(data.neighbors); | ||
break; | ||
case 'removes': | ||
// leaving neighborhood | ||
this.onRemovesMessage(data.neighbors); | ||
if (data.neighbors) this.onRemovesMessage(data.neighbors); | ||
break; | ||
@@ -281,2 +283,5 @@ } | ||
value: function push(eventName, payload) { | ||
if (!this.canPush()) { | ||
return; | ||
} | ||
switch (eventName) { | ||
@@ -297,3 +302,3 @@ case 'update:state': | ||
value: function canPush() { | ||
return !!this.conn; | ||
return !!this.conn && this.conn.readyState === this.conn.OPEN; | ||
} | ||
@@ -303,7 +308,3 @@ }, { | ||
value: function neighbors(format) { | ||
var _this6 = this; | ||
if (format && format === 'Array') return Object.keys(this.neighborHash).map(function (key) { | ||
return _this6.neighborHash[key]; | ||
});else return this.neighborHash; | ||
if (format && format === 'Array') return (0, _private.dictionaryToArray)(this.neighborHash);else return this.neighborHash; | ||
} | ||
@@ -328,3 +329,3 @@ }, { | ||
this.state = state; | ||
(0, _private.log)(this.logger, 'sending update', state); | ||
// log(this.logger, 'sending update', state) | ||
var payload = { | ||
@@ -347,2 +348,34 @@ method: 'update', | ||
} | ||
/** | ||
* Returns the distance between two entities | ||
* @param {string} [options.units] - the unit of measurement. Defaults to meters | ||
*/ | ||
}, { | ||
key: 'getDistanceBetweenEntities', | ||
value: function getDistanceBetweenEntities(entityOne, entityTwo, options) { | ||
return (0, _distance.distanceOnSphere)(entityOne.geo.lat, entityOne.geo.lng, entityTwo.geo.lat, entityTwo.geo.lng); | ||
} | ||
/** | ||
* Gets a list of neighbors within a specified distance | ||
* @param {number} distance | ||
* @param {string} [options.listType] - the list format to return. Defaults to Dictionary. Options: "Array" | "Dict" | ||
* @param {string} [options.units] - the unit of measurement. Defaults to meters | ||
*/ | ||
}, { | ||
key: 'getNeighborsByMaxDistance', | ||
value: function getNeighborsByMaxDistance(distance) { | ||
var _this6 = this; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var filteredList = (0, _private.dictionaryToArray)(this.neighborHash).filter(function (x) { | ||
return distance >= (0, _distance.distanceOnSphere)(_this6.state.geo.lat, _this6.state.geo.lng, x.geo.lat, x.geo.lng); | ||
}); | ||
if (options.listType === 'Array') return filteredList;else return (0, _private.arrayToDictionary)(filteredList); | ||
} | ||
}]); | ||
@@ -349,0 +382,0 @@ |
@@ -8,2 +8,17 @@ "use strict"; | ||
logger(msg, data); | ||
}; | ||
// Converts a dictionary to an array | ||
_exports.dictionaryToArray = function (dict) { | ||
return Object.keys(dict).map(function (key) { | ||
return dict[key]; | ||
}); | ||
}; | ||
// Converts a dictionary to an array | ||
_exports.arrayToDictionary = function (arr) { | ||
return arr.reduce(function (map, obj) { | ||
map[obj.iid] = obj; | ||
return map; | ||
}, {}); | ||
}; |
{ | ||
"name": "exaquark-js", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "JS wrapper for exaquark.com", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -11,3 +11,5 @@ # exaQuark JS | ||
### Documentation | ||
See full docs at [https://docs.exaquark.io/](https://docs.exaquark.io/) | ||
@@ -30,5 +32,4 @@ | ||
#### Demo | ||
TODOS: | ||
- [ ] add API key - the connection should be a POST request? | ||
You can see a demo of the library in action in `demo`. See `demo/README.md` for instructions. |
@@ -1,2 +0,3 @@ | ||
import { log } from './utils/private' | ||
import { log, dictionaryToArray, arrayToDictionary } from './utils/private' | ||
import { distanceOnSphere } from './utils/distance' | ||
@@ -139,9 +140,9 @@ const loadJSONP = (() => { | ||
case 'neighbors': // list of neighbors | ||
this.onNeighborsMessage(data.neighbors) | ||
if (data.neighbors) this.onNeighborsMessage(data.neighbors) | ||
break | ||
case 'updates': // moved etc | ||
this.onUpdatesMessage(data.neighbors) | ||
if (data.neighbors) this.onUpdatesMessage(data.neighbors) | ||
break | ||
case 'removes': // leaving neighborhood | ||
this.onRemovesMessage(data.neighbors) | ||
if (data.neighbors) this.onRemovesMessage(data.neighbors) | ||
break | ||
@@ -197,2 +198,3 @@ } | ||
push (eventName, payload) { | ||
if (!this.canPush()) { return } | ||
switch (eventName) { | ||
@@ -211,6 +213,6 @@ case 'update:state': | ||
canPush () { | ||
return !!this.conn | ||
return !!this.conn && (this.conn.readyState === this.conn.OPEN) | ||
} | ||
neighbors (format) { | ||
if (format && format === 'Array') return Object.keys(this.neighborHash).map(key => this.neighborHash[key]) | ||
if (format && format === 'Array') return dictionaryToArray(this.neighborHash) | ||
else return this.neighborHash | ||
@@ -228,3 +230,3 @@ } | ||
this.state = state | ||
log(this.logger, 'sending update', state) | ||
// log(this.logger, 'sending update', state) | ||
let payload = { | ||
@@ -245,3 +247,25 @@ method: 'update', | ||
} | ||
/** | ||
* Returns the distance between two entities | ||
* @param {string} [options.units] - the unit of measurement. Defaults to meters | ||
*/ | ||
getDistanceBetweenEntities (entityOne, entityTwo, options) { | ||
return distanceOnSphere(entityOne.geo.lat, entityOne.geo.lng, entityTwo.geo.lat, entityTwo.geo.lng) | ||
} | ||
/** | ||
* Gets a list of neighbors within a specified distance | ||
* @param {number} distance | ||
* @param {string} [options.listType] - the list format to return. Defaults to Dictionary. Options: "Array" | "Dict" | ||
* @param {string} [options.units] - the unit of measurement. Defaults to meters | ||
*/ | ||
getNeighborsByMaxDistance (distance, options = {}) { | ||
let filteredList = dictionaryToArray(this.neighborHash) | ||
.filter(x => distance >= distanceOnSphere(this.state.geo.lat, this.state.geo.lng, x.geo.lat, x.geo.lng)) | ||
if (options.listType === 'Array') return filteredList | ||
else return arrayToDictionary(filteredList) | ||
} | ||
} | ||
export default exaQuark |
@@ -7,1 +7,14 @@ var exports = module.exports = {} | ||
} | ||
// Converts a dictionary to an array | ||
exports.dictionaryToArray = function (dict) { | ||
return Object.keys(dict).map(key => dict[key]) | ||
} | ||
// Converts a dictionary to an array | ||
exports.arrayToDictionary = function (arr) { | ||
return arr.reduce(function (map, obj) { | ||
map[obj.iid] = obj | ||
return map | ||
}, {}) | ||
} |
24924
8
642
34