lib/src/service/CanonicalRecursive.ts
garrettmills 1d5056b753
All checks were successful
continuous-integration/drone/push Build is passing
Setup eslint and enforce rules
2021-06-02 22:36:25 -05:00

35 lines
892 B
TypeScript

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
}
}