microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/configurationReader.ts

89lines · 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
df4bce40digeff10 years ago4import {InternalErrorCode} from "./error/internalErrorCode";
5import {ErrorHelper} from "./error/errorHelper";
6
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
34public static readObject(value: any): Object {
35if (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
872e0262digeff10 years ago60public static readIntWithDefaultAsync(value: any, defaultValuePromise: Q.Promise<number>): Q.Promise<number> {
5e651f3edigeff10 years ago61return defaultValuePromise.then(defaultValue => {
62return this.readIntWithDefaultSync(value, defaultValue);
63});
64}
65
9e81e40fPatricio Beltran10 years ago66private static isArray(value: any): boolean {
67return Array.isArray(value);
68}
69
70private static isObject(value: any): boolean {
71return typeof value === "object" || !ConfigurationReader.isArray(value);
72}
73
74private static isString(value: any): boolean {
75return typeof value === "string";
76}
77
78private static isBoolean(value: any): boolean {
79return typeof value === "boolean";
80}
81
872e0262digeff10 years ago82private static isInt(value: any): boolean {
5e651f3edigeff10 years ago83return this.isNumber(value) && value % 1 === 0;
84}
85
872e0262digeff10 years ago86private static isNumber(value: any): boolean {
5e651f3edigeff10 years ago87return typeof value === "number";
88}
89}