Skip to content

Commit

Permalink
update getOrDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
wz2cool committed May 19, 2024
1 parent 09940e7 commit 0f68c7b
Show file tree
Hide file tree
Showing 5 changed files with 9,257 additions and 6 deletions.
Empty file removed .scannerwork/.sonar_lock
Empty file.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"karma-firefox-launcher": "^1.1.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"karma-phantomjs-launcher": "^1.0.4",
"merge-stream": "^1.0.1",
"mocha": "~5.2.0",
"mocha-lcov-reporter": "~1.3.0",
Expand Down
29 changes: 24 additions & 5 deletions src/utils/object.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,32 @@ export class ObjectUtils {
* @example ObjectUtils.getOrDefault<number | undefined>(undefined, 0) = "0"
* @example ObjectUtils.getOrDefault<number | null>(1, 0) = "1"
* @example ObjectUtils.getOrDefault<number | null>(null, 0) = "0"
* @example ObjectUtils.getOrDefault(number | null>)(1, 2, 3) = "1"
* @example ObjectUtils.getOrDefault(number | null>)(null, 2, 3) = "2"
* @example ObjectUtils.getOrDefault(number | null>)(null, null, 3) = "3"
* @example ObjectUtils.getOrDefault(number | null>)(null, null, null) = null
* @example ObjectUtils.getOrDefault(number | null>)(null, null, null, 3) = "3"
*/
public static getOrDefault<T>(value: T, defaultValue: NonNullable<T>): NonNullable<T> {
if (ObjectUtils.isNullOrUndefined(value)) {
return defaultValue;
} else {
return value as NonNullable<T>;
public static getOrDefault<T>(value1: T, defaultValue: T): T;
public static getOrDefault<T>(value1: T, value2: T, defaultValue: T): T;
public static getOrDefault<T>(value1: T, value2: T, value3: T, defaultValue: T): T;
public static getOrDefault<T>(
value1: T,
value2: T,
value3: T,
defaultValue: T
): T
public static getOrDefault<T>(value1: T, value2?: T, value3?: T, defaultValue?: T): T {
if (!ObjectUtils.isNullOrUndefined(value1)) {
return value1 as NonNullable<T>;
}
if (!ObjectUtils.isNullOrUndefined(value2)) {
return value2 as NonNullable<T>;
}
if (!ObjectUtils.isNullOrUndefined(value3)) {
return value3 as NonNullable<T>;
}
return defaultValue;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions test/utils/object.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,42 @@ describe(".ObjectUtils", () => {
});

describe("#getOrDefault", () => {
it("should return 1, if value1 is not null", () => {
const value1: string | undefined = "1";
const value2: string | undefined = "2";
const result = ObjectUtils.getOrDefault(value1, value2, "defaultValue");
expect("1").to.be.eq(result);
});

it("should return 2, if value2 is not null and value1 is null", () => {
const value1: string | undefined = undefined;
const value2: string | undefined = "2";
const result = ObjectUtils.getOrDefault(value1, value2, "defaultValue");
expect("2").to.be.eq(result);
});

it("should return 'defaultValue', if all values is null", () => {
const value1: string | undefined = undefined;
const value2: string | undefined = undefined;
const result = ObjectUtils.getOrDefault(value1, value2, "defaultValue");
expect("defaultValue").to.be.eq(result);
})

it("should return 'defaultValue', if all values is null", () => {
const value1: string | undefined = undefined;
const value2: string | undefined = undefined;
const value3: string | undefined = undefined;
const result = ObjectUtils.getOrDefault(value1, value2, value3, "defaultValue");
expect("defaultValue").to.be.eq(result);
})

it("should return '', if first value is not null", () => {
const value1: string | undefined = undefined;
const value2: string | undefined = "2";
const result = ObjectUtils.getOrDefault(value1, value2, "defaultValue");
expect("2").to.be.eq(result);
});

it("should return [1,2,3] if value is not null or empty", () => {
const target = [1, 2, 3];
const value: number[] | undefined = target;
Expand Down

0 comments on commit 0f68c7b

Please sign in to comment.