Skip to content

Commit bfcee67

Browse files
committed
Remove unnecessary options
1 parent 38c6577 commit bfcee67

File tree

3 files changed

+38
-61
lines changed

3 files changed

+38
-61
lines changed

src/AzureAppConfiguration.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ export interface ConfigurationObjectConstructionOptions {
3232
* Default is '.'.
3333
*/
3434
separator?: string;
35-
36-
/**
37-
* The prefix of hierarchical keys to be converted object properties, usefull when converting a subset of the keys.
38-
* Default is '', representing the root of the object.
39-
*/
40-
prefix?: string;
41-
42-
/**
43-
* The behavior when error or amibiguity occurs on converting hierarchical keys.
44-
* Default is 'error'.
45-
*/
46-
onError?: "error" | "ignore";
4735
}
4836

4937
interface IGettable {

src/AzureAppConfigurationImpl.ts

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -184,47 +184,31 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
184184
*/
185185
constructConfigurationObject(options?: ConfigurationObjectConstructionOptions): Record<string, any> {
186186
const separator = options?.separator ?? ".";
187-
const prefix = options?.prefix ?? "";
188-
const onError = options?.onError ?? "error";
189187

190188
// construct hierarchical data object from map
191189
const data: Record<string, any> = {};
192190
for (const [key, value] of this.#configMap) {
193-
if (key.startsWith(prefix)) {
194-
const segments = key.slice(prefix.length).split(separator);
195-
let current = data;
196-
// construct hierarchical data object along the path
197-
for (let i = 0; i < segments.length - 1; i++) {
198-
const segment = segments[i];
199-
// undefined or empty string
200-
if (!segment) {
201-
if (onError === "error") {
202-
throw new Error(`invalid key: ${key}`);
203-
} else if (onError === "ignore") {
204-
continue;
205-
} else {
206-
throw new Error(`The value of 'onError' is not supported: ${onError}`);
207-
}
208-
}
209-
// create path if not exist
210-
if (current[segment] === undefined) {
211-
current[segment] = {};
212-
}
213-
// The path has been occupied by a non-object value, causing ambiguity.
214-
if (typeof current[segment] !== "object") {
215-
if (onError === "error") {
216-
throw new Error(`The key '${prefix}${segments.slice(0, i + 1).join(separator)}' is not a valid path.`);
217-
} else if (onError === "ignore") {
218-
current[segment] = {}; // overwrite the non-object value
219-
} else {
220-
throw new Error(`The value of 'onError' is not supported: ${onError}`);
221-
}
222-
}
223-
current = current[segment];
191+
const segments = key.split(separator);
192+
let current = data;
193+
// construct hierarchical data object along the path
194+
for (let i = 0; i < segments.length - 1; i++) {
195+
const segment = segments[i];
196+
// undefined or empty string
197+
if (!segment) {
198+
throw new Error(`invalid key: ${key}`);
199+
}
200+
// create path if not exist
201+
if (current[segment] === undefined) {
202+
current[segment] = {};
203+
}
204+
// The path has been occupied by a non-object value, causing ambiguity.
205+
if (typeof current[segment] !== "object") {
206+
throw new Error(`The key '${segments.slice(0, i + 1).join(separator)}' is not a valid path.`);
224207
}
225-
// set value to the last segment
226-
current[segments[segments.length - 1]] = value;
208+
current = current[segment];
227209
}
210+
// set value to the last segment
211+
current[segments[segments.length - 1]] = value;
228212
}
229213
return data;
230214
}

test/load.test.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ const mockedKVs = [{
3838
}, {
3939
key: "app3.settings.fontColor",
4040
value: "yellow"
41-
}].map(createMockedKeyValue);
41+
}, {
42+
key: "app4.excludedFolders.0",
43+
value: "node_modules"
44+
}, {
45+
key: "app4.excludedFolders.1",
46+
value: "dist"
47+
}
48+
].map(createMockedKeyValue);
4249

4350
describe("load", function () {
4451
this.timeout(10000);
@@ -226,33 +233,31 @@ describe("load", function () {
226233
* get() will return "placeholder" for "app3.settings" and "yellow" for "app3.settings.fontColor", as expected.
227234
* data.app3.settings will return "placeholder" as a whole JSON object, which is not guarenteed to be correct.
228235
*/
229-
it("Edge case: Hierarchical key-value pairs with overlapped key prefix. Ignore error.", async () => {
236+
it("Edge case: Hierarchical key-value pairs with overlapped key prefix.", async () => {
230237
const connectionString = createMockedConnectionString();
231238
const settings = await load(connectionString, {
232239
selectors: [{
233240
keyFilter: "app3.settings*"
234241
}]
235242
});
236243
expect(settings).not.undefined;
237-
// use get() method
238-
expect(settings.get("app3.settings")).eq("placeholder");
239-
expect(settings.get("app3.settings.fontColor")).eq("yellow");
240-
// use data property
241-
const data = settings.constructConfigurationObject({ onError: "ignore" }); // ignore error on hierarchical key conversion
242-
expect(data.app3.settings).not.eq("placeholder"); // not as expected.
243-
expect(data.app3.settings.fontColor).eq("yellow");
244+
expect(() => {
245+
settings.constructConfigurationObject();
246+
}).to.throw("The key 'app3.settings' is not a valid path.");
244247
});
245248

246-
it("Edge case: Hierarchical key-value pairs with overlapped key prefix. Default error.", async () => {
249+
it("should construct configuration object with array", async () => {
247250
const connectionString = createMockedConnectionString();
248251
const settings = await load(connectionString, {
249252
selectors: [{
250-
keyFilter: "app3.settings*"
253+
keyFilter: "app4.*"
251254
}]
252255
});
253256
expect(settings).not.undefined;
254-
expect(() => {
255-
settings.constructConfigurationObject();
256-
}).to.throw("The key 'app3.settings' is not a valid path.");
257+
const data = settings.constructConfigurationObject();
258+
expect(data).not.undefined;
259+
// Both { '0': 'node_modules', '1': 'dist' } and ['node_modules', 'dist'] are valid.
260+
expect(data.app4.excludedFolders[0]).eq("node_modules");
261+
expect(data.app4.excludedFolders[1]).eq("dist");
257262
});
258263
});

0 commit comments

Comments
 (0)