diff options
Diffstat (limited to 'src/helpers')
-rw-r--r-- | src/helpers/asar-helpers.ts | 7 | ||||
-rw-r--r-- | src/helpers/i18n-helpers.ts | 12 | ||||
-rw-r--r-- | src/helpers/password-helpers.ts | 14 | ||||
-rw-r--r-- | src/helpers/recipe-helpers.ts | 16 | ||||
-rw-r--r-- | src/helpers/schedule-helpers.ts | 8 | ||||
-rw-r--r-- | src/helpers/serverless-helpers.ts (renamed from src/helpers/serverless-helpers.js) | 0 | ||||
-rw-r--r-- | src/helpers/service-helpers.js | 16 | ||||
-rw-r--r-- | src/helpers/service-helpers.ts | 21 | ||||
-rw-r--r-- | src/helpers/url-helpers.ts | 2 | ||||
-rw-r--r-- | src/helpers/userAgent-helpers.ts | 2 | ||||
-rw-r--r-- | src/helpers/validation-helpers.js | 67 | ||||
-rw-r--r-- | src/helpers/validation-helpers.ts | 96 |
12 files changed, 149 insertions, 112 deletions
diff --git a/src/helpers/asar-helpers.ts b/src/helpers/asar-helpers.ts index 3d9f0d941..9d975c193 100644 --- a/src/helpers/asar-helpers.ts +++ b/src/helpers/asar-helpers.ts | |||
@@ -1,3 +1,10 @@ | |||
1 | import { join } from 'path'; | ||
2 | |||
1 | export function asarPath(dir: string = '') { | 3 | export function asarPath(dir: string = '') { |
2 | return dir.replace('app.asar', 'app.asar.unpacked'); | 4 | return dir.replace('app.asar', 'app.asar.unpacked'); |
3 | } | 5 | } |
6 | |||
7 | // Replacing app.asar is not beautiful but unfortunately necessary | ||
8 | export function asarRecipesPath(...segments: string[]) { | ||
9 | return join(asarPath(join(__dirname, '..', 'recipes')), ...[segments].flat()); | ||
10 | } | ||
diff --git a/src/helpers/i18n-helpers.ts b/src/helpers/i18n-helpers.ts index c1f18f446..ec7dc8e98 100644 --- a/src/helpers/i18n-helpers.ts +++ b/src/helpers/i18n-helpers.ts | |||
@@ -4,13 +4,11 @@ export function getLocale({ | |||
4 | let localeStr = locale; | 4 | let localeStr = locale; |
5 | if (locales[locale] === undefined) { | 5 | if (locales[locale] === undefined) { |
6 | let localeFuzzy: string | undefined; | 6 | let localeFuzzy: string | undefined; |
7 | Object.keys(locales).forEach((localStr) => { | 7 | for (const localStr of Object.keys(locales)) { |
8 | if (locales && Object.hasOwnProperty.call(locales, localStr)) { | 8 | if (locales && Object.hasOwnProperty.call(locales, localStr) && locale.slice(0, 2) === localStr.slice(0, 2)) { |
9 | if (locale.substring(0, 2) === localStr.substring(0, 2)) { | ||
10 | localeFuzzy = localStr; | 9 | localeFuzzy = localStr; |
11 | } | 10 | } |
12 | } | 11 | } |
13 | }); | ||
14 | 12 | ||
15 | if (localeFuzzy !== undefined) { | 13 | if (localeFuzzy !== undefined) { |
16 | localeStr = localeFuzzy; | 14 | localeStr = localeFuzzy; |
@@ -61,12 +59,12 @@ export function getSelectOptions({ | |||
61 | if (sort) { | 59 | if (sort) { |
62 | keys = keys.sort(Intl.Collator().compare); | 60 | keys = keys.sort(Intl.Collator().compare); |
63 | } | 61 | } |
64 | keys.forEach((key) => { | 62 | for (const key of keys) { |
65 | options.push({ | 63 | options.push({ |
66 | value: key, | 64 | value: key, |
67 | label: locales[key], | 65 | label: locales[key], |
68 | }); | 66 | }); |
69 | }); | 67 | } |
70 | 68 | ||
71 | return options; | 69 | return options; |
72 | } | 70 | } |
diff --git a/src/helpers/password-helpers.ts b/src/helpers/password-helpers.ts index 89c75c752..e5d9a4a25 100644 --- a/src/helpers/password-helpers.ts +++ b/src/helpers/password-helpers.ts | |||
@@ -12,9 +12,9 @@ export function scorePassword(password: string) { | |||
12 | 12 | ||
13 | // award every unique letter until 5 repetitions | 13 | // award every unique letter until 5 repetitions |
14 | const letters = {}; | 14 | const letters = {}; |
15 | for (let i = 0; i < password.length; i += 1) { | 15 | for (const letter of password) { |
16 | letters[password[i]] = (letters[password[i]] || 0) + 1; | 16 | letters[letter] = (letters[letter] || 0) + 1; |
17 | score += 5.0 / letters[password[i]]; | 17 | score += 5 / letters[letter]; |
18 | } | 18 | } |
19 | 19 | ||
20 | // bonus points for mixing it up | 20 | // bonus points for mixing it up |
@@ -26,11 +26,11 @@ export function scorePassword(password: string) { | |||
26 | }; | 26 | }; |
27 | 27 | ||
28 | let variationCount = 0; | 28 | let variationCount = 0; |
29 | Object.keys(variations).forEach((key) => { | 29 | for (const key of Object.keys(variations)) { |
30 | variationCount += (variations[key] === true) ? 1 : 0; | 30 | variationCount += variations[key] === true ? 1 : 0; |
31 | }); | 31 | } |
32 | 32 | ||
33 | score += (variationCount - 1) * 10; | 33 | score += (variationCount - 1) * 10; |
34 | 34 | ||
35 | return parseInt(score.toString(), 10); | 35 | return Number.parseInt(score.toString(), 10); |
36 | } | 36 | } |
diff --git a/src/helpers/recipe-helpers.ts b/src/helpers/recipe-helpers.ts index 965429210..d482dffab 100644 --- a/src/helpers/recipe-helpers.ts +++ b/src/helpers/recipe-helpers.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | /* eslint-disable global-require */ | ||
1 | import { parse } from 'path'; | 2 | import { parse } from 'path'; |
2 | import { userDataRecipesPath } from '../environment'; | 3 | import { userDataRecipesPath } from '../environment-remote'; |
3 | 4 | ||
4 | export function getRecipeDirectory(id: string = ''): string { | 5 | export function getRecipeDirectory(id: string = ''): string { |
5 | return userDataRecipesPath(id); | 6 | return userDataRecipesPath(id); |
@@ -15,20 +16,17 @@ export function loadRecipeConfig(recipeId: string) { | |||
15 | // Delete module from cache | 16 | // Delete module from cache |
16 | delete require.cache[require.resolve(configPath)]; | 17 | delete require.cache[require.resolve(configPath)]; |
17 | 18 | ||
18 | // eslint-disable-next-line | 19 | // eslint-disable-next-line import/no-dynamic-require |
19 | let config = require(configPath); | 20 | const config = require(configPath); |
20 | 21 | ||
21 | const moduleConfigPath = require.resolve(configPath); | 22 | const moduleConfigPath = require.resolve(configPath); |
22 | config.path = parse(moduleConfigPath).dir; | 23 | config.path = parse(moduleConfigPath).dir; |
23 | 24 | ||
24 | return config; | 25 | return config; |
25 | } catch (e) { | 26 | } catch (error) { |
26 | console.error(e); | 27 | console.error(error); |
27 | return null; | 28 | return null; |
28 | } | 29 | } |
29 | } | 30 | } |
30 | 31 | ||
31 | module.paths.unshift( | 32 | module.paths.unshift(getDevRecipeDirectory(), getRecipeDirectory()); |
32 | getDevRecipeDirectory(), | ||
33 | getRecipeDirectory(), | ||
34 | ); | ||
diff --git a/src/helpers/schedule-helpers.ts b/src/helpers/schedule-helpers.ts index 754fd5556..55b7c1e6f 100644 --- a/src/helpers/schedule-helpers.ts +++ b/src/helpers/schedule-helpers.ts | |||
@@ -5,15 +5,15 @@ export function isInTimeframe(start: string, end: string) { | |||
5 | startHourStr, | 5 | startHourStr, |
6 | startMinuteStr, | 6 | startMinuteStr, |
7 | ] = start.split(':'); | 7 | ] = start.split(':'); |
8 | const startHour = parseInt(startHourStr, 10); | 8 | const startHour = Number.parseInt(startHourStr, 10); |
9 | const startMinute = parseInt(startMinuteStr, 10); | 9 | const startMinute = Number.parseInt(startMinuteStr, 10); |
10 | 10 | ||
11 | const [ | 11 | const [ |
12 | endHourStr, | 12 | endHourStr, |
13 | endMinuteStr, | 13 | endMinuteStr, |
14 | ] = end.split(':'); | 14 | ] = end.split(':'); |
15 | const endHour = parseInt(endHourStr, 10); | 15 | const endHour = Number.parseInt(endHourStr, 10); |
16 | const endMinute = parseInt(endMinuteStr, 10); | 16 | const endMinute = Number.parseInt(endMinuteStr, 10); |
17 | 17 | ||
18 | const currentHour = new Date().getHours(); | 18 | const currentHour = new Date().getHours(); |
19 | const currentMinute = new Date().getMinutes(); | 19 | const currentMinute = new Date().getMinutes(); |
diff --git a/src/helpers/serverless-helpers.js b/src/helpers/serverless-helpers.ts index 01549e038..01549e038 100644 --- a/src/helpers/serverless-helpers.js +++ b/src/helpers/serverless-helpers.ts | |||
diff --git a/src/helpers/service-helpers.js b/src/helpers/service-helpers.js deleted file mode 100644 index 745f40dd9..000000000 --- a/src/helpers/service-helpers.js +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | import { readdirSync, removeSync } from 'fs-extra'; | ||
2 | import { userDataPath } from '../environment'; | ||
3 | |||
4 | export function getServicePartitionsDirectory(...segments) { | ||
5 | return userDataPath('Partitions', ...([segments].flat())); | ||
6 | } | ||
7 | |||
8 | export function removeServicePartitionDirectory(id = '', addServicePrefix = false) { | ||
9 | const servicePartition = getServicePartitionsDirectory(`${addServicePrefix ? 'service-' : ''}${id}`); | ||
10 | return removeSync(servicePartition); | ||
11 | } | ||
12 | |||
13 | export async function getServiceIdsFromPartitions() { | ||
14 | const files = readdirSync(getServicePartitionsDirectory()); | ||
15 | return files.filter((n) => n !== '__chrome_extension'); | ||
16 | } | ||
diff --git a/src/helpers/service-helpers.ts b/src/helpers/service-helpers.ts new file mode 100644 index 000000000..678d5024f --- /dev/null +++ b/src/helpers/service-helpers.ts | |||
@@ -0,0 +1,21 @@ | |||
1 | import { readdirSync, removeSync } from 'fs-extra'; | ||
2 | import { userDataPath } from '../environment-remote'; | ||
3 | |||
4 | export function getServicePartitionsDirectory(...segments) { | ||
5 | return userDataPath('Partitions', ...[segments].flat()); | ||
6 | } | ||
7 | |||
8 | export function removeServicePartitionDirectory( | ||
9 | id = '', | ||
10 | addServicePrefix = false, | ||
11 | ) { | ||
12 | const servicePartition = getServicePartitionsDirectory( | ||
13 | `${addServicePrefix ? 'service-' : ''}${id}`, | ||
14 | ); | ||
15 | return removeSync(servicePartition); | ||
16 | } | ||
17 | |||
18 | export async function getServiceIdsFromPartitions() { | ||
19 | const files = readdirSync(getServicePartitionsDirectory()); | ||
20 | return files.filter(n => n !== '__chrome_extension'); | ||
21 | } | ||
diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts index 3657ae693..1e87ecabb 100644 --- a/src/helpers/url-helpers.ts +++ b/src/helpers/url-helpers.ts | |||
@@ -12,7 +12,7 @@ export function isValidExternalURL(url: string | URL) { | |||
12 | let parsedUrl: URL; | 12 | let parsedUrl: URL; |
13 | try { | 13 | try { |
14 | parsedUrl = new URL(url.toString()); | 14 | parsedUrl = new URL(url.toString()); |
15 | } catch (_) { | 15 | } catch { |
16 | return false; | 16 | return false; |
17 | } | 17 | } |
18 | 18 | ||
diff --git a/src/helpers/userAgent-helpers.ts b/src/helpers/userAgent-helpers.ts index 73c8bfd03..091a76400 100644 --- a/src/helpers/userAgent-helpers.ts +++ b/src/helpers/userAgent-helpers.ts | |||
@@ -8,7 +8,7 @@ import { | |||
8 | function macOS() { | 8 | function macOS() { |
9 | const version = macosVersion() || ''; | 9 | const version = macosVersion() || ''; |
10 | let cpuName = os.cpus()[0].model.split(' ')[0]; | 10 | let cpuName = os.cpus()[0].model.split(' ')[0]; |
11 | if (cpuName && cpuName.match(/\(/)) { | 11 | if (cpuName && /\(/.test(cpuName)) { |
12 | cpuName = cpuName.split('(')[0]; | 12 | cpuName = cpuName.split('(')[0]; |
13 | } | 13 | } |
14 | return `Macintosh; ${cpuName} Mac OS X ${version.replace(/\./g, '_')}`; | 14 | return `Macintosh; ${cpuName} Mac OS X ${version.replace(/\./g, '_')}`; |
diff --git a/src/helpers/validation-helpers.js b/src/helpers/validation-helpers.js deleted file mode 100644 index 116f19905..000000000 --- a/src/helpers/validation-helpers.js +++ /dev/null | |||
@@ -1,67 +0,0 @@ | |||
1 | import { defineMessages } from 'react-intl'; | ||
2 | import isEmail from 'validator/lib/isEmail'; | ||
3 | |||
4 | const messages = defineMessages({ | ||
5 | required: { | ||
6 | id: 'validation.required', | ||
7 | defaultMessage: '!!!Field is required', | ||
8 | }, | ||
9 | email: { | ||
10 | id: 'validation.email', | ||
11 | defaultMessage: '!!!Email not valid', | ||
12 | }, | ||
13 | url: { | ||
14 | id: 'validation.url', | ||
15 | defaultMessage: '!!!Not a valid URL', | ||
16 | }, | ||
17 | minLength: { | ||
18 | id: 'validation.minLength', | ||
19 | defaultMessage: '!!!Too few characters', | ||
20 | }, | ||
21 | oneRequired: { | ||
22 | id: 'validation.oneRequired', | ||
23 | defaultMessage: '!!!At least one is required', | ||
24 | }, | ||
25 | }); | ||
26 | |||
27 | export function required({ field }) { | ||
28 | const isValid = (field.value.trim() !== ''); | ||
29 | return [isValid, window.ferdi.intl.formatMessage(messages.required, { field: field.label })]; | ||
30 | } | ||
31 | |||
32 | export function email({ field }) { | ||
33 | const value = field.value.trim(); | ||
34 | const isValid = isEmail(value); | ||
35 | return [isValid, window.ferdi.intl.formatMessage(messages.email, { field: field.label })]; | ||
36 | } | ||
37 | |||
38 | export function url({ field }) { | ||
39 | const value = field.value.trim(); | ||
40 | let isValid = false; | ||
41 | |||
42 | if (value !== '') { | ||
43 | // eslint-disable-next-line | ||
44 | isValid = Boolean(value.match(/(^|[\s.:;?\-\]<(])(https?:\/\/[-\w;/?:@&=+$|_.!~*|'()[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',|().:;?\-[\]>)])/i)); | ||
45 | } else { | ||
46 | isValid = true; | ||
47 | } | ||
48 | |||
49 | return [isValid, window.ferdi.intl.formatMessage(messages.url, { field: field.label })]; | ||
50 | } | ||
51 | |||
52 | export function minLength(length) { | ||
53 | return ({ field }) => { | ||
54 | let isValid = true; | ||
55 | if (field.touched) { | ||
56 | isValid = field.value.length >= length; | ||
57 | } | ||
58 | return [isValid, window.ferdi.intl.formatMessage(messages.minLength, { field: field.label, length })]; | ||
59 | }; | ||
60 | } | ||
61 | |||
62 | export function oneRequired(targets) { | ||
63 | return ({ field, form }) => { | ||
64 | const invalidFields = targets.filter((target) => form.$(target).value === ''); | ||
65 | return [targets.length !== invalidFields.length, window.ferdi.intl.formatMessage(messages.required, { field: field.label })]; | ||
66 | }; | ||
67 | } | ||
diff --git a/src/helpers/validation-helpers.ts b/src/helpers/validation-helpers.ts new file mode 100644 index 000000000..23c297443 --- /dev/null +++ b/src/helpers/validation-helpers.ts | |||
@@ -0,0 +1,96 @@ | |||
1 | import { defineMessages } from 'react-intl'; | ||
2 | import isEmail from 'validator/lib/isEmail'; | ||
3 | |||
4 | const messages = defineMessages({ | ||
5 | required: { | ||
6 | id: 'validation.required', | ||
7 | defaultMessage: '{field} is required', | ||
8 | }, | ||
9 | email: { | ||
10 | id: 'validation.email', | ||
11 | defaultMessage: '{field} is not valid', | ||
12 | }, | ||
13 | url: { | ||
14 | id: 'validation.url', | ||
15 | defaultMessage: '{field} is not a valid URL', | ||
16 | }, | ||
17 | minLength: { | ||
18 | id: 'validation.minLength', | ||
19 | defaultMessage: '{field} should be at least {length} characters long', | ||
20 | }, | ||
21 | oneRequired: { | ||
22 | id: 'validation.oneRequired', | ||
23 | defaultMessage: 'At least one is required', | ||
24 | }, | ||
25 | }); | ||
26 | |||
27 | export function required({ field }) { | ||
28 | const isValid = field.value.trim() !== ''; | ||
29 | return [ | ||
30 | isValid, | ||
31 | (window as any).ferdi.intl.formatMessage(messages.required, { | ||
32 | field: field.label, | ||
33 | }), | ||
34 | ]; | ||
35 | } | ||
36 | |||
37 | export function email({ field }) { | ||
38 | const value = field.value.trim(); | ||
39 | const isValid = isEmail(value); | ||
40 | return [ | ||
41 | isValid, | ||
42 | (window as any).ferdi.intl.formatMessage(messages.email, { | ||
43 | field: field.label, | ||
44 | }), | ||
45 | ]; | ||
46 | } | ||
47 | |||
48 | export function url({ field }) { | ||
49 | const value = field.value.trim(); | ||
50 | let isValid = false; | ||
51 | |||
52 | isValid = | ||
53 | value !== '' | ||
54 | ? Boolean( | ||
55 | // eslint-disable-next-line unicorn/better-regex | ||
56 | /(^|[\s.:;?\-\]<(])(https?:\/\/[-\w;/?:@&=+$|_.!~*|'()[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',|().:;?\-[\]>)])/i.test( | ||
57 | value, | ||
58 | ), | ||
59 | ) | ||
60 | : true; | ||
61 | |||
62 | return [ | ||
63 | isValid, | ||
64 | (window as any).ferdi.intl.formatMessage(messages.url, { | ||
65 | field: field.label, | ||
66 | }), | ||
67 | ]; | ||
68 | } | ||
69 | |||
70 | export function minLength(length: number) { | ||
71 | return ({ field }) => { | ||
72 | let isValid = true; | ||
73 | if (field.touched) { | ||
74 | isValid = field.value.length >= length; | ||
75 | } | ||
76 | return [ | ||
77 | isValid, | ||
78 | (window as any).ferdi.intl.formatMessage(messages.minLength, { | ||
79 | field: field.label, | ||
80 | length, | ||
81 | }), | ||
82 | ]; | ||
83 | }; | ||
84 | } | ||
85 | |||
86 | export function oneRequired(targets: string[]) { | ||
87 | return ({ field, form }) => { | ||
88 | const invalidFields = targets.filter(target => form.$(target).value === ''); | ||
89 | return [ | ||
90 | targets.length !== invalidFields.length, | ||
91 | (window as any).ferdi.intl.formatMessage(messages.required, { | ||
92 | field: field.label, | ||
93 | }), | ||
94 | ]; | ||
95 | }; | ||
96 | } | ||