Skip to content

Commit c2056cd

Browse files
authored
Merge branch 'TTLApp:main' into fix/move-js-to-listeners
2 parents 73affdc + fb86b28 commit c2056cd

7 files changed

Lines changed: 188 additions & 294 deletions

File tree

__tests__/__main__/import-export.mjs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,6 @@ describe('Import export', function()
4242
fs.writeFileSync(invalidEntriesFile, invalidEntriesContent, 'utf8');
4343
});
4444

45-
describe('validEntry(entry)', function()
46-
{
47-
const goodEntry = {'type': 'flexible', 'date': '2020-06-03', 'values': ['08:00', '12:00', '13:00', '14:00']};
48-
const goodWaivedEntry = {'type': 'waived', 'date': '2020-06-03', 'data': 'waived', 'hours': '08:00'};
49-
const badEntry = {'type': 'flexible', 'date': '2020-06-03', 'values': ['not-an-hour']};
50-
const badEntry2 = {'type': 'flexible', 'date': '2020-06-03', 'values': 'not-an-array'};
51-
const badWaivedEntry = {'type': 'regular', 'date': '2020-06-03', 'data': 'day-begin', 'hours': 'not-an-hour'};
52-
it('should be valid', () =>
53-
{
54-
assert.strictEqual(ImportExport.validEntry(goodWaivedEntry), true);
55-
assert.strictEqual(ImportExport.validEntry(goodEntry), true);
56-
});
57-
58-
it('should not be valid', () =>
59-
{
60-
assert.strictEqual(ImportExport.validEntry(badWaivedEntry), false);
61-
assert.strictEqual(ImportExport.validEntry(badEntry), false);
62-
assert.strictEqual(ImportExport.validEntry(badEntry2), false);
63-
});
64-
});
65-
6645
describe('exportDatabaseToFile', function()
6746
{
6847
it('Check that export works', () =>

__tests__/__main__/validate-json.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import assert from 'assert';
44

5-
import { validateJSON } from '../../js/validate-json.js';
5+
import { validateJSON } from '../../js/validate-json.mjs';
66

77
describe('Validate json', function()
88
{

js/import-export.mjs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
/*eslint-disable no-prototype-builtins*/
21
'use strict';
32

43
import { assert } from 'console';
54
import Store from 'electron-store';
65
import fs from 'fs';
76

87
import { generateKey } from './date-db-formatter.mjs';
9-
import TimeMath from './time-math.mjs';
8+
import { validateJSON } from './validate-json.mjs';
109

1110
/**
1211
* Returns the database as an array of:
@@ -55,45 +54,8 @@ function _getWaivedEntries()
5554
return output;
5655
}
5756

58-
function _validateDate(dateStr)
59-
{
60-
const date = new Date(dateStr);
61-
return date instanceof Date && !Number.isNaN(date.getTime());
62-
}
63-
6457
class ImportExport
6558
{
66-
static validEntry(entry)
67-
{
68-
if (entry.hasOwnProperty('type') && ['waived', 'flexible'].indexOf(entry.type) !== -1)
69-
{
70-
const validatedDate = entry.hasOwnProperty('date') && _validateDate(entry.date);
71-
let hasExpectedProperties;
72-
let validatedTime = true;
73-
if (entry.type === 'flexible')
74-
{
75-
hasExpectedProperties = entry.hasOwnProperty('values') && Array.isArray(entry.values) && entry.values.length > 0;
76-
if (hasExpectedProperties)
77-
{
78-
for (const value of entry.values)
79-
{
80-
validatedTime &= (TimeMath.validateTime(value) || value === '--:--');
81-
}
82-
}
83-
}
84-
else
85-
{
86-
hasExpectedProperties = entry.hasOwnProperty('data');
87-
validatedTime = entry.hasOwnProperty('hours') && TimeMath.validateTime(entry.hours);
88-
}
89-
if (hasExpectedProperties && validatedDate && validatedTime)
90-
{
91-
return true;
92-
}
93-
}
94-
return false;
95-
}
96-
9759
static exportDatabaseToFile(filename)
9860
{
9961
let information = _getEntries();
@@ -122,7 +84,7 @@ class ImportExport
12284
for (let i = 0; i < information.length; ++i)
12385
{
12486
const entry = information[i];
125-
if (!ImportExport.validEntry(entry))
87+
if (!validateJSON([entry]))
12688
{
12789
failedEntries += 1;
12890
continue;

0 commit comments

Comments
 (0)