microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/configurationReader.ts

92lines · 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} else {
12throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedStringValue, value);
13}
14}
15
16public static readBoolean(value: any): boolean {
17if (this.isBoolean(value)) {
18return value;
19} else if (value === "true" || value === "false") {
20return value === "true";
21} else {
22throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedBooleanValue, value);
23}
24}
25
26public static readArray(value: any): Array<any> {
27if (this.isArray(value)) {
28return value;
29} else {
30throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedArrayValue, value);
31}
32}
33
4f8669f9JiglioNero6 years ago34public static readObject(value: any): Record<string, any> {
9e81e40fPatricio Beltran10 years ago35if (this.isObject(value)) {
36return value;
37} else {
38throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedObjectValue, value);
39}
40}
41
5e651f3edigeff10 years ago42/* We try to read an integer. It can be either an integer, or a string that can be parsed as an integer */
872e0262digeff10 years ago43public static readInt(value: any): number {
5e651f3edigeff10 years ago44if (this.isInt(value)) {
45return value;
9e81e40fPatricio Beltran10 years ago46} else if (this.isString(value)) {
5e651f3edigeff10 years ago47return parseInt(value, 10);
48} else {
df4bce40digeff10 years ago49throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedIntegerValue, value);
5e651f3edigeff10 years ago50}
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)
54If the value is provided but it can't be parsed we'll throw an exception so the user knows that we didn't understand
55the value that was provided */
872e0262digeff10 years ago56public static readIntWithDefaultSync(value: any, defaultValue: number): number {
9e81e40fPatricio Beltran10 years ago57return value ? this.readInt(value) : defaultValue;
5e651f3edigeff10 years ago58}
59
34472878RedMickey5 years ago60public static readIntWithDefaultAsync(
61value: any,
62defaultValuePromise: Promise<number>,
63): Promise<number> {
5e651f3edigeff10 years ago64return defaultValuePromise.then(defaultValue => {
65return this.readIntWithDefaultSync(value, defaultValue);
66});
67}
68
9e81e40fPatricio Beltran10 years ago69private static isArray(value: any): boolean {
70return Array.isArray(value);
71}
72
73private static isObject(value: any): boolean {
74return typeof value === "object" || !ConfigurationReader.isArray(value);
75}
76
77private static isString(value: any): boolean {
78return typeof value === "string";
79}
80
81private static isBoolean(value: any): boolean {
82return typeof value === "boolean";
83}
84
872e0262digeff10 years ago85private static isInt(value: any): boolean {
5e651f3edigeff10 years ago86return this.isNumber(value) && value % 1 === 0;
87}
88
872e0262digeff10 years ago89private static isNumber(value: any): boolean {
5e651f3edigeff10 years ago90return typeof value === "number";
91}
34472878RedMickey5 years ago92}