From b547e203a613b468b908bddfea9997f3c0d3dcb8 Mon Sep 17 00:00:00 2001 From: Eric van der Vlist Date: Sun, 2 Apr 2023 12:35:53 +0200 Subject: [PATCH] Pluscode npm package plus a utility function to test if a string may be a pluscode --- package-lock.json | 6 ++++++ package.json | 1 + src/lib/check-pluscode.test.ts | 38 ++++++++++++++++++++++++++++++++++ src/lib/check-pluscodes.ts | 16 ++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 src/lib/check-pluscode.test.ts create mode 100644 src/lib/check-pluscodes.ts diff --git a/package-lock.json b/package-lock.json index 15c69f0..a1ff88a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,6 +35,7 @@ "ol": "^7.3.0", "osmtogeojson": "^3.0.0-beta.5", "patch-package": "^6.5.1", + "pluscodes": "^2.6.0", "pouchdb": "^8.0.1", "pouchdb-adapter-indexeddb": "^8.0.1", "pouchdb-browser": "^8.0.1", @@ -7066,6 +7067,11 @@ "node": ">=6" } }, + "node_modules/pluscodes": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/pluscodes/-/pluscodes-2.6.0.tgz", + "integrity": "sha512-+3sW+Qt+znuN2uMFFvebo2m5MsaTjBXOzEYvkfx4RMeOYnNCQv3OWeQujfRAo6nzg7D+5vD2b3tihtwW3b5pfg==" + }, "node_modules/postcss": { "version": "8.4.21", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.21.tgz", diff --git a/package.json b/package.json index d3a65cd..4f460a3 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "ol": "^7.3.0", "osmtogeojson": "^3.0.0-beta.5", "patch-package": "^6.5.1", + "pluscodes": "^2.6.0", "pouchdb": "^8.0.1", "pouchdb-adapter-indexeddb": "^8.0.1", "pouchdb-browser": "^8.0.1", diff --git a/src/lib/check-pluscode.test.ts b/src/lib/check-pluscode.test.ts new file mode 100644 index 0000000..d4c0ca1 --- /dev/null +++ b/src/lib/check-pluscode.test.ts @@ -0,0 +1,38 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import maybePlusCode from './check-pluscodes'; + +describe('The maybePlusCode method', () => { + it('detects a valid short pluscode without qualifier', () => { + const code = '9G8F+6W'; + expect(maybePlusCode(code)).toEqual({ code, isShort: true }); + }); + it('detects a valid short pluscode with qualifier', () => { + const code = '948G+V4 Oviedo'; + expect(maybePlusCode(code)).toEqual({ + code: '948G+V4', + qualifier: 'Oviedo', + isShort: true, + }); + }); + it('detects a valid long pluscode with 10 digits', () => { + const code = '9FFW84J9+XG'; + expect(maybePlusCode(code)).toEqual({ + code, + isShort: false, + }); + }); + it('detects a valid long pluscode with 8 digits', () => { + const code = '9FFW84J9+'; + expect(maybePlusCode(code)).toEqual({ + code, + isShort: false, + }); + }); + it('detects a valid long pluscode with 4 digits', () => { + const code = '9FFW0000+'; + expect(maybePlusCode(code)).toEqual({ + code, + isShort: false, + }); + }); +}); diff --git a/src/lib/check-pluscodes.ts b/src/lib/check-pluscodes.ts new file mode 100644 index 0000000..539e359 --- /dev/null +++ b/src/lib/check-pluscodes.ts @@ -0,0 +1,16 @@ +const maybePlusRegexp = + /^([23456789CFGHJMPQRVWX]+0*\+[23456789CFGHJMPQRVWX]*)(\s+(.*))?$/; + +const maybePlusCode = (code: string) => { + const matches = code.match(maybePlusRegexp); + if (!matches) { + return false; + } + return { + code: matches[1], + isShort: matches[1].length < 9, + qualifier: matches[3] || undefined, + }; +}; + +export default maybePlusCode;