1
0
mirror of https://github.com/jiahaog/Nativefier synced 2024-06-26 07:38:59 +02:00
Nativefier/src/utils/parseUtils.ts
Adam Weeden 7a08a2d676
Enable TypeScript strict:true, and more typescript-eslint rules (#1223)
* Catch promise errors better

* Move subFunctions to bottom of createNewWindow

* Use parents when creating child BrowserWindow instances

* Some about:blank pages have an anchor (for some reason)

* Inject browserWindowOptions better

* Interim refactor to MainWindow object

* Split up the window functions/helpers/events some

* Further separate out window functions + tests

* Add a mock for unit testing functions that access electron

* Add unit tests for onWillPreventUnload

* Improve windowEvents tests

* Add the first test for windowHelpers

* Move WebRequest event handling to node

* insertCSS completely under test

* clearAppData completely under test

* Fix contextMenu require bug

* More tests + fixes

* Fix + add to createNewTab tests

* Convert createMainWindow back to func + work out gremlins

* Move setupWindow away from main since its shared

* Make sure contextMenu is handling promises

* Fix issues with fullscreen not working + menu refactor

* Run jest against app/dist so that we can hit app unit tests as well

* Requested PR changes

* Add strict typing; tests currently failing

* Fix failing unit tests

* Add some more eslint warnings and fixes

* More eslint fixes

* Strict typing on (still issues with the lib dir)

* Fix the package.json import/require

* Fix some funky test errors

* Warn -> Error for eslint rules

* @ts-ignore -> @ts-expect-error

* Add back the ext code I removed
2021-06-15 22:20:49 -04:00

64 lines
1.6 KiB
TypeScript

import * as log from 'loglevel';
import { isWindows } from '../helpers/helpers';
export function parseBoolean(
val: boolean | string | number | undefined,
_default: boolean,
): boolean {
if (val === undefined) {
return _default;
}
try {
if (typeof val === 'boolean') {
return val;
}
val = String(val);
switch (val.toLocaleLowerCase()) {
case 'true':
case '1':
case 'yes':
return true;
case 'false':
case '0':
case 'no':
return false;
default:
return _default;
}
} catch {
return _default;
}
}
export function parseBooleanOrString(val: string): boolean | string {
switch (val) {
case 'true':
return true;
case 'false':
return false;
default:
return val;
}
}
export function parseJson<Type>(val: string): Type | undefined {
if (!val) return undefined;
try {
return JSON.parse(val) as Type;
} catch (err: unknown) {
const windowsShellHint = isWindows()
? `\n In particular, Windows cmd doesn't have single quotes, so you have to use only double-quotes plus escaping: "{\\"someKey\\": \\"someValue\\"}"`
: '';
log.error(
`Unable to parse JSON value: ${val}\n` +
`JSON should look like {"someString": "someValue", "someBoolean": true, "someArray": [1,2,3]}.\n` +
` - Only double quotes are allowed, single quotes are not.\n` +
` - Learn how your shell behaves and escapes characters.${windowsShellHint}\n` +
` - If unsure, validate your JSON using an online service.`,
);
throw err;
}
}