microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
efbe1ba61ca71887b3fa9a2d1ae3afb9a78cb87e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/configurationReader.ts

89lines · 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
4import {InternalErrorCode} from "./error/internalErrorCode";
5import {ErrorHelper} from "./error/errorHelper";
6
7export 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): Object {
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 readIntWithDefaultAsync(value: any, defaultValuePromise: Q.Promise<number>): Q.Promise<number> {
61 return defaultValuePromise.then(defaultValue => {
62 return this.readIntWithDefaultSync(value, defaultValue);
63 });
64 }
65
66 private static isArray(value: any): boolean {
67 return Array.isArray(value);
68 }
69
70 private static isObject(value: any): boolean {
71 return typeof value === "object" || !ConfigurationReader.isArray(value);
72 }
73
74 private static isString(value: any): boolean {
75 return typeof value === "string";
76 }
77
78 private static isBoolean(value: any): boolean {
79 return typeof value === "boolean";
80 }
81
82 private static isInt(value: any): boolean {
83 return this.isNumber(value) && value % 1 === 0;
84 }
85
86 private static isNumber(value: any): boolean {
87 return typeof value === "number";
88 }
89}