Pluscode npm package plus a utility function to test if a string may be a pluscode

This commit is contained in:
Eric van der Vlist 2023-04-02 12:35:53 +02:00
parent 519d671761
commit b547e203a6
4 changed files with 61 additions and 0 deletions

6
package-lock.json generated
View File

@ -35,6 +35,7 @@
"ol": "^7.3.0", "ol": "^7.3.0",
"osmtogeojson": "^3.0.0-beta.5", "osmtogeojson": "^3.0.0-beta.5",
"patch-package": "^6.5.1", "patch-package": "^6.5.1",
"pluscodes": "^2.6.0",
"pouchdb": "^8.0.1", "pouchdb": "^8.0.1",
"pouchdb-adapter-indexeddb": "^8.0.1", "pouchdb-adapter-indexeddb": "^8.0.1",
"pouchdb-browser": "^8.0.1", "pouchdb-browser": "^8.0.1",
@ -7066,6 +7067,11 @@
"node": ">=6" "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": { "node_modules/postcss": {
"version": "8.4.21", "version": "8.4.21",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.21.tgz", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.21.tgz",

View File

@ -58,6 +58,7 @@
"ol": "^7.3.0", "ol": "^7.3.0",
"osmtogeojson": "^3.0.0-beta.5", "osmtogeojson": "^3.0.0-beta.5",
"patch-package": "^6.5.1", "patch-package": "^6.5.1",
"pluscodes": "^2.6.0",
"pouchdb": "^8.0.1", "pouchdb": "^8.0.1",
"pouchdb-adapter-indexeddb": "^8.0.1", "pouchdb-adapter-indexeddb": "^8.0.1",
"pouchdb-browser": "^8.0.1", "pouchdb-browser": "^8.0.1",

View File

@ -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,
});
});
});

View File

@ -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;