Skip to content

Commit 5e0b8aa

Browse files
committed
feat(strings): add explicit string methods
- fromString, fromLonFirstString - fromLatFirstString - toString, toLonFirstString - toLatFirstString
1 parent 694265e commit 5e0b8aa

File tree

3 files changed

+116
-22
lines changed

3 files changed

+116
-22
lines changed

README.md

+52-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ No one has agreed on a standard way of representing lon/lat. This is a small nor
2828
- [fromLatlng](#fromlatlng)
2929
- [fromPoint](#frompoint)
3030
- [fromString](#fromstring)
31+
- [fromLatFirstString](#fromlatfirststring)
3132
- [isEqual](#isequal)
3233
- [print](#print)
3334
- [toCoordinates](#tocoordinates)
3435
- [toLeaflet](#toleaflet)
3536
- [toPoint](#topoint)
3637
- [toString](#tostring)
38+
- [toLatFirstString](#tolatfirststring)
3739
- [lonlat.types.output](#lonlattypesoutput)
3840

3941
### lonlat.types.input
@@ -177,20 +179,41 @@ Returns **[lonlat.types.output](#lonlattypesoutput)**
177179

178180
#### fromString
179181

180-
Tries to parse from a string.
182+
<b>aliases:</b> fromLonFirstString<br>
183+
184+
Tries to parse from a string where the longitude appears before the latitude.
181185

182186
**Parameters**
183187

184188
- `str` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format: `longitude,latitude`
185-
- `latIsFirst` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** Whether or not the first value is latitude. (optional, default `false`)
186189

187190
**Examples**
188191

189192
```javascript
190193
var lonlat = require('@conveyal/lonlat')
191194

192-
var position = lonlat.fromString('12,34') // { lon: 12, lat: 34 }
193-
var position = lonlat.fromString('12,34', true) // { lon: 34, lat: 12 }
195+
var position = lonlat.fromString('12,34') // { lon: 12, lat: 34 }
196+
var position = lonlat.fromLonFirstString('12,34') // { lon: 12, lat: 34 }
197+
```
198+
199+
- Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)**
200+
201+
Returns **[lonlat.types.output](#lonlattypesoutput)**
202+
203+
#### fromLatFirstString
204+
205+
Tries to parse from a string where the latitude appears before the longitude.
206+
207+
**Parameters**
208+
209+
- `str` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format: `latitude,longitude`
210+
211+
**Examples**
212+
213+
```javascript
214+
var lonlat = require('@conveyal/lonlat')
215+
216+
var position = lonlat.fromLatFirstString('12,34) // { lon: 34, lat: 12 }
194217
```
195218
196219
- Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)**
@@ -304,7 +327,30 @@ Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
304327
305328
#### toString
306329
307-
Translates to coordinate string.
330+
<b>aliases:</b> toLonFirstString<br>
331+
332+
Translates to coordinate string where the longitude appears before latitude.
333+
334+
**Parameters**
335+
336+
- `input` **[lonlat.types.input](#lonlattypesinput)**
337+
338+
**Examples**
339+
340+
```javascript
341+
var lonlat = require('@conveyal/lonlat')
342+
343+
var str = lonlat.toString({ lat: 12, longitude: 34 }) // '34,12'
344+
var str = lonlat.toLonFirstString({ lat: 12, longitude: 34 }) // '34,12'
345+
```
346+
347+
- Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)**
348+
349+
Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format 'longitude,latitude'
350+
351+
#### toLatFirstString
352+
353+
Translates to coordinate string where the latitude appears before longitude.
308354
309355
**Parameters**
310356
@@ -315,7 +361,7 @@ Translates to coordinate string.
315361
```javascript
316362
var lonlat = require('@conveyal/lonlat')
317363
318-
var str = lonlat.toString({ lat: 12, longitude: 34 }) // '34,12'
364+
var str = lonlat.toLatFirstString({ lat: 12, longitude: 34 }) // '12,34'
319365
```
320366
321367
- Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)**

index.js

+49-13
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,43 @@ function fromPoint (point) {
126126
module.exports.fromPoint = fromPoint
127127

128128
/**
129-
* Tries to parse from a string.
129+
* <b>aliases:</b> fromLonFirstString<br>
130+
*
131+
* Tries to parse from a string where the longitude appears before the latitude.
132+
*
133+
* @memberof conveyal/lonlat
134+
* @param {string} str A string in the format: `longitude,latitude`
135+
* @return {lonlat.types.output}
136+
* @throws {lonlat.types.InvalidCoordinateException}
137+
* @example
138+
* var lonlat = require('@conveyal/lonlat')
139+
140+
var position = lonlat.fromString('12,34') // { lon: 12, lat: 34 }
141+
var position = lonlat.fromLonFirstString('12,34') // { lon: 12, lat: 34 }
142+
*/
143+
function fromString (str) {
144+
var arr = str.split(',')
145+
return floatize({lon: arr[0], lat: arr[1]})
146+
}
147+
module.exports.fromString = module.exports.fromLonFirstString = fromString
148+
149+
/**
150+
* Tries to parse from a string where the latitude appears before the longitude.
130151
*
131152
* @memberof conveyal/lonlat
132-
* @param {string} str A string in the format: `longitude,latitude`
133-
* @param {boolean} [latIsFirst=false] Whether or not the first value is latitude.
153+
* @param {string} str A string in the format: `latitude,longitude`
134154
* @return {lonlat.types.output}
135155
* @throws {lonlat.types.InvalidCoordinateException}
136156
* @example
137157
* var lonlat = require('@conveyal/lonlat')
138158
139-
var position = lonlat.fromString('12,34') // { lon: 12, lat: 34 }
140-
var position = lonlat.fromString('12,34', true) // { lon: 34, lat: 12 }
159+
var position = lonlat.fromLatFirstString('12,34) // { lon: 34, lat: 12 }
141160
*/
142-
function fromString (str, latIsFirst) {
161+
function fromLatFirstString (str) {
143162
var arr = str.split(',')
144-
return latIsFirst
145-
? floatize({lat: arr[0], lon: arr[1]})
146-
: floatize({lon: arr[0], lat: arr[1]})
163+
return floatize({lat: arr[0], lon: arr[1]})
147164
}
148-
module.exports.fromString = fromString
165+
module.exports.fromLatFirstString = fromLatFirstString
149166

150167
/**
151168
* Determine if two inputs are equal to each other
@@ -236,21 +253,40 @@ module.exports.toPoint = function toPoint (input) {
236253
}
237254

238255
/**
239-
* Translates to coordinate string.
256+
* <b>aliases:</b> toLonFirstString<br>
257+
*
258+
* Translates to coordinate string where the longitude appears before latitude.
240259
*
241260
* @param {lonlat.types.input} input
242261
* @return {string} A string in the format 'longitude,latitude'
243262
* @throws {lonlat.types.InvalidCoordinateException}
244263
* @example
245264
* var lonlat = require('@conveyal/lonlat')
246265
247-
var str = lonlat.toString({ lat: 12, longitude: 34 }) // '34,12'
266+
var str = lonlat.toString({ lat: 12, longitude: 34 }) // '34,12'
267+
var str = lonlat.toLonFirstString({ lat: 12, longitude: 34 }) // '34,12'
248268
*/
249-
module.exports.toString = function toString (input) {
269+
module.exports.toString = module.exports.toLonFirstString = function toString (input) {
250270
var ll = normalize(input)
251271
return ll.lon + ',' + ll.lat
252272
}
253273

274+
/**
275+
* Translates to coordinate string where the latitude appears before longitude.
276+
*
277+
* @param {lonlat.types.input} input
278+
* @return {string} A string in the format 'longitude,latitude'
279+
* @throws {lonlat.types.InvalidCoordinateException}
280+
* @example
281+
* var lonlat = require('@conveyal/lonlat')
282+
283+
var str = lonlat.toLatFirstString({ lat: 12, longitude: 34 }) // '12,34'
284+
*/
285+
module.exports.toLatFirstString = function toLatFirstString (input) {
286+
var ll = normalize(input)
287+
return ll.lat + ',' + ll.lon
288+
}
289+
254290
function floatize (lonlat) {
255291
var lon = parseFloatWithAlternates([lonlat.lon, lonlat.lng, lonlat.longitude])
256292
var lat = parseFloatWithAlternates([lonlat.lat, lonlat.latitude])

index.test.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ describe('lonlat', () => {
7272
}, {
7373
expected: str,
7474
method: 'toString'
75+
}, {
76+
expected: str,
77+
method: 'toLonFirstString'
78+
}, {
79+
expected: strLatFirst,
80+
method: 'toLatFirstString'
7581
}]
7682

7783
testCases.forEach((test) => {
@@ -87,15 +93,21 @@ describe('lonlat', () => {
8793
description: 'Object with lng and lat keys'
8894
}, {
8995
calculated: ll.fromCoordinates(coordinates),
90-
description: 'Array of lon and lat'
96+
description: 'Array of lon and lat (fromCoordinates)'
97+
}, {
98+
calculated: ll.fromGeoJSON(coordinates),
99+
description: 'Array of lon and lat (fromGeoJSON)'
91100
}, {
92101
calculated: ll.fromPoint(point),
93102
description: 'Object with x and y keys'
94103
}, {
95104
calculated: ll.fromString(str),
96-
description: 'String with comma separating lon and lat, respectively'
105+
description: 'String with comma separating lon and lat, respectively (fromString)'
106+
}, {
107+
calculated: ll.fromLonFirstString(str),
108+
description: 'String with comma separating lon and lat, respectively (fromLonFirstString)'
97109
}, {
98-
calculated: ll.fromString(strLatFirst, true),
110+
calculated: ll.fromLatFirstString(strLatFirst),
99111
description: 'String with comma separating lat and lon, respectively'
100112
}]
101113

0 commit comments

Comments
 (0)