Reimplementing GPX imports.
This commit is contained in:
1 parent
a2d0e8cf6a
commit
5fdd63103e
8 files changed
+148
-5
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import { assert } from 'console';
|
||||
import { findStartTime } from './gpx';
|
||||
|
||||
describe('findStartTime', () => {
|
||||
test('to be undefined for a string', () => {
|
||||
const start = findStartTime('');
|
||||
expect(start).toBeUndefined();
|
||||
});
|
||||
test('to be undefined for an object without time key', () => {
|
||||
const start = findStartTime({ foo: 'foo', bar: 'bar' });
|
||||
expect(start).toBeUndefined();
|
||||
});
|
||||
test('to be the time value for an object with a time key', () => {
|
||||
const start = findStartTime({ foo: 'foo', time: 'bar' });
|
||||
expect(start).toEqual('bar');
|
||||
});
|
||||
test('to be the lowest time value for an object with several time keys', () => {
|
||||
const start = findStartTime({
|
||||
foo: { time: 'foo' },
|
||||
time: 'bar',
|
||||
bar: { time: 'a' },
|
||||
});
|
||||
expect(start).toEqual('a');
|
||||
});
|
||||
test('to be the lowest time value for an array with several objects with time keys', () => {
|
||||
const start = findStartTime({
|
||||
foos: [{ time: 'foo' }, { time: '0' }],
|
||||
time: 'bar',
|
||||
bar: { time: 'a' },
|
||||
});
|
||||
expect(start).toEqual('0');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
const min = (s1?: string, s2?: string) => {
|
||||
return s1! < s2! ? s1 : s2;
|
||||
};
|
||||
|
||||
export const findStartTime = (x: any, startTime?: string) => {
|
||||
if (typeof x === 'object') {
|
||||
let newStartTime = startTime;
|
||||
|
||||
for (const key in x) {
|
||||
if (key === 'time') {
|
||||
newStartTime = min(newStartTime, x[key]);
|
||||
} else {
|
||||
newStartTime = findStartTime(x[key], newStartTime);
|
||||
}
|
||||
}
|
||||
return newStartTime;
|
||||
}
|
||||
else return startTime;
|
||||
};
|
||||
Reference in new issue
Block a user