npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iainad/merge-objects

v1.0.1

Published

Merges a number of javascript objects into a new object, preserving getter and setter methods in the process

Downloads

2

Readme

merge-objects

Simple library to merge an arbritary number of objects together preserving getter and setter methods.

Background

Object.assign and the spread operator only get you so far when you are trying to merge objects that contain getter and setter methods like:

const person = {
  firstName: null,
  lastName: null,

  get name() {
    return `${this.firstName} ${this.lastName}`
  },
  set name(newName) {
    const [firstName, lastName] = newName.split(' ')

    this.firstName = firstName
    this.lastName = lastName
  },
}

If we wanted to create a clone of this we might think to use:

const person2 = Object.assign({}, person)

but when we do this person2 has a simple key based value for name not the dynamic value linked to firstName and lastName as person.

e.g.

> person.name = 'Iain Donaldson'
'Iain Donaldson'
> person
{ firstName: 'Iain', lastName: 'Donaldson', name: [Getter/Setter] }
> person2
{ firstName: 'Iain', lastName: 'Donaldson', name: 'Iain Donaldson' }

Using the spread operator results in the same thing, e.g.

const person3 = { ...person }

person3 now has keys that reflect the values of the getter in person and not the getter and setter methods themselves.

This package allwows you to combine objects that include getter/setter methods simply.

Usage

Usage of this package is straightforward, e.g.:

import mergeObjects from '@iainad/merge-objects'

const person = {
  firstName: null,
  lastName: null,

  get name() {
    return `${this.firstName} ${this.lastName}`
  },

  set name(newName) {
    const [firstName, lastName] = newName.split(' ')

    this.firstName = firstName
    this.lastName = lastName
  },
}

const address = {
  houseNumber: 10,
  streetName: 'Town Road',
  town: 'Townville',
  zip: '12233',
}

const mergedPerson = mergeObjects(person, address)

In the above example the new object will be the combination of the objects but maintains the getter and setter methods for the name as in the original object.

Credits

Most of the actually useful code here comes from @joelmoss in ibiza React 'state management for party animals' so thanks Joel!

I wanted this to allow me to destructure models in Ibiza as follows:

import mergeObjects from '@iainad/merge-objects'
import { createModel } from 'ibiza'

const modelAccessors = {
  get name() {
    return `${this.firstName} ${this.lastName}`
  },
  set name(newName) {
    const [firstName, lastName] = newName.split(' ')

    this.firstName = firstName
    this.lastName = lastName
  },
}

const modelActions = {
  clearName() {
    this.firstName = ''
    this.lastName = ''
  },
}

const modelInitialProps = {
  firstName: null,
  lastName: null,
}

const model = createModel('model.mount.path', (_, runtimeProps) =>
  mergeObjects(modelInitialProps, modelAccessors, modelActions, runtimeProps)
)

export default model

This allows creation of more complex models with getters, setters and actions but not having to have them all in the same model defintion file.

TODO

Add some tests!