Pluscode npm package plus a utility function to test if a string may be a pluscode
This commit is contained in:
parent
519d671761
commit
b547e203a6
|
@ -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",
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -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;
|
Loading…
Reference in New Issue