Coder Social home page Coder Social logo

define-accessors's Introduction

define-accessors

define accessors for an object on another object

🔧 Install · 🧩 Example · 📜 API docs · 🔥 Releases · 💪🏼 Contribute · 🖐️ Help


Install

$ npm i define-accessors

API

Table of Contents

defineAccessors

src/index.ts:59-72

Defines accessors for a source object on a target object.

Example; all values reflected on source:

const target = {}
const source = {
  foo: 'a prop',
  bar: 'another',
  zoo: 10,
}
const typed = defineAccessors(target, source)
expect(typed).toBe(target)
expect(typed.foo).toBe(source.foo)
expect(typed.bar).toBe(source.bar)
expect(typed.zoo).toBe(source.zoo)

typed.foo = 'something else'
expect(typed.foo).toEqual('something else')
expect(source.foo).toEqual('something else')

Example; all values reflected on other using a custom property descriptor factory:

const target = {}
const source = {
  foo: 'a prop',
}
const other: Record<string, unknown> = {
  foo: 'something else',
}
const typed = defineAccessors(target, source, (key: string) => ({
  enumerable: true,
  get() {
    return other[key]
  },
  set(value: never) {
    other[key] = value
  },
}))
expect(typed).toBe(target)
expect(typed.foo).toBe(other.foo)

Parameters

  • target T The target object to define accessors on
  • source S The source object where the actual values are
  • propertyDescriptorFactory function (key: any, source: S): PropertyDescriptor A function that returns a custom property descriptor for the given key (optional, default createPropertyDescriptor)

Returns any The target object but with its type intersected with the source's type

Contribute

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2021 stagas

define-accessors's People

Contributors

stagas avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.