Utility functions for converting between coordinate objects and string keys. This module provides functions to convert a coordinate object with x and y properties into a string key, and to convert a string key back into a coordinate object. The coordinate format is "x,y", where x and y are numbers. The functions are useful for storing and retrieving coordinates in a hash map or similar data structure.
- Source
Returns:
- The string representation of the coordinate.
- Type:
- string
Example
// Example usage:
const coord = { x: 10, y: 20 };
const key = coord2Key(coord); // "10,20"
const newCoord = key2Coord(key); // { x: 10, y: 20 }
console.log(newCoord); // { x: 10, y: 20 }
Methods
(static) coord2Key(coord) → {string}
Converts a coordinate object to a string key.
Parameters:
Name | Type | Description |
---|---|---|
coord | Object | The coordinate object with x and y properties. |
- Source
Returns:
- The string key in the format "x,y".
- Type:
- string
(static) key2Coord(key) → {Object}
Converts a string key back to a coordinate object.
Parameters:
Name | Type | Description |
---|---|---|
key | string | The string key in the format "x,y". |
- Source
Returns:
- The coordinate object with x and y properties.
- Type:
- Object