Pluscode npm package plus a utility function to test if a string may be a pluscode
This commit is contained in:
1 parent
519d671761
commit
b547e203a6
4 files changed
+61
No files matched your search
@@ -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;
|
||||
Reference in new issue
Block a user