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

super-assign

v1.0.4

Published

Vitaminized object assignation with customizable options and inferred types

Downloads

8

Readme

super-assign

Vitaminized object assignation with customizable options and inferred types.

Installation

via npm:

npm install super-assign

via yarn:

yarn add super-assign

Usage

It's used in the same way as Object.assign but it comes with support for object deep extension by default:

import { assign } from `super-assign`;

const object = {
  n: 123,
  s: 'foobar',
  o: {
    a: 1,
    b: 2,
  },
};
const updates = {
  hoge: 'hoge',
  o: {
    a: 0,
    c: 3,
  }
};

assign(object, updates);
// object → {
//   n: 123,
//   s: 'foobar',
//   hoge: 'hoge',
//   o: {
//     a: 0,
//     b: 2,
//     c: 3,
//   },
// }

Like Object.assign, every argument gets assigned and modify the first argument, which acts as the target:

import { assign } from `super-assign`;

const res = assign(target, source1, source2, ..., sourceN);
// res === target

Pure assignment

To avoid modifying the target, assignCopy is available also as a pure function:

import { assignCopy } from `super-assign`;

const newObject = assignCopy(obj1, obj2, obj3);
// newObject !== obj1

Field deletion

Sometimes it can be useful to delete fields when updating an object. While this could be achieved by assigning undefined or null values, there's an option to natively delete the desired fields.

By default there's a special value provided by the library to achieve this:

import { assign, ASSIGN_DELETE } from 'super-assign';

const object = {
  n: 123,
  s: 'string',
  o: {
    a: 1,
    b: 2,
    c: 3,
  },
};
assign(object, {
  // update object.s value
  s: 'updated',
  o: {
    // update object.o.a value
    a: 0,
    // delete object.o.b from origin
    b: ASSIGN_DELETE,
    // add new object.o.d field
    d: 4,
  },
});
// object → {
//   n: 123,
//   s: 'updated',
//   o: {
//     a: 0,
//     c: 3,
//     d: 4,
//   }
// }

Note: ASSIGN_DELETE is a Symbol to ensure a unique value and due to this it cannot be serialized (i.e. for http requests).

A custom value can provided to fit the cases where serialization is needed (see Customization).

import { getCustomAssign } from 'super-assign';

// `__DEL__` will be used now to delete fields
const DELETE = '__DEL__';
const assign = getCustomAssign({ deleteValue: DELETE });
assign({ a: 1, b: 2, c: 3 }, { b: DELETE });
// → { a:1, c:3 }

Usage notes

While objects can be extended, functions, Symbols and DOM elements will just be referenced so any modification to a field of these types in the extended object will affect as well as the values in the sources.

Arrays will be shallow copied (array modifications won't affect the source, but any array element will just be references)

const base = {};
const src = {
  n: 123,
  str: 'string',
  obj: { a: 1, b: 2 },
  arr: [{}, {}, {}],
  fn: () => {},
  dom: document.createElement('div'),
  sym: Symbol(),
};
const res = assign(base, src);
// base.n !== src.n
// base.str !== src.str
// base.obj !== src.obj
// base.arr !== src.arr
// base.arr[i] === src.arr[i]
// base.fn === src.fn
// base.dom === src.dom
// base.sym === src.sym

Customization

New assigners can be created with custom behavior via the getCustomAssign function:

import { getCustomAssign } from 'super-assign';

const customAssign = getCustomAssign({
  shallow: true,
  returnCopy: true,
});

Note that every time you call getCustomAssign a new function will be created. If the same function is going to be reused in several places it's recommended to memoize/store+share it.

/*
 * utils/assign.ts
 */
import { getCustomAssign } from 'super-assign';

export const myAssign = getCustomAssign({
  shallow: false,
  returnCopy: true,
  ignoreValues: [undefined, null],
});

/*
 * Usage from any/other/file.ts
 */
import { myAssign } from 'utils/assign';

const res = myAssign(obj1, obj2);

For those cases where a custom assignment is just required once, there's also available the assignWithOptions function, which works in the same way but accepts the customizable options as the first parameter and doesn't create any new function:

import { AssignOptions, assignWithOptions } from 'super-assign';

const options: AssignOptions = {
  returnCopy: true,
  shallow: true,
};
const res = assignWithOptions(options, src1, src2);

Customizable options

The list of customizable options are:

| Option | Default | Description | | ------------ | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ignoreValues | [undefined] | List of values to ignore. By default undefined values will not be assigned but null values will | | ignoreKeys | undefined | List of keys to ignore. None by default | | deleteValue | ASSIGN_DELETE | If defined, when a key has this value, the key will be deleted from the target object. Note that this is undefined the field deletion feature will be disabled | | returnCopy | false | If true the target object will not be modified | | shallow | false | If true there will be no deep assignment for objects |