LogoLogo
  • Home
  • Services
    • Fiber
    • Accessor
    • Schema
    • Form
    • Translator
  • Components
    • <Router/>
    • <Memo />
  • Observables
    • Async
    • Value
    • Store
    • List
    • Local Storage Value
    • Local Storage Store
    • Local Storage List
  • Hooks
    • useAsync
    • useAffect
    • useStream
    • useAction
    • useDebounce
    • useThrottle
    • usePrevious
    • useIdle
    • useValue
    • useList
    • useStore
    • useForm
    • useFormBinder
    • useTranslator
    • useQuery
  • Helpers
    • Tag
    • Input Helpers
    • Promise Helpers
    • Save Helpers
    • Pagination Helpers
    • Clipboard Helpers
    • Calendar Helpers
    • Local Storage Helpers
Powered by GitBook
On this page
  • createAccessor()
  • ObjectAccessor.key()
  • ObjectAccessor.keyAt()
  • ObjectAccessor.get()
  • ObjectAccessor.getAt()
  • ObjectAccessor.has()
  • ObjectAccessor.hasAt()

Was this helpful?

  1. Services

Accessor

Turn any object into a statically typed facade.

PreviousFiberNextSchema

Last updated 3 years ago

Was this helpful?

Source code is hosted on

This library allows you to create a statically typed object accessor based on the . 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
npm install --save @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:

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")
GitHub
Proxy API
Translator