Skip to content

Commit

Permalink
update according to nip, remove alpha-2, alpha-3 and numeric from tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Feb 5, 2024
1 parent 0a264de commit 6e00657
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nostr-geotags",
"version": "0.2.2",
"version": "0.3.0",
"description": "Give an object of geodata, returns standardized nostr geotags ",
"type": "module",
"main": "dist/index.js",
Expand Down
73 changes: 37 additions & 36 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ describe('generateTags()', () => {
[ 'g', 'u2' ],
[ 'g', 'u' ],
[ 'G', 'countryCode' ],
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ],
[ 'g', 'HU', 'countryCode' ],
[ 'g', 'HUN', 'countryCode' ],
[ 'g', '348', 'countryCode' ],
[ 'G', 'countryName' ],
[ 'g', 'Hungary', 'countryName' ],
[ 'G', 'regionCode' ],
Expand Down Expand Up @@ -230,9 +230,9 @@ describe('generateTags()', () => {
[ 'g', '47, 19', 'dd' ],
[ 'g', '47', 'lat' ],
[ 'g', '19', 'lon' ],
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ],
[ 'g', 'HU', 'countryCode' ],
[ 'g', 'HUN', 'countryCode' ],
[ 'g', '348', 'countryCode' ],
[ 'g', 'Hungary', 'countryName' ],
[ 'g', 'HU-BU', 'regionCode' ]
])
Expand All @@ -255,9 +255,9 @@ describe('generateTags()', () => {
[ 'G', 'lon' ],
[ 'g', '19', 'lon' ],
[ 'G', 'countryCode' ],
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ],
[ 'g', 'HU', 'countryCode'],
[ 'g', 'HUN', 'countryCode' ],
[ 'g', '348', 'countryCode' ],
[ 'G', 'countryName' ],
[ 'g', 'Hungary', 'countryName' ],
[ 'G', 'regionCode' ],
Expand Down Expand Up @@ -405,9 +405,9 @@ describe('generateTags()', () => {
console.log('iso31661', result)
expect(result).toEqual(expect.arrayContaining([
[ 'G', 'countryCode' ],
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ],
[ 'g', 'HU', 'countryCode'],
[ 'g', 'HUN', 'countryCode' ],
[ 'g', '348', 'countryCode' ],
[ 'G', 'countryName' ],
[ 'g', 'Hungary', 'countryName' ]
]));
Expand Down Expand Up @@ -458,15 +458,16 @@ describe('generateTags()', () => {
console.log('iso31663', result)
expect(result).toEqual(expect.arrayContaining([
[ 'G', 'countryCode' ],
[ 'g', 'AI', 'countryCode', 'alpha-2' ],
[ 'g', 'AIA', 'countryCode', 'alpha-3' ],
[ 'g', '660', 'countryCode', 'numeric' ],
[ 'g', 'AI', 'countryCode'],
[ 'g', 'AIA', 'countryCode' ],
[ 'g', '660', 'countryCode' ],
[ 'G', 'countryName' ],
[ 'g', 'Anguilla', 'countryName' ],
[ 'G', 'countryCode' ],
[ 'g', 'DJ', 'countryCode', 'alpha-2' ]
]));
});
[ 'g', 'DJ', 'countryCode' ]
]
));
})

it('should handle ISO-3166-3 correctly with pessimistic input', () => {
const input: InputData = {
Expand All @@ -485,9 +486,9 @@ describe('generateTags()', () => {
console.log('iso-3166-3 no changes:', result)
expect(result).toEqual(expect.arrayContaining([
['G', 'countryCode'],
[ 'g', 'DE', 'countryCode', 'alpha-2' ],
[ 'g', 'DEU', 'countryCode', 'alpha-3' ],
[ 'g', '276', 'countryCode', 'numeric' ],
[ 'g', 'DE', 'countryCode' ],
[ 'g', 'DEU', 'countryCode' ],
[ 'g', '276', 'countryCode' ],
]))
});

Expand Down Expand Up @@ -535,9 +536,9 @@ describe('generateTags()', () => {
console.log('country and countryCode are false', result)
expect(result).toEqual(expect.not.arrayContaining([
['G', 'countryCode'],
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ],
[ 'g', 'HU', 'countryCode' ],
[ 'g', 'HUN', 'countryCode' ],
[ 'g', '348', 'countryCode' ],
[ 'G', 'countryName' ],
[ 'g', 'Hungary', 'countryName' ]
]))
Expand Down Expand Up @@ -624,9 +625,9 @@ describe('namespace inflection', () => {
console.log('isoAsNamespace is false', result)
expect(result).toEqual(expect.arrayContaining([
[ 'G', 'countryCode' ],
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ],
[ 'g', 'HU', 'countryCode' ],
[ 'g', 'HUN', 'countryCode' ],
[ 'g', '348', 'countryCode'],
[ 'G', 'countryName' ],
[ 'g', 'Hungary', 'countryName' ]
]));
Expand Down Expand Up @@ -702,10 +703,10 @@ describe('sortTagsByKey()', () => {

it('should sort tags', () => {
const tags: GeoTags[] = [
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HU', 'countryCode'],
[ 'G', 'countryCode' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ]
[ 'g', 'HUN', 'countryCode' ],
[ 'g', '348', 'countryCode' ]
];

console.log('before sort', tags)
Expand All @@ -714,19 +715,19 @@ describe('sortTagsByKey()', () => {
console.log('after sort', result)
expect(result).toEqual([
[ 'G', 'countryCode' ],
[ 'g', 'HU', 'countryCode', 'alpha-2' ],
[ 'g', 'HUN', 'countryCode', 'alpha-3' ],
[ 'g', '348', 'countryCode', 'numeric' ]
[ 'g', 'HU', 'countryCode' ],
[ 'g', 'HUN', 'countryCode'],
[ 'g', '348', 'countryCode' ]
]);
});
})

describe('filterOutType()', () => {
it('should filter out tags by type', () => {
const tags: GeoTags[] = [
['g', 'HU', 'countryCode', 'alpha-2'],
['g', 'HUN', 'countryCode', 'alpha-3'],
['g', '348', 'countryCode', 'numeric'],
['g', 'HU', 'countryCode'],
['g', 'HUN', 'countryCode'],
['g', '348', 'countryCode'],
['g', 'HU-BU', 'regionCode']
];

Expand Down
15 changes: 4 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ const generateTags = (input: InputData, opts: Options): GeoTags[] => {
const namespace = iso31661Namespace(opts)
const iso31661Tags: LabelTag[] = [];
if (countryData) {
iso31661Tags.push(['g', countryData.alpha2, namespace, 'alpha-2']);
iso31661Tags.push(['g', countryData.alpha3, namespace, 'alpha-3']);
iso31661Tags.push(['g', countryData.numeric, namespace, 'numeric']);
iso31661Tags.push(['g', countryData.alpha2, namespace]);
iso31661Tags.push(['g', countryData.alpha3, namespace]);
iso31661Tags.push(['g', countryData.numeric, namespace]);
if(countryData.name) {
iso31661Tags.push(['G', 'countryName']);
iso31661Tags.push(['g', countryData.name, 'countryName']);
Expand Down Expand Up @@ -227,18 +227,11 @@ const generateTags = (input: InputData, opts: Options): GeoTags[] => {

const originalValue = countryData[type as keyof ISO31661Entry];
const updatedValues = getUpdatedIso31663Values(type, originalValue);

const typeMap = {
alpha2: 'alpha-2',
alpha3: 'alpha-3',
numeric: 'numeric',
name: 'name'
}

// Add updated values if they are different from the original
updatedValues.forEach(updatedValue => {
if ( (originalValue !== updatedValue && type !== 'name'))
iso31663Tags.push(['g', updatedValue, namespace, typeMap[type] ]);
iso31663Tags.push(['g', updatedValue, namespace]);
});
});

Expand Down

0 comments on commit 6e00657

Please sign in to comment.