microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/configurationReader.ts
39lines · 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 | /* We try to read an integer. It can be either an integer, or a string that can be parsed as an integer */ |
| 9 | public static readInt(value: any): number { |
| 10 | if (this.isInt(value)) { |
| 11 | return value; |
| 12 | } else if (typeof value === "string") { |
| 13 | return parseInt(value, 10); |
| 14 | } else { |
| 15 | throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedIntegerValue, value); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /* 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) |
| 20 | 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 |
| 21 | the value that was provided */ |
| 22 | public static readIntWithDefaultSync(value: any, defaultValue: number): number { |
| 23 | return value ? this.readInt(value) : defaultValue; |
| 24 | } |
| 25 | |
| 26 | public static readIntWithDefaultAsync(value: any, defaultValuePromise: Q.Promise<number>): Q.Promise<number> { |
| 27 | return defaultValuePromise.then(defaultValue => { |
| 28 | return this.readIntWithDefaultSync(value, defaultValue); |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | private static isInt(value: any): boolean { |
| 33 | return this.isNumber(value) && value % 1 === 0; |
| 34 | } |
| 35 | |
| 36 | private static isNumber(value: any): boolean { |
| 37 | return typeof value === "number"; |
| 38 | } |
| 39 | } |