Accessor

Turn any object into a statically typed facade.

Source code is hosted on GitHub

This library allows you to create a statically typed object accessor based on the Proxy API. The main purpose of this package is to allow dynamic object access in a static manner, without having to rely on dynamic, string based keys. Code won't compile if you try to access a path that does not exist. You keep control over what is returned from the getter, by providing a custom access handler.

yarn add @corets/accessor

createAccessor()

Create an accessor instance of the type ObjectAccessor.

import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

Now you can statically access every object field:

accessor.some.field.get()

You can provide a custom access handler to customise the access behaviour. Below is an example of how one could statically access translations, proxied by a translation library, using the accessor.

import { createAccessor } from "@corets/accessor"

const translations = {  
    pages: {    
        home: {      
            title: "Home {replacement}"    
        },    
        about: {      
            title: "About {replacement}"    
        }  
    }
}

const translator = createSomeSortOfTranslator(translations)

const locales = createAccessor(translations, (source, key, ...replacements: any[]): string => {  
    return translator.get(key, { replacements })
})

Now you can indirectly access translations through the accessor, without using any dynamic keys.

const translatedHomePageTitle = locales.pages.home.title.get("Page")

Trying to access a missing key will result in a compilation error. 💥

This library is used internally by this translation library, to power its static translations access:

pageTranslator

ObjectAccessor.key()

Get absolute field key:

import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.field.key()

ObjectAccessor.keyAt()

Get absolute field key, bypassing the static typing:

import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.keyAt("field")

ObjectAccessor.get()

Get field value coming from the access handler:

import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.field.get()

ObjectAccessor.getAt()

Get field value coming from the access handler, bypassing the static typing:

import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.getAt("field")

ObjectAccessor.has()

Check if a field exists:

import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.field.has()

ObjectAccessor.hasAt()

Check if a field exists, bypassing the static typing:

import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.hasAt("field")

Last updated