@akc42/app-utils 5.0.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -1
- package/app-utils.js +23 -3
- package/colour.js +1 -1
- package/date-utils.js +63 -0
- package/package.json +1 -1
- package/pdf.js +1 -1
package/README.md
CHANGED
|
@@ -111,7 +111,23 @@ Additional support is provided with
|
|
|
111
111
|
|
|
112
112
|
## csv
|
|
113
113
|
|
|
114
|
-
the modules default export is a function which can be called with a name and optionally a parameters object. The name
|
|
114
|
+
the modules default export is a function which can be called with a name and optionally a parameters object. The name
|
|
115
|
+
is formed into url to `/api/csv/<name>` to make a download request and if they exist the parameters object is turned
|
|
116
|
+
into a query string. The response from that uri is downloaded (expected to be a csv file).
|
|
117
|
+
|
|
118
|
+
## Date Utilities
|
|
119
|
+
|
|
120
|
+
There are a few operations on dates and times that happen quite frequently. This module provides 4 functions to handle them, all of which take a single parameter. We generally assume everything is in `localtime`, but there is no specific reference to this, so we just assume the answer is in the same timezone as the input.
|
|
121
|
+
|
|
122
|
+
1. **minToTime** take the number of minutes from midnight and provides the string HH:MM based on the 24 hour clock. The
|
|
123
|
+
number of minutes should be between 0 and 1439, otherwise zero length string is returned.
|
|
124
|
+
2. **timeToMin** takes the time and returns the number of minutes from midnight. An invalid time string causes a -1 to
|
|
125
|
+
be returned.
|
|
126
|
+
3. **strToUrlDate** converts a string formatted as DD/MM/YYYY (leading zeros may be omitted for DD and MM and YYYY must
|
|
127
|
+
be between 1900 and 2199) and returns a string formatted as YYYY-MM-DD. It the date is invalid (including using the
|
|
128
|
+
29 February on a non leap year) a zero length string is returned.
|
|
129
|
+
4. **urlDateToStr** converts a string formatted as YYYY-MM-DD and returns it as DD/MM/YYYY. Leading zeros must not be
|
|
130
|
+
omitted. If the date is invalid (including any month that is too many days) that a zero length string is returned.
|
|
115
131
|
|
|
116
132
|
## debug
|
|
117
133
|
|
package/app-utils.js
CHANGED
|
@@ -11,7 +11,27 @@ import {partMap} from './partMap.js';
|
|
|
11
11
|
import pdf from './pdf.js';
|
|
12
12
|
import Route from './route.js';
|
|
13
13
|
import submit from './submit-function.js';
|
|
14
|
-
import {switchPath, generateUri,
|
|
14
|
+
import {switchPath, generateUri, navigate} from './switch-path.js';
|
|
15
|
+
import { minToTime,timeToMin,strToUrlDate,urlDateToStr } from './date-utils.js';
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
function capitalise(name) {
|
|
18
|
+
if (name.length > 0 ) {
|
|
19
|
+
let words = name.split(' ');
|
|
20
|
+
for(let i = 0; i < words.length; i++) {
|
|
21
|
+
if (i > 0 && ( words[i].toLowerCase() === 'de' || words[i].toLowerCase() === 'la')) {
|
|
22
|
+
words[i] = words[i].toLowerCase();
|
|
23
|
+
} else if (words[i].length > 2 && words[i].toUpperCase().substring(0,2) === `O'` ) {
|
|
24
|
+
const newword = capitalise(words[i].substring(2))
|
|
25
|
+
words[i] = words[i].substring(0,2).toUpperCase() + newword;
|
|
26
|
+
} else {
|
|
27
|
+
words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1).toLowerCase();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return words.join(' ');
|
|
31
|
+
}
|
|
32
|
+
return '';
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export {api,ApiError,AppKeys,calcTextColor,capitalise, config, connectUrl, csv, Debug, disconnectUrl, domHost, generateUri,
|
|
36
|
+
getMasterTabPromise, Logger, minToTime, navigate, partMap, pdf, reReadConfig, Route,setConfig, strToUrlDate, submit,
|
|
37
|
+
switchPath, timeToMin, urlDateToStr };
|
package/colour.js
CHANGED
package/date-utils.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@licence
|
|
3
|
+
Copyright (c) 2026 Alan Chandler, all rights reserved
|
|
4
|
+
|
|
5
|
+
This file is part of @akc42/app-utils.
|
|
6
|
+
|
|
7
|
+
@akc42/app-utils is free software: you can redistribute it and/or modify
|
|
8
|
+
it under the terms of the GNU General Public License as published by
|
|
9
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
@akc42/app-utils is distributed in the hope that it will be useful,
|
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
GNU General Public License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU General Public License
|
|
18
|
+
along with @akc42/app-utils. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
*/
|
|
20
|
+
const timeRegex = /^((?:0?[0-9]|1[0-9]|2[0-3]))(?::((?:0?[0-9]|[1-5][0-9])))?$/;
|
|
21
|
+
export function minToTime(m) {
|
|
22
|
+
if (m === 0) return '00:00';
|
|
23
|
+
if (isNaN(m) || m < 0 || m > 1439) return '';
|
|
24
|
+
|
|
25
|
+
var hr = Math.floor(m/60);
|
|
26
|
+
var mn = Math.floor(m % 60);
|
|
27
|
+
return hr.toString().padStart(2,'0') + ':' + mn.toString().padStart(2,'0');
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function timeToMin(time) {
|
|
31
|
+
if (typeof time !== 'string') return -1; //protective against crashes
|
|
32
|
+
if (time.length === 0) return -1;
|
|
33
|
+
const matches = timeRegex.exec(time);
|
|
34
|
+
if (matches) {
|
|
35
|
+
let mins = 0;
|
|
36
|
+
if (typeof matches[2] !== 'undefined') mins = parseInt(matches[2],10);
|
|
37
|
+
let hours = parseInt(matches[1], 10);
|
|
38
|
+
if (Number.isInteger(hours) && Number.isInteger(mins)) return (hours * 60) + mins;
|
|
39
|
+
}
|
|
40
|
+
return -1;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const urlRegex = /^((?:19|2[01])(?:(?:(?:0[48]|[2468][048]|[13579][26])(?=-02-29))|\d{2}(?!-02-(?:29|3[01]))))-((?:02(?!-3[01])|0[469](?!-31)|11(?!-31)|(?:0[13578]|1[02])))-([0][1-9]|[12][0-9]|3[01])$/;
|
|
44
|
+
const strRegex = /^((?:0?|[1-9])\d)\/((?:0?|[1-9])\d)\/((?:19|2[01])\d{2})$/;
|
|
45
|
+
|
|
46
|
+
export function urlDateToStr(urlDay) {
|
|
47
|
+
let matches;
|
|
48
|
+
//eslint-disable-next-line no-cond-assign
|
|
49
|
+
if (matches = urlRegex.exec(urlDay.toString())) {
|
|
50
|
+
return matches[3] + '/' + matches[2] + '/' + matches[1];
|
|
51
|
+
}
|
|
52
|
+
return '';
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export function strToUrlDate(str) {
|
|
56
|
+
let matches;
|
|
57
|
+
if (matches = strRegex.exec(str.toString())) {
|
|
58
|
+
const urlDate = matches[3] + '-' + matches[2].padStart(2,'0') + '-' + matches[1].padStart(2,'0');
|
|
59
|
+
if (urlRegex.test(urlDate)) return urlDate;
|
|
60
|
+
}
|
|
61
|
+
return '';
|
|
62
|
+
}
|
|
63
|
+
|
package/package.json
CHANGED
package/pdf.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
You should have received a copy of the GNU General Public License
|
|
18
18
|
along with @akc42/app-utils. If not, see <http://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
import api from './
|
|
20
|
+
import api from './api.js';
|
|
21
21
|
export default (url,params) => {
|
|
22
22
|
api(`pdf/${url}`,params,true);
|
|
23
23
|
}
|