1.0.0 • Published 5 months ago

@burnsred/defaults v1.0.0

Weekly downloads
-
License
UNLICENSED
Repository
bitbucket
Last release
5 months ago

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:

  1. 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`
  1. 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"].

  1. 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.

  1. 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;
}
1.0.0

5 months ago