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