Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/uupaa/GeoHex

GeoHex v3.x with TypeScript
https://github.com/uupaa/GeoHex

Last synced: 5 days ago
JSON representation

GeoHex v3.x with TypeScript

Awesome Lists containing this project

README

        

# GeoHex v3.2 with TypeScript

[GeoHex](http://www.geohex.org/) v3.2 with TypeScript implementation.

# PREPARE

```sh
$ npm i -S @uupaa/geohex
```

# import

```ts
// Pattern 1 (recommended): import complete relative path without `--moduleResolution` and `--baseUrl` compiler options.
// For import url, specify the complete relative path.
// This works with node.js and browser. Please consider bundled into one file.
import { GeoHex } from "../node_modules/@uupaa/geohex/lib/GeoHex.js";

/*
{
"compilerOptions": {
"module": "ESNext",
//"moduleResolution": "node",
//"baseUrl": "./",
}
}
*/
```

```ts
// Pattern 2: import package name with `--moduleResolution` and `--baseUrl` compiler options.
// For import url, specify the short package name.
// This works in node.js, but does not work if you import directly from the browser.
import { GeoHex } from "@uupaa/geohex";

/*
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"baseUrl": "./",
}
}
*/
```

# Build and Bundle modules

The `npm run build` command, build to `lib/GeoHex.js` file.

`npm run bundle:all` command, bundle to `lib/GeoHex.esm.js`, `lib/GeoHex.es5.js` and `lib/GeoHex.cjs.js` files.

Other commands.

| commands | input file | output file(s) |
|----------------------|---------------|-------------|
| `npm run build` | `ts/` | `lib/GeoHex.js`
`lib/GeoHex.d.ts` |
| `npm run bundle` | `lib/GeoHex.js` | `lib/GeoHex.esm.js` |
| `npm run bundle:esm` | `lib/GeoHex.js` | `lib/GeoHex.esm.js` |
| `npm run bundle:es5` | `lib/GeoHex.js` | `lib/GeoHex.es5.js` |
| `npm run bundle:cjs` | `lib/GeoHex.js` | `lib/GeoHex.cjs.js` |
| `npm run bundle:all` | `lib/GeoHex.js` | `lib/GeoHex.esm.js`
`lib/GeoHex.es5.js`
`lib/GeoHex.cjs.js` |
| `npm run watch` | | |
| `npm run test` | | |
| `npm run test:compat` | | |

# Browser and runtime support

| Browser | ``<br/>`import` | `<script>` | `require()` |
|---------------------------|----------|----------------|---------------|
| Chrome | 61+ | :o: | |
| Chrome (Android) | 61+ | :o: | |
| Safari | 10.1+ | :o: | |
| Safari (iOS) | 10.3+ | :o: | |
| Firefox | 60+ | :o: | |
| Edge | 16+ | :o: | |
| new Edge (Chromium based) | 76+ | :o: | |
| IE | :x: | :o: | |
| Electron(render) | :o: | :o: | |
| Electron(main) | | | :o: |
| Node.js | | | :o: |

# USAGE

Use `import` and `<script type="module">` style.

```html
// test/esm.html
<!DOCTYPE html><html><head>
<title>GeoHex ver 3.2 browser test</title>
</head>
<body>
<script type="module">
import { GeoHex } from "./node_modules/@uupaa/geohex/lib/GeoHex.esm.js";
console.log(GeoHex.version);

const lat = 35.780516755235475;
const lng = 139.57031250000003;
const lv = 9;

// --- encode lat,lng to GeoHexCode ---
const zone1 = GeoHex.getZoneByLocation(lat, lng, lv);
console.log(zone1.toJSON()); // { lat: 35.78044332128247, lng: 139.57018747142203, lon, x: 101375, y: -35983, code: XM566370240, level: 9 }

// --- decode GeoHexCode to GeoHexZone ---
const zone2 = GeoHex.getZoneByCode(zone1.code);

if (zone1.equals(zone2)) {
console.log(`matched`);
}