microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/configurationReader.ts
91lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { InternalErrorCode } from "./error/internalErrorCode"; |
| 5 | import { ErrorHelper } from "./error/errorHelper"; |
| 6 | |
| 7 | export class ConfigurationReader { |
| 8 | public static readString(value: any): string { |
| 9 | if (this.isString(value)) { |
| 10 | return value; |
| 11 | } else { |
| 12 | throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedStringValue, value); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | public static readBoolean(value: any): boolean { |
| 17 | if (this.isBoolean(value)) { |
| 18 | return value; |
| 19 | } else if (value === "true" || value === "false") { |
| 20 | return value === "true"; |
| 21 | } else { |
| 22 | throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedBooleanValue, value); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public static readArray(value: any): Array<any> { |
| 27 | if (this.isArray(value)) { |
| 28 | return value; |
| 29 | } else { |
| 30 | throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedArrayValue, value); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public static readObject(value: any): Record<string, any> { |
| 35 | if (this.isObject(value)) { |
| 36 | return value; |
| 37 | } else { |
| 38 | throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedObjectValue, value); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /* We try to read an integer. It can be either an integer, or a string that can be parsed as an integer */ |
| 43 | public static readInt(value: any): number { |
| 44 | if (this.isInt(value)) { |
| 45 | return value; |
| 46 | } else if (this.isString(value)) { |
| 47 | return parseInt(value, 10); |
| 48 | } else { |
| 49 | throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedIntegerValue, value); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* We try to read an integer. If it's a falsable value we return the default value, if not we behave like this.readInt(value) |
| 54 | If the value is provided but it can't be parsed we'll throw an exception so the user knows that we didn't understand |
| 55 | the value that was provided */ |
| 56 | public static readIntWithDefaultSync(value: any, defaultValue: number): number { |
| 57 | return value ? this.readInt(value) : defaultValue; |
| 58 | } |
| 59 | |
| 60 | public static async readIntWithDefaultAsync( |
| 61 | value: any, |
| 62 | defaultValuePromise: Promise<number>, |
| 63 | ): Promise<number> { |
| 64 | const defaultValue = await defaultValuePromise; |
| 65 | return this.readIntWithDefaultSync(value, defaultValue); |
| 66 | } |
| 67 | |
| 68 | private static isArray(value: any): boolean { |
| 69 | return Array.isArray(value); |
| 70 | } |
| 71 | |
| 72 | private static isObject(value: any): boolean { |
| 73 | return typeof value === "object" || !ConfigurationReader.isArray(value); |
| 74 | } |
| 75 | |
| 76 | private static isString(value: any): boolean { |
| 77 | return typeof value === "string"; |
| 78 | } |
| 79 | |
| 80 | private static isBoolean(value: any): boolean { |
| 81 | return typeof value === "boolean"; |
| 82 | } |
| 83 | |
| 84 | private static isInt(value: any): boolean { |
| 85 | return this.isNumber(value) && value % 1 === 0; |
| 86 | } |
| 87 | |
| 88 | private static isNumber(value: any): boolean { |
| 89 | return typeof value === "number"; |
| 90 | } |
| 91 | } |
| 92 | |