You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/service/CanonicalRecursive.ts

35 lines
892 B

import {Canonical} from './Canonical'
/**
* Variant of the Canonical unit whose accessor allows accessing nested
* properties on the resolved objects.
*
* @example
* The Config unit is a CanonicalRecursive unit. So, once a config file is
* resolved, a particular value in the config file can be retrieved as well:
*
* ```typescript
* // app/config/my/config.config.ts
* {
* foo: {
* bar: 123
* }
* }
* ```
*
* This can be accessed as:
* ```typescript
* config.get('my:config.foo.bar') // => 123
* ```
*/
export class CanonicalRecursive extends Canonical<any> {
public get(key: string, fallback?: unknown): any | undefined {
const parts = key.split('.')
let currentValue = this.loadedItems
for ( const part of parts ) {
currentValue = currentValue?.[part]
}
return currentValue ?? fallback
}
}