From 78254ed3a4911e202bf0eb417a291e82f67006c2 Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Wed, 5 Oct 2016 17:13:53 -0400 Subject: [PATCH] feat(naming): Switching from lng to lon cause... In line with the name of this library we are changing the standard short representation of longitude to be lon instead of lng. The author (Hi!) mistakenly thought through his experience that lng was the more conventional representation and it has given himself and others headaches. --- README.md | 39 +++++++++++++++++++-------------------- index.js | 38 +++++++++++++++++++------------------- test.js | 32 ++++++++++++++++---------------- 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 68f2bb8..47c6a14 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # lonlng Lat/lon normalization cause...**sigh**. -No one has agreed on a standard way of representing lat/lng. This is a small normalization library. Use this to convert all outside input before processing internally and convert to an external format right when it's being output. +No one has agreed on a standard way of representing lat/lon. This is a small normalization library. Use this to convert all outside input before processing internally and convert to an external format right when it's being output. -## Just use the `{lng: ${longitude}, lat: ${latitude}}` representation +## Just use the `{lon: ${longitude}, lat: ${latitude}}` representation Utilizing this won't always be possible/easiest, so please at least adopt the following conventions. Any variables or functions that contain the following names should be represented by the accompanying structure: -* `latlng`: `{lng: ${longitude}, lat: ${latitude}}` +* `latlon`: `{lon: ${longitude}, lat: ${latitude}}` * `coordinates`: `[${longitude}, ${latitude}]` * `point`: `{x: ${longitude}, y: ${latitude}}` @@ -22,30 +22,29 @@ import assert from 'assert' import ll from 'lonlng' const lat = 38.13234 -const lng = 70.01232 -const latlng = {lat, lng} -const point = {x: lng, y: lat} -const coordinates = [lng, lat] -const str = `${lng},${lat}` +const lon = 70.01232 +const latlon = {lat, lon} +const point = {x: lon, y: lat} +const coordinates = [lon, lat] +const str = `${lon},${lat}` const pairs = [ // normalization - [latlng, ll(latlng)], - [latlng, ll(point)], - [latlng, ll(coordinates)], - [latlng, ll(str)], + [latlon, ll(latlon)], + [latlon, ll(point)], + [latlon, ll(coordinates)], + [latlon, ll(str)], // convert to type, normalizes to `latlng` first in each function - [ll.toCoordinates(latlng), coordinates], - [ll.toPoint(latlng), point], - [ll.toString(latlng), str], - + [ll.toCoordinates(latlon), coordinates], + [ll.toPoint(latlon), point], + [ll.toString(latlon), str], // if the type is known, use the specific convert function directly - [latlng, ll.fromLatlng(latlng)], - [latlng, ll.fromCoordinates(coordinates)], - [latlng, ll.fromPoint(point)], - [latlng, ll.fromString(str)] + [latlon, ll.fromLatlng(latlon)], + [latlon, ll.fromCoordinates(coordinates)], + [latlon, ll.fromPoint(point)], + [latlon, ll.fromString(str)] ] pairs.forEach(pair => assert.deepEqual(pair[0], pair[1])) diff --git a/index.js b/index.js index c32c47b..b916b27 100644 --- a/index.js +++ b/index.js @@ -1,66 +1,67 @@ module.exports = normalize module.exports.fromCoordinates = module.exports.fromGeoJSON = fromCoordinates -module.exports.fromLatlng = module.exports.fromLeaflet = fromLatlng +module.exports.fromLatlng = module.exports.fromLatlon = module.exports.fromLeaflet = fromLatlon module.exports.fromPoint = fromPoint module.exports.fromString = fromString module.exports.print = function print (input, fixed) { var ll = normalize(input) - return ll.lng.toFixed(fixed || 5) + ', ' + ll.lat.toFixed(fixed || 5) + return ll.lon.toFixed(fixed || 5) + ', ' + ll.lat.toFixed(fixed || 5) } -module.exports.isEqual = function (latlng1, latlng2, epsilon) { - latlng1 = normalize(latlng1) - latlng2 = normalize(latlng2) +module.exports.isEqual = function (latlon1, latlon2, epsilon) { + latlon1 = normalize(latlon1) + latlon2 = normalize(latlon2) epsilon = epsilon || 0 - return (Math.abs(latlng1.lat - latlng2.lat) <= epsilon) && (Math.abs(latlng1.lng - latlng2.lng) <= epsilon) + return (Math.abs(latlon1.lat - latlon2.lat) <= epsilon) && (Math.abs(latlon1.lon - latlon2.lon) <= epsilon) } module.exports.toCoordinates = module.exports.toGeoJSON = function toCoordinates (input) { var ll = normalize(input) - return [ll.lng, ll.lat] + return [ll.lon, ll.lat] } -module.exports.toLatlng = function toLatlng (input) { +module.exports.tolatlon = function tolatlon (input) { return normalize(input) } module.exports.toPoint = function toPoint (input) { var ll = normalize(input) - return {x: ll.lng, y: ll.lat} + return {x: ll.lon, y: ll.lat} } module.exports.toString = function toString (input) { var ll = normalize(input) - return ll.lng + ',' + ll.lat + return ll.lon + ',' + ll.lat } module.exports.toLeaflet = function toLeaflet (input) { if (!window.L) throw new Error('Leaflet not found.') var ll = normalize(input) - return new window.L.Latlng(ll.lat, ll.lng) + return window.L.latLng(ll.lat, ll.lon) } function fromCoordinates (coordinates) { - return floatize({lng: coordinates[0], lat: coordinates[1]}) + return floatize({lon: coordinates[0], lat: coordinates[1]}) } -function fromLatlng (latlng) { - return floatize(latlng) +function fromLatlon (latlon) { + return floatize(latlon) } function fromPoint (point) { - return floatize({lng: point.x, lat: point.y}) + return floatize({lon: point.x, lat: point.y}) } function fromString (str) { const arr = str.split(',') - return floatize({lng: arr[0], lat: arr[1]}) + return floatize({lon: arr[0], lat: arr[1]}) } -function floatize (latlng) { - return {lng: parseFloat(latlng.lng || latlng.lon || latlng.longitude), lat: parseFloat(latlng.lat || latlng.latitude)} +function floatize (latlon) { + const lon = parseFloat(latlon.lon || latlon.lng || latlon.longitude) + return {lng: lon, lon: lon, lat: parseFloat(latlon.lat || latlon.latitude)} } function normalize (unknown) { @@ -70,4 +71,3 @@ function normalize (unknown) { else if (unknown.x && unknown.y) return fromPoint(unknown) return floatize(unknown) } - diff --git a/test.js b/test.js index 5911005..3df75ae 100644 --- a/test.js +++ b/test.js @@ -2,29 +2,29 @@ const assert = require('assert') const ll = require('./') const lat = 38.13234 -const lng = 70.01232 -const latlng = {lat, lng} -const point = {x: lng, y: lat} -const coordinates = [lng, lat] -const str = `${lng},${lat}` +const lon = 70.01232 +const latlon = {lat, lon, lng: lon} +const point = {x: lon, y: lat} +const coordinates = [lon, lat] +const str = `${lon},${lat}` const pairs = [ // normalization - [latlng, ll(latlng)], - [latlng, ll(point)], - [latlng, ll(coordinates)], - [latlng, ll(str)], + [latlon, ll(latlon)], + [latlon, ll(point)], + [latlon, ll(coordinates)], + [latlon, ll(str)], // convert to type, normalizes to `latlng` first in each function - [ll.toCoordinates(latlng), coordinates], - [ll.toPoint(latlng), point], - [ll.toString(latlng), str], + [ll.toCoordinates(latlon), coordinates], + [ll.toPoint(latlon), point], + [ll.toString(latlon), str], // if the type is known, use the specific convert function directly - [latlng, ll.fromLatlng(latlng)], - [latlng, ll.fromCoordinates(coordinates)], - [latlng, ll.fromPoint(point)], - [latlng, ll.fromString(str)] + [latlon, ll.fromLatlng(latlon)], + [latlon, ll.fromCoordinates(coordinates)], + [latlon, ll.fromPoint(point)], + [latlon, ll.fromString(str)] ] pairs.forEach((pair) => assert.deepEqual(pair[0], pair[1]))