From f7db84cb43bfe543504cbaa2ffa7e37e254c86da Mon Sep 17 00:00:00 2001 From: Emre Turan Date: Fri, 19 Jul 2019 18:33:38 +0200 Subject: [PATCH] [@types/unl-core] Add types for unl-core module (#36987) --- types/unl-core/index.d.ts | 141 +++++++++++++++++++++++++++++++ types/unl-core/tsconfig.json | 23 +++++ types/unl-core/tslint.json | 1 + types/unl-core/unl-core-tests.ts | 19 +++++ 4 files changed, 184 insertions(+) create mode 100644 types/unl-core/index.d.ts create mode 100644 types/unl-core/tsconfig.json create mode 100644 types/unl-core/tslint.json create mode 100644 types/unl-core/unl-core-tests.ts diff --git a/types/unl-core/index.d.ts b/types/unl-core/index.d.ts new file mode 100644 index 0000000000..422e2466bb --- /dev/null +++ b/types/unl-core/index.d.ts @@ -0,0 +1,141 @@ +// Type definitions for unl-core 1.0 +// Project: https://github.com/u-n-l/core-js, http://www.movable-type.co.uk/scripts/geohash.html +// Definitions by: UNL Network B.V. +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +export enum Direction { + North = "N", + South = "S", + East = "E", + West = "W" +} + +export enum ElevationType { + floor = "floor", + heightincm = "heightincm" +} + +export interface Neighbours { + n: string; + ne: string; + e: string; + se: string; + s: string; + sw: string; + w: string; + nw: string; +} + +export interface Bounds { + sw: Point; + ne: Point; + elevation: number; + elevationType: ElevationType; +} + +export interface Point { + lat: number; + lon: number; + elevation: number; + elevationType: ElevationType; +} + +export interface EncodeOptions { + elevation: number; + elevationType: ElevationType; +} + +export interface GeohashWithElevation { + elevation: number; + elevationType: ElevationType; + geohash: string; +} + + /** + * Encodes latitude/longitude to geohash, either to specified precision or to automatically + * evaluated precision. + * + * @param lat - Latitude in degrees. + * @param lon - Longitude in degrees. + * @param [precision] - Number of characters in resulting geohash. + * @param [options] - Number of options. Including elevation + * @returns Geohash of supplied latitude/longitude. + * @throws Invalid geohash. + * + * @example + * var geohash = Geohash.encode(52.205, 0.119, 7); // => 'u120fxw' + * var geohash = Geohash.encode(52.205, 0.119, 7, { elevation: 9, elevationType: 'floor'}); // => 'u120fxw@9' + */ + +export function encode( + latitude: number, + longitude: number, + precision?: number, + options?: EncodeOptions +): string; + +/** + * Decode geohash to latitude/longitude and elevation (location is approximate centre of geohash cell, + * to reasonable precision). + * + * @param geohash - Geohash string to be converted to latitude/longitude. + * @returns (Center of and elevation) geohashed location. + * @throws Invalid geohash. + * + * @example + * var latlon = Geohash.decode('u120fxw'); // => { lat: 52.205, lon: 0.1188, elevation:0, elevationType:floor } + * var latlon = Geohash.decode('u120fxw@3'); // => { lat: 52.205, lon: 0.1188, elevation:3, elevationType:floor } + * var latlon = Geohash.decode('u120fxw#87'); // => { lat: 52.205, lon: 0.1188, elevation:87, elevationType:heightincm } + */ +export function decode(geohash: string): Point; + +/** + * Returns SW/NE latitude/longitude bounds of specified geohash. + * + * @param geohash - Cell that bounds are required of. + * @returns Bounds + * @throws Invalid geohash. + */ +export function bounds(geohash: string): Bounds; + +/** + * Determines adjacent cell in given direction. + * + * @param geohash - Cell to which adjacent cell is required. + * @param direction - Direction from geohash (N/S/E/W). + * @returns Geocode of adjacent cell. + * @throws Invalid geohash. + */ +export function adjacent(geohash: string, direction: Direction | string): string; + +/** + * Returns all 8 adjacent cells to specified geohash. + * + * @param geohash - Geohash neighbours are required of. + * @returns The neighbours + * @throws Invalid geohash. + */ +export function neighbours(geohash: string): Neighbours; + +/** + * Returns geohash and elevation properties. + * It is mainly used by internal functions + * + * @param geohashWithElevation - Geohash with elevation chars. + * @returns GeohashWithElevation + * @throws Invalid geohash. + */ +export function excludeElevation(geohashWithElevation: string): GeohashWithElevation; + +/** + * Adds elevation chars and elevation + * It is mainly used by internal functions + * + * @param geohashWithoutElevation - Geohash without elevation chars. + * @param elevation - Height of the elevation. + * @param elevationType - floor | heightincm. + * @returns + * @throws Invalid geohash. + */ +export function appendElevation(geohashWithoutElevation: string, elevation: number, elevationType: ElevationType): string; diff --git a/types/unl-core/tsconfig.json b/types/unl-core/tsconfig.json new file mode 100644 index 0000000000..b5f1073386 --- /dev/null +++ b/types/unl-core/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "unl-core-tests.ts" + ] +} \ No newline at end of file diff --git a/types/unl-core/tslint.json b/types/unl-core/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/unl-core/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/unl-core/unl-core-tests.ts b/types/unl-core/unl-core-tests.ts new file mode 100644 index 0000000000..94bea7a6b1 --- /dev/null +++ b/types/unl-core/unl-core-tests.ts @@ -0,0 +1,19 @@ +import * as Geohash from 'unl-core'; + +// Encoding +const atx_geohash: string = Geohash.encode(30.2672, -97.7431); +const atx_geohash_p3: string = Geohash.encode(30.2672, -97.7431, 3); + +// Decoding +const atx_latlong: Geohash.Point = Geohash.decode(atx_geohash); + +// Bounds +const atx_bounds: Geohash.Bounds = Geohash.bounds(atx_geohash); + +// Adjacent +const atx_adj_cell1: string = Geohash.adjacent(atx_geohash, Geohash.Direction.North); +const atx_adj_cell2: string = Geohash.adjacent(atx_geohash, 'N'); + +// Neighbors +const atx_neighbors: Geohash.Neighbours = Geohash.neighbours(atx_geohash); +const atx_adj_cell3: string = atx_neighbors.n;