web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

format.ts (3156B)


      1 import type {
      2     GeocoderReverseResult,
      3     GeolocationAddress,
      4     GeolocationLatitudeFmtOption,
      5     GeometryPoint,
      6 } from "./types.js";
      7 import { parse_geocode_address } from "./parse.js";
      8 
      9 export const fmt_geocode_address = (geoc: GeocoderReverseResult): string => {
     10     const addr = parse_geocode_address(geoc);
     11     return addr ? `${addr.primary}, ${addr.admin}, ${addr.country}` : "";
     12 };
     13 
     14 export const fmt_geolocation_address = (addr: GeolocationAddress): string => {
     15     return `${addr.primary}, ${addr.admin}, ${addr.country}`;
     16 };
     17 
     18 export const fmt_geometry_point_coords = (
     19     point: GeometryPoint,
     20     locale: string
     21 ): string => {
     22     const lat = geol_lat_fmt(point.coordinates[1], "dms", locale, 3);
     23     const lng = geol_lng_fmt(point.coordinates[0], "dms", locale, 3);
     24     return `${lat}, ${lng}`;
     25 };
     26 
     27 export const geol_lat_fmt = (
     28     lat: number,
     29     fmt_opt: GeolocationLatitudeFmtOption,
     30     locale: string,
     31     precision: number = 5
     32 ): string => {
     33     const options: Intl.NumberFormatOptions = {
     34         minimumFractionDigits: 2,
     35         maximumFractionDigits: 2,
     36     };
     37     const fmt_deg = new Intl.NumberFormat(locale, { maximumFractionDigits: 0 });
     38     const fmt_min = new Intl.NumberFormat(locale, options);
     39     const fmt_sec = new Intl.NumberFormat(locale, options);
     40     if (fmt_opt === "dms") {
     41         const deg = Math.floor(Math.abs(lat));
     42         const min = Math.floor((Math.abs(lat) - deg) * 60);
     43         const sec = (Math.abs(lat) - deg - min / 60) * 3600;
     44         return `${fmt_deg.format(deg)}° ${fmt_min.format(min)}' ${fmt_sec.format(sec)}" ${lat >= 0 ? "N" : "S"}`;
     45     } else if (fmt_opt === "dm") {
     46         const deg = Math.floor(Math.abs(lat));
     47         const min = (Math.abs(lat) - deg) * 60;
     48         return `${fmt_deg.format(deg)}° ${fmt_min.format(min)}' ${lat >= 0 ? "N" : "S"}`;
     49     } else {
     50         return `${lat.toLocaleString(locale, { maximumFractionDigits: precision })}° ${lat >= 0 ? "N" : "S"}`;
     51     }
     52 };
     53 
     54 export const geol_lng_fmt = (
     55     lng: number,
     56     fmt_opt: GeolocationLatitudeFmtOption,
     57     locale: string,
     58     precision: number = 5
     59 ): string => {
     60     const options: Intl.NumberFormatOptions = {
     61         minimumFractionDigits: 2,
     62         maximumFractionDigits: 2,
     63     };
     64     const fmt_deg = new Intl.NumberFormat(locale, { maximumFractionDigits: 0 });
     65     const fmt_min = new Intl.NumberFormat(locale, options);
     66     const fmt_sec = new Intl.NumberFormat(locale, options);
     67     if (fmt_opt === "dms") {
     68         const degrees = Math.floor(Math.abs(lng));
     69         const minutes = Math.floor((Math.abs(lng) - degrees) * 60);
     70         const seconds = (Math.abs(lng) - degrees - minutes / 60) * 3600;
     71         return `${fmt_deg.format(degrees)}° ${fmt_min.format(minutes)}' ${fmt_sec.format(seconds)}" ${lng >= 0 ? "E" : "W"}`;
     72     } else if (fmt_opt === "dm") {
     73         const degrees = Math.floor(Math.abs(lng));
     74         const minutes = (Math.abs(lng) - degrees) * 60;
     75         return `${fmt_deg.format(degrees)}° ${fmt_min.format(minutes)}' ${lng >= 0 ? "E" : "W"}`;
     76     } else {
     77         return `${lng.toLocaleString(locale, { maximumFractionDigits: precision })}° ${lng >= 0 ? "E" : "W"}`;
     78     }
     79 };