@burnsred/defaults
v1.0.0
Published
A system for providing project-wide defaults to libraries.
Downloads
34
Readme
Defaults
A system for providing project-wide defaults to libraries.
Setup
First, define your defaults object.
This will map lookup keys to any sort of value. It can be nested to help with specialised scoping.
export default {
foo: FooComponent,
routes: {
foo: RoutedFoo,
members: {
foo: SpecialFooComponent,
},
},
}
In your code, use the hooks to access them:
- Direct lookup
const foo = useDefault("foo"); // Will return `FooComponent`
The lookup key can contain dots, in which case it will drill down into objects.
const foo = useDefault("routes.foo"); // Will return `RoutedFoo`
- Defaults Map
Will find the first matching (non-undefined) lookup for each key from its list of lookups.
const foo = useDefaultMap({
foo: ["routes.members.foo", "routes.foo"],
})
This will first check for default["routes"]["members"]["foo"]
, and if that's
undefined, try default["routes"]["foo"]
, finally resorting to
default["foo"]
.
- Fill defaults
Use the fallback defaults method to fill absent entries on an object.
const fullProps = useFillDefaults(props, defaultsMap);
Any keys from defaultsMap
that are not present in props
will be resolved as
with useDefaultMap
.
- Scoped lookup
All of the above hooks acception an optional scope
argument.
This can be useful to, for instance, provide a typed default proxy for lookups within a certain section of the defaults object:
function useDefaultWidget(key: string): Widget {
return useDefault(key, 'widgets') as Widget;
}