From 68ebdbdbf6d52dbc545fcfc7b6c0ecf19fa9748e Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 14 Nov 2022 11:39:37 +0100 Subject: [PATCH] Using plainnumbers instead of BigInts for GPXids. --- src/components/map/LayerStack.test.tsx | 230 +------------------------ src/db/gpx.test.ts | 6 +- src/db/gpx.ts | 4 +- src/db/lib.test.ts | 116 ++++++------- src/db/rte.test.ts | 10 +- src/db/rtept.test.ts | 10 +- src/db/trk.test.ts | 10 +- src/db/trkpt.test.ts | 10 +- src/db/trkseg.test.ts | 10 +- src/db/wpt.test.ts | 10 +- src/lib/ids.test.ts | 16 +- src/lib/ids.ts | 7 +- src/lib/types.d.ts | 14 +- 13 files changed, 120 insertions(+), 333 deletions(-) diff --git a/src/components/map/LayerStack.test.tsx b/src/components/map/LayerStack.test.tsx index bee6cae..06b6a30 100644 --- a/src/components/map/LayerStack.test.tsx +++ b/src/components/map/LayerStack.test.tsx @@ -20,8 +20,8 @@ describe('The LayerStack component', () => { expect(svg).toMatchInlineSnapshot(` { /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + /> `); @@ -168,8 +59,8 @@ describe('The LayerStack component', () => { expect(svg).toMatchInlineSnapshot(` { /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + /> `); diff --git a/src/db/gpx.test.ts b/src/db/gpx.test.ts index f2c3bab..0d72c1c 100644 --- a/src/db/gpx.test.ts +++ b/src/db/gpx.test.ts @@ -17,9 +17,9 @@ describe('The gpx module', () => { globalThis.Date.now = originalDateNow; }); test('db.put() a new Gpx when required', async () => { - await putNewGpx({ gpx: 0n }); + await putNewGpx({ gpx: 0 }); expect(globalThis.db.put).toBeCalledWith({ - _id: 'gpx/00000000000000000', + _id: 'gpx/0000000000000000', _rev: undefined, doc: { $: { @@ -57,6 +57,6 @@ describe('The gpx module', () => { }); test('db.put() generates an id if needed', async () => { const id = await putNewGpx(); - expect(id).toEqual({ gpx: 8640000000000000n }); + expect(id).toEqual({ gpx: 4320000000000000 }); }); }); diff --git a/src/db/gpx.ts b/src/db/gpx.ts index d44a0c9..5bc7a17 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -1,4 +1,4 @@ -import getUri from '../lib/ids'; +import getUri, { intToGpxId } from '../lib/ids'; import { put } from './lib'; const emptyGpx: Gpx = { @@ -32,7 +32,7 @@ const emptyGpx: Gpx = { }; export const putNewGpx = async ( - id: IdGpx = { gpx: BigInt(Date.now()) + 8640000000000000n } + id: IdGpx = { gpx: intToGpxId(Date.now()) } ) => { const uri = getUri('gpx', id); await put( diff --git a/src/db/lib.test.ts b/src/db/lib.test.ts index 6e24459..d74af6f 100644 --- a/src/db/lib.test.ts +++ b/src/db/lib.test.ts @@ -32,17 +32,17 @@ Object { "offset": 0, "rows": Array [ Object { - "id": "gpx/08640000000000000", - "key": "gpx/08640000000000000", + "id": "gpx/4320000000000000", + "key": "gpx/4320000000000000", "value": Object { - "rev": "1-e8125b3d924c831968288bf8786e8651", + "rev": "1-49baa096ec0c89962f2cafd3ff50b80b", }, }, Object { - "id": "gpx/08640000000000000/2trk/000000", - "key": "gpx/08640000000000000/2trk/000000", + "id": "gpx/4320000000000000/2trk/000000", + "key": "gpx/4320000000000000/2trk/000000", "value": Object { - "rev": "1-a4d5a6dfa66766fd3e42c6ee328436f7", + "rev": "1-46d2f0dcad2299e6ba2830cb1e10b234", }, }, ], @@ -61,8 +61,8 @@ Object { "rows": Array [ Object { "doc": Object { - "_id": "gpx/08640000000000000", - "_rev": "1-e8125b3d924c831968288bf8786e8651", + "_id": "gpx/4320000000000000", + "_rev": "1-49baa096ec0c89962f2cafd3ff50b80b", "doc": Object { "$": Object { "creator": "dyomedea version 0.000002", @@ -81,25 +81,25 @@ Object { }, "type": "gpx", }, - "id": "gpx/08640000000000000", - "key": "gpx/08640000000000000", + "id": "gpx/4320000000000000", + "key": "gpx/4320000000000000", "value": Object { - "rev": "1-e8125b3d924c831968288bf8786e8651", + "rev": "1-49baa096ec0c89962f2cafd3ff50b80b", }, }, Object { "doc": Object { - "_id": "gpx/08640000000000000/2trk/000000", - "_rev": "1-a4d5a6dfa66766fd3e42c6ee328436f7", + "_id": "gpx/4320000000000000/2trk/000000", + "_rev": "1-46d2f0dcad2299e6ba2830cb1e10b234", "doc": Object { "number": 0, }, "type": "trk", }, - "id": "gpx/08640000000000000/2trk/000000", - "key": "gpx/08640000000000000/2trk/000000", + "id": "gpx/4320000000000000/2trk/000000", + "key": "gpx/4320000000000000/2trk/000000", "value": Object { - "rev": "1-a4d5a6dfa66766fd3e42c6ee328436f7", + "rev": "1-46d2f0dcad2299e6ba2830cb1e10b234", }, }, ], @@ -115,24 +115,24 @@ Object { "offset": 0, "rows": Array [ Object { - "id": "gpx/08640000000000000", - "key": "gpx/08640000000000000", + "id": "gpx/4320000000000000", + "key": "gpx/4320000000000000", "value": Object { - "rev": "1-e8125b3d924c831968288bf8786e8651", + "rev": "1-49baa096ec0c89962f2cafd3ff50b80b", }, }, Object { - "id": "gpx/08640000000000000/2trk/000000", - "key": "gpx/08640000000000000/2trk/000000", + "id": "gpx/4320000000000000/2trk/000000", + "key": "gpx/4320000000000000/2trk/000000", "value": Object { - "rev": "1-a4d5a6dfa66766fd3e42c6ee328436f7", + "rev": "1-46d2f0dcad2299e6ba2830cb1e10b234", }, }, Object { - "id": "gpx/08640000000000000/2trk/000000/000000", - "key": "gpx/08640000000000000/2trk/000000/000000", + "id": "gpx/4320000000000000/2trk/000000/000000", + "key": "gpx/4320000000000000/2trk/000000/000000", "value": Object { - "rev": "1-9d45ff67006abf9bd493c0e38e3b9d2d", + "rev": "1-9ecf6e716dbbb26576a0b13a062b8f5f", }, }, ], @@ -148,31 +148,31 @@ Object { "offset": 0, "rows": Array [ Object { - "id": "gpx/08640000000000000", - "key": "gpx/08640000000000000", + "id": "gpx/4320000000000000", + "key": "gpx/4320000000000000", "value": Object { - "rev": "1-e8125b3d924c831968288bf8786e8651", + "rev": "1-49baa096ec0c89962f2cafd3ff50b80b", }, }, Object { - "id": "gpx/08640000000000000/2trk/000000", - "key": "gpx/08640000000000000/2trk/000000", + "id": "gpx/4320000000000000/2trk/000000", + "key": "gpx/4320000000000000/2trk/000000", "value": Object { - "rev": "1-a4d5a6dfa66766fd3e42c6ee328436f7", + "rev": "1-46d2f0dcad2299e6ba2830cb1e10b234", }, }, Object { - "id": "gpx/08640000000000000/2trk/000000/000000", - "key": "gpx/08640000000000000/2trk/000000/000000", + "id": "gpx/4320000000000000/2trk/000000/000000", + "key": "gpx/4320000000000000/2trk/000000/000000", "value": Object { - "rev": "1-9d45ff67006abf9bd493c0e38e3b9d2d", + "rev": "1-9ecf6e716dbbb26576a0b13a062b8f5f", }, }, Object { - "id": "gpx/08640000000000000/2trk/000000/000000/000000", - "key": "gpx/08640000000000000/2trk/000000/000000/000000", + "id": "gpx/4320000000000000/2trk/000000/000000/000000", + "key": "gpx/4320000000000000/2trk/000000/000000/000000", "value": Object { - "rev": "1-8ed7435552a904ab50c836156fdde2b4", + "rev": "1-99c4c054903f577d66ddac0384e04d06", }, }, ], @@ -189,17 +189,17 @@ Object { "offset": 0, "rows": Array [ Object { - "id": "gpx/08640000000000000", - "key": "gpx/08640000000000000", + "id": "gpx/4320000000000000", + "key": "gpx/4320000000000000", "value": Object { - "rev": "1-e8125b3d924c831968288bf8786e8651", + "rev": "1-49baa096ec0c89962f2cafd3ff50b80b", }, }, Object { - "id": "gpx/08640000000000000/0wpt/000000", - "key": "gpx/08640000000000000/0wpt/000000", + "id": "gpx/4320000000000000/0wpt/000000", + "key": "gpx/4320000000000000/0wpt/000000", "value": Object { - "rev": "1-5641f46ee0e0bb75493540be43e6d3b6", + "rev": "1-a3213e8257fb142c09f8b7d7c9482dac", }, }, ], @@ -216,17 +216,17 @@ Object { "offset": 0, "rows": Array [ Object { - "id": "gpx/08640000000000000", - "key": "gpx/08640000000000000", + "id": "gpx/4320000000000000", + "key": "gpx/4320000000000000", "value": Object { - "rev": "1-e8125b3d924c831968288bf8786e8651", + "rev": "1-49baa096ec0c89962f2cafd3ff50b80b", }, }, Object { - "id": "gpx/08640000000000000/1rte/000000", - "key": "gpx/08640000000000000/1rte/000000", + "id": "gpx/4320000000000000/1rte/000000", + "key": "gpx/4320000000000000/1rte/000000", "value": Object { - "rev": "1-1ce2ce3ed4fdc8cd652625325fa4249e", + "rev": "1-a683896687f08ed73ceede02dc0c210e", }, }, ], @@ -243,24 +243,24 @@ Object { "offset": 0, "rows": Array [ Object { - "id": "gpx/08640000000000000", - "key": "gpx/08640000000000000", + "id": "gpx/4320000000000000", + "key": "gpx/4320000000000000", "value": Object { - "rev": "1-e8125b3d924c831968288bf8786e8651", + "rev": "1-49baa096ec0c89962f2cafd3ff50b80b", }, }, Object { - "id": "gpx/08640000000000000/1rte/000000", - "key": "gpx/08640000000000000/1rte/000000", + "id": "gpx/4320000000000000/1rte/000000", + "key": "gpx/4320000000000000/1rte/000000", "value": Object { - "rev": "1-1ce2ce3ed4fdc8cd652625325fa4249e", + "rev": "1-a683896687f08ed73ceede02dc0c210e", }, }, Object { - "id": "gpx/08640000000000000/1rte/000000/000000", - "key": "gpx/08640000000000000/1rte/000000/000000", + "id": "gpx/4320000000000000/1rte/000000/000000", + "key": "gpx/4320000000000000/1rte/000000/000000", "value": Object { - "rev": "1-89cbde37e6c4a85f43e2b68c881183c0", + "rev": "1-e2eff2ff270417d00f681fcc3f7ceafe", }, }, ], diff --git a/src/db/rte.test.ts b/src/db/rte.test.ts index 35bf25b..b124ba1 100644 --- a/src/db/rte.test.ts +++ b/src/db/rte.test.ts @@ -18,9 +18,9 @@ describe('The rte module', () => { globalThis.Date.now = originalDateNow; }); test('db.put() a new rte when required', async () => { - putNewRte({ gpx: 8640000000000000n, rte: 25 }); + putNewRte({ gpx: 4320000000000000, rte: 25 }); await expect(globalThis.db.put).toBeCalledWith({ - _id: 'gpx/08640000000000000/1rte/000025', + _id: 'gpx/4320000000000000/1rte/000025', _rev: undefined, doc: { cmt: undefined, @@ -37,11 +37,11 @@ describe('The rte module', () => { }); }); test('db.put() generates an id for the trk if needed', async () => { - const id = await putNewRte({ gpx: 0n }); - expect(id).toEqual({ gpx: 0n, rte: 0 }); + const id = await putNewRte({ gpx: 0 }); + expect(id).toEqual({ gpx: 0, rte: 0 }); }); test('db.put() generates ids for both gpx and trk if needed', async () => { const id = await putNewRte(); - expect(id).toEqual({ gpx: 8640000000000000n, rte: 0 }); + expect(id).toEqual({ gpx: 4320000000000000, rte: 0 }); }); }); diff --git a/src/db/rtept.test.ts b/src/db/rtept.test.ts index 70dc6f7..0e56afe 100644 --- a/src/db/rtept.test.ts +++ b/src/db/rtept.test.ts @@ -18,9 +18,9 @@ describe('The rtept module', () => { globalThis.Date.now = originalDateNow; }); test('db.put() a new rtept when required', async () => { - putNewRtept({ gpx: 0n, rte: 0, rtept: 0 }); + putNewRtept({ gpx: 0, rte: 0, rtept: 0 }); await expect(globalThis.db.put).toBeCalledWith({ - _id: 'gpx/00000000000000000/1rte/000000/000000', + _id: 'gpx/0000000000000000/1rte/000000/000000', _rev: undefined, doc: { $: { lat: 0, lon: 0 }, @@ -48,11 +48,11 @@ describe('The rtept module', () => { }); }); test('db.put() generates an id for the rtept if needed', async () => { - const id = await putNewRtept({ gpx: 0n }); - expect(id).toEqual({ gpx: 0n, rte: 0, rtept: 0 }); + const id = await putNewRtept({ gpx: 0 }); + expect(id).toEqual({ gpx: 0, rte: 0, rtept: 0 }); }); test('db.put() generates ids for both gpx and rte if needed', async () => { const id = await putNewRtept(); - expect(id).toEqual({ gpx: 8640000000000000n, rte: 0, rtept: 0 }); + expect(id).toEqual({ gpx: 4320000000000000, rte: 0, rtept: 0 }); }); }); diff --git a/src/db/trk.test.ts b/src/db/trk.test.ts index ced7833..4c1b2ba 100644 --- a/src/db/trk.test.ts +++ b/src/db/trk.test.ts @@ -18,9 +18,9 @@ describe('The trk module', () => { globalThis.Date.now = originalDateNow; }); test('db.put() a new trk when required', async () => { - putNewTrk({ gpx: 1n, trk: 2 }); + putNewTrk({ gpx: 1, trk: 2 }); await expect(globalThis.db.put).toBeCalledWith({ - _id: 'gpx/00000000000000001/2trk/000002', + _id: 'gpx/0000000000000001/2trk/000002', _rev: undefined, doc: { cmt: undefined, @@ -37,11 +37,11 @@ describe('The trk module', () => { }); }); test('db.put() generates an id for the trk if needed', async () => { - const id = await putNewTrk({ gpx: 2n }); - expect(id).toEqual({ gpx: 2n, trk: 0}); + const id = await putNewTrk({ gpx: 2 }); + expect(id).toEqual({ gpx: 2, trk: 0}); }); test('db.put() generates ids for both gpx and trk if needed', async () => { const id = await putNewTrk(); - expect(id).toEqual({ gpx: 8640000000000000n, trk: 0}); + expect(id).toEqual({ gpx: 4320000000000000, trk: 0}); }); }); diff --git a/src/db/trkpt.test.ts b/src/db/trkpt.test.ts index a02f0db..aad11f6 100644 --- a/src/db/trkpt.test.ts +++ b/src/db/trkpt.test.ts @@ -19,13 +19,13 @@ describe('The trkpt module', () => { }); test('db.put() a new trkpt when required', async () => { putNewTrkpt({ - gpx: 1n, + gpx: 1, trk: 2, trkseg: 3, trkpt: 4, }); await expect(globalThis.db.put).toBeCalledWith({ - _id: 'gpx/00000000000000001/2trk/000002/000003/000004', + _id: 'gpx/0000000000000001/2trk/000002/000003/000004', _rev: undefined, doc: { $: { lat: 0, lon: 0 }, @@ -59,9 +59,9 @@ describe('The trkpt module', () => { }); }); test('db.put() generates an id for the trk if needed', async () => { - const id = await putNewTrkpt({ gpx: 5n }); + const id = await putNewTrkpt({ gpx: 5 }); expect(id).toEqual({ - gpx: 5n, + gpx: 5, trk: 0, trkseg: 0, trkpt: 0, @@ -70,7 +70,7 @@ describe('The trkpt module', () => { test('db.put() generates ids for both gpx and trk if needed', async () => { const id = await putNewTrkpt(); expect(id).toEqual({ - gpx: 8640000000000000n, + gpx: 4320000000000000, trk: 0, trkseg: 0, trkpt: 0, diff --git a/src/db/trkseg.test.ts b/src/db/trkseg.test.ts index dbd3fc3..a222be9 100644 --- a/src/db/trkseg.test.ts +++ b/src/db/trkseg.test.ts @@ -18,9 +18,9 @@ describe('The trkseg module', () => { globalThis.Date.now = originalDateNow; }); test('db.put() a new trk when required', async () => { - putNewTrkseg({ gpx: 12345678901234567n, trk: 123456, trkseg: 5 }); + putNewTrkseg({ gpx: 1234567890123456, trk: 123456, trkseg: 5 }); await expect(globalThis.db.put).toBeCalledWith({ - _id: 'gpx/12345678901234567/2trk/123456/000005', + _id: 'gpx/1234567890123456/2trk/123456/000005', _rev: undefined, doc: { trkpt: undefined, @@ -30,11 +30,11 @@ describe('The trkseg module', () => { }); }); test('db.put() generates an id for the trk if needed', async () => { - const id = await putNewTrkseg({ gpx: 1n }); - expect(id).toEqual({ gpx: 1n, trk: 0, trkseg: 0 }); + const id = await putNewTrkseg({ gpx: 1 }); + expect(id).toEqual({ gpx: 1, trk: 0, trkseg: 0 }); }); test('db.put() generates ids for both gpx and trk if needed', async () => { const id = await putNewTrkseg(); - expect(id).toEqual({ gpx: 8640000000000000n, trk: 0, trkseg: 0 }); + expect(id).toEqual({ gpx: 4320000000000000, trk: 0, trkseg: 0 }); }); }); diff --git a/src/db/wpt.test.ts b/src/db/wpt.test.ts index b500948..07e90cf 100644 --- a/src/db/wpt.test.ts +++ b/src/db/wpt.test.ts @@ -18,9 +18,9 @@ describe('The wpt module', () => { globalThis.Date.now = originalDateNow; }); test('db.put() a new wpt when required', async () => { - putNewWpt({ gpx: 1n, wpt: 2 }); + putNewWpt({ gpx: 1, wpt: 2 }); await expect(globalThis.db.put).toBeCalledWith({ - _id: 'gpx/00000000000000001/0wpt/000002', + _id: 'gpx/0000000000000001/0wpt/000002', _rev: undefined, doc: { $: { lat: 0, lon: 0 }, @@ -48,11 +48,11 @@ describe('The wpt module', () => { }); }); test('db.put() generates an id for the wpt if needed', async () => { - const id = await putNewWpt({ gpx: 1n }); - expect(id).toEqual({ gpx: 1n, wpt: 0 }); + const id = await putNewWpt({ gpx: 1 }); + expect(id).toEqual({ gpx: 1, wpt: 0 }); }); test('db.put() generates ids for both gpx and trk if needed', async () => { const id = await putNewWpt(); - expect(id).toEqual({ gpx: 8640000000000000n, wpt: 0 }); + expect(id).toEqual({ gpx: 4320000000000000, wpt: 0 }); }); }); diff --git a/src/lib/ids.test.ts b/src/lib/ids.test.ts index 1ea73a7..0ca36d1 100644 --- a/src/lib/ids.test.ts +++ b/src/lib/ids.test.ts @@ -44,9 +44,9 @@ describe('Checking a multilevel route with optional part', () => { describe('Checking gpx ids', () => { const id = { - gpx: 12345678901234567n, + gpx: 1234567890123456, }; - const key = 'gpx/12345678901234567'; + const key = 'gpx/1234567890123456'; test(', vice', () => { const gpx = uri('gpx', id); expect(gpx).toBe(key); @@ -59,10 +59,10 @@ describe('Checking gpx ids', () => { describe('Checking trk ids', () => { const id = { - gpx: 12345678901234567n, + gpx: 1234567890123456, trk: 123456, }; - const key = 'gpx/12345678901234567/2trk/123456'; + const key = 'gpx/1234567890123456/2trk/123456'; test(', vice', () => { const rte = uri('trk', id); expect(rte).toBe(key); @@ -75,11 +75,11 @@ describe('Checking trk ids', () => { describe('Checking trkseg ids', () => { const id = { - gpx: 111n, + gpx: 111, trk: 0, trkseg: 3, }; - const key = 'gpx/00000000000000111/2trk/000000/000003'; + const key = 'gpx/0000000000000111/2trk/000000/000003'; test(', vice', () => { const rte = uri('trkseg', id); expect(rte).toBe(key); @@ -92,12 +92,12 @@ describe('Checking trkseg ids', () => { describe('Checking trkpt ids', () => { const id = { - gpx: 25n, + gpx: 25, trk: 8, trkseg: 0, trkpt: 155, }; - const key = 'gpx/00000000000000025/2trk/000008/000000/000155'; + const key = 'gpx/0000000000000025/2trk/000008/000000/000155'; test(', vice', () => { const rte = uri('trkpt', id); expect(rte).toBe(key); diff --git a/src/lib/ids.ts b/src/lib/ids.ts index 002993f..cb1a730 100644 --- a/src/lib/ids.ts +++ b/src/lib/ids.ts @@ -15,7 +15,7 @@ const bigIntType = (n: number) => { }; const coding = { - gpx: bigIntType(17), + gpx: integerType(16), wpt: integerType(6), rte: integerType(6), rtept: integerType(6), @@ -43,3 +43,8 @@ const uri = (type: RouteKey, param: any) => { }; export default uri; + +const minDate = -8640000000000000; +const halfMinDate = minDate / 2; + +export const intToGpxId = (i: number) => Math.round(i / 2) - halfMinDate; diff --git a/src/lib/types.d.ts b/src/lib/types.d.ts index 8ed64a1..e05ac25 100644 --- a/src/lib/types.d.ts +++ b/src/lib/types.d.ts @@ -1,34 +1,34 @@ interface IdGpx { - gpx: BigInt; + gpx: number; } interface IdTrk { - gpx: BigInt; + gpx: number; trk: number; } interface IdTrkseg { - gpx: BigInt; + gpx: number; trk: number; trkseg: number; } interface IdTrkpt { - gpx: BigInt; + gpx: number; trk: number; trkseg: number; trkpt: number; } interface IdWpt { - gpx: BigInt; + gpx: number; wpt: number; } interface IdRte { - gpx: BigInt; + gpx: number; rte: number; } interface IdRtept { - gpx: BigInt; + gpx: number; rte: number; rtept: number; }