diff --git a/src/common/utils/object/object.mocha.ts b/src/common/utils/object/object.mocha.ts index 96375b7d7..056ec28d7 100644 --- a/src/common/utils/object/object.mocha.ts +++ b/src/common/utils/object/object.mocha.ts @@ -15,7 +15,7 @@ */ import { expect } from "chai"; -import { fromEntries, mapValues, omitFalsyValues } from "./object"; +import { fromEntries, mapValues, omitFalsyValues, pickValues } from "./object"; describe("Object utils", () => { describe("omitFalsyValues", () => { @@ -73,6 +73,43 @@ describe("Object utils", () => { }); }); + describe("pickValues", () => { + + const greaterThan10 = (n: number) => n > 10; + + it("should left only values that pass predicate", () => { + const input: any = { + a: 9, + b: 10, + c: 11 + }; + + const expected = { + c: 11 + }; + + expect(pickValues(input, greaterThan10)).to.be.deep.equal(expected); + }); + + it("should handle empty object", () => { + expect(pickValues({}, greaterThan10)).to.be.deep.equal({}); + }); + + it("should not modify input object", () => { + const input: any = { + a: 9, + b: 10, + c: 11 + }; + + const inputCopy = Object.assign({}, input); + + pickValues(input, greaterThan10); + + expect(input).to.deep.equal(inputCopy); + }); + }); + describe("mapValues", () => { function addAndStringify(num: number): string { return String(num + 1); diff --git a/src/common/utils/object/object.ts b/src/common/utils/object/object.ts index beb1194a1..0ba83905e 100644 --- a/src/common/utils/object/object.ts +++ b/src/common/utils/object/object.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { assoc, Unary } from "../functional/functional"; +import { assoc, Predicate, Unary } from "../functional/functional"; import { isTruthy } from "../general/general"; export function extend(source: any, target: any): any { @@ -27,12 +27,7 @@ export function extend(source: any, target: any): any { } export function omitFalsyValues(obj: T): Partial { - return Object.keys(obj).reduce>((res, key: keyof T & string) => { - if (isTruthy(obj[key])) { - res[key] = obj[key]; - } - return res; - }, {}); + return pickValues(obj, isTruthy); } type Key = string; @@ -44,6 +39,16 @@ export function mapValues(obj: Record, fn: Unary); } +export function pickValues(obj: T, predicate: Predicate): Partial { + return (Object.keys(obj) as K[]).reduce((result: Partial, key: K) => { + const value = obj[key]; + if (predicate(value)) { + result[key] = value; + } + return result; + }, {} as Partial); +} + export function fromEntries(entries: Array<[K, T]>): Record { return entries.reduce((result: Record, [key, value]: [K, T]) => assoc(result, key, value), {} as Record); diff --git a/src/server/cli/build-settings.ts b/src/server/cli/build-settings.ts index d385cbe9f..b35ced270 100644 --- a/src/server/cli/build-settings.ts +++ b/src/server/cli/build-settings.ts @@ -21,7 +21,9 @@ import { ClusterAuthJS } from "../../common/models/cluster-auth/cluster-auth"; import { fromConfig as clusterFromConfig } from "../../common/models/cluster/cluster"; import { fromConfig as dataCubeFromConfig } from "../../common/models/data-cube/data-cube"; import { fromConfig as sourcesFromConfig, Sources, SourcesJS } from "../../common/models/sources/sources"; +import { complement } from "../../common/utils/functional/functional"; import { isNil } from "../../common/utils/general/general"; +import { pickValues } from "../../common/utils/object/object"; import { LoggerFormat, ServerSettings, ServerSettingsJS } from "../models/server-settings/server-settings"; import { TurniloSettings } from "./run-turnilo"; @@ -45,10 +47,12 @@ function overrideClustersAuth(config: SourcesJS, auth: ClusterAuthJS): SourcesJS } export default function buildSettings(config: object, options: ServerOptions, auth?: ClusterAuthJS): TurniloSettings { + const definedOptions = pickValues(options, complement(isNil)); const serverSettingsJS: ServerSettingsJS = { ...config, - ...options + ...definedOptions }; + const serverSettings = ServerSettings.fromJS(serverSettingsJS); const logger = getLogger(serverSettings.loggerFormat); const appSettings = appSettingsFromConfig(config, logger);