microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.10.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/configurationReader.ts

86lines · modeblame

5e651f3edigeff10 years ago1// 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 ago4import { InternalErrorCode } from "./error/internalErrorCode";
5import { ErrorHelper } from "./error/errorHelper";
df4bce40digeff10 years ago6
5e651f3edigeff10 years ago7export class ConfigurationReader {
9e81e40fPatricio Beltran10 years ago8public static readString(value: any): string {
9if (this.isString(value)) {
10return value;
11}
09f6024fHeniker4 years ago12throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedStringValue, value);
9e81e40fPatricio Beltran10 years ago13}
14
15public static readBoolean(value: any): boolean {
16if (this.isBoolean(value)) {
17return value;
18} else if (value === "true" || value === "false") {
19return value === "true";
20}
09f6024fHeniker4 years ago21throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedBooleanValue, value);
9e81e40fPatricio Beltran10 years ago22}
23
24public static readArray(value: any): Array<any> {
25if (this.isArray(value)) {
26return value;
27}
09f6024fHeniker4 years ago28throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedArrayValue, value);
9e81e40fPatricio Beltran10 years ago29}
30
4f8669f9JiglioNero6 years ago31public static readObject(value: any): Record<string, any> {
9e81e40fPatricio Beltran10 years ago32if (this.isObject(value)) {
33return value;
34}
09f6024fHeniker4 years ago35throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedObjectValue, value);
9e81e40fPatricio Beltran10 years ago36}
37
5e651f3edigeff10 years ago38/* We try to read an integer. It can be either an integer, or a string that can be parsed as an integer */
872e0262digeff10 years ago39public static readInt(value: any): number {
5e651f3edigeff10 years ago40if (this.isInt(value)) {
41return value;
9e81e40fPatricio Beltran10 years ago42} else if (this.isString(value)) {
5e651f3edigeff10 years ago43return parseInt(value, 10);
44}
09f6024fHeniker4 years ago45throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedIntegerValue, value);
5e651f3edigeff10 years ago46}
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)
49If the value is provided but it can't be parsed we'll throw an exception so the user knows that we didn't understand
50the value that was provided */
872e0262digeff10 years ago51public static readIntWithDefaultSync(value: any, defaultValue: number): number {
9e81e40fPatricio Beltran10 years ago52return value ? this.readInt(value) : defaultValue;
5e651f3edigeff10 years ago53}
54
0d77292aJiglioNero4 years ago55public static async readIntWithDefaultAsync(
34472878RedMickey5 years ago56value: any,
57defaultValuePromise: Promise<number>,
58): Promise<number> {
0d77292aJiglioNero4 years ago59const defaultValue = await defaultValuePromise;
60return this.readIntWithDefaultSync(value, defaultValue);
5e651f3edigeff10 years ago61}
62
9e81e40fPatricio Beltran10 years ago63private static isArray(value: any): boolean {
64return Array.isArray(value);
65}
66
67private static isObject(value: any): boolean {
68return typeof value === "object" || !ConfigurationReader.isArray(value);
69}
70
71private static isString(value: any): boolean {
72return typeof value === "string";
73}
74
75private static isBoolean(value: any): boolean {
76return typeof value === "boolean";
77}
78
872e0262digeff10 years ago79private static isInt(value: any): boolean {
5e651f3edigeff10 years ago80return this.isNumber(value) && value % 1 === 0;
81}
82
872e0262digeff10 years ago83private static isNumber(value: any): boolean {
5e651f3edigeff10 years ago84return typeof value === "number";
85}
34472878RedMickey5 years ago86}