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

jquery-extendext

v1.0.0

Published

jQuery.extend with configurable behaviour for arrays

Downloads

88,680

Readme

jQuery.extendext

npm version jsDelivr CDN Build Status

jQuery.extend with configurable behaviour for arrays.

Isn't $.extend good enough ?

Well, it's actually pretty good, and is generally sufficient, but it merges arrays in a strange way depending of what you want. Example:

var DEFAULTS = {
  operators: ['AND', 'OR', 'XOR']
};

var config = {
  operators: ['OR', 'XOR']
};

config = $.extend(true, {}, DEFAULTS, config);

When executing this code, one will expects to get config.operators = ['OR', 'XOR'], but instead you get ['OR', 'XOR', 'XOR], because $.extend merges arrays like objects as per spec.

Other deep merging utilities I found either have the same behaviour or perform both merge and append on array values (nrf110/deepmerge for example).

Usage

jQuery.extendext.js contains a new $.extendext function with the exact same behaviour as $.extend if not additional config is provided.

The difference is that it accepts a optional second string argument to specify how arrays should be merged.

jQuery.extendext([deep ,][arrayMode ,] target, object1 [, objectN ] )
  • deep boolean — If true, the merge becomes recursive (aka. deep copy).
  • arrayMode string — Specify the arrays merge operation, either replace, concat, extend or default
  • target object — The object to extend. It will receive the new properties.
  • object1 object — An object containing additional properties to merge in.
  • objectN object — Additional objects containing properties to merge in.

"replace" mode

In this mode, every Array values in target is replaced by a copy of the same value found in objectN. The copy is recursive if deep is true.

var DEFAULTS = {
  operators: ['AND', 'OR', 'XOR']
};

var config = {
  operators: ['OR', 'XOR']
};

config = $.extendext(true, 'replace', {}, DEFAULTS, config);

assert.deepEqual(config, {
  operators: ['OR', 'XOR']
}) // true;

"concat" mode

In this mode, Arrays found in both target and objectN are always concatenated. If deep is true, a recursive copy of each value if concatenated instead of the value itself.

var DEFAULTS = {
  operators: ['AND', 'OR', 'XOR']
};

var config = {
  operators: ['OR', 'XOR']
};

config = $.extendext(true, 'concat', {}, DEFAULTS, config);

assert.deepEqual(config, {
  operators: ['AND', 'OR', 'XOR', 'OR', 'XOR']
}) // true;

"extend" mode

This is how nrf110/deepmerge works. In this mode, Arrays values are treated a bit differently:

  • If plain objects are found at the same position in both target and objectN they are merged recursively or not (depending on deep option).
  • Otherwise, if the value in objectN is not found in target, it is pushed at the end of the array.
var DEFAULTS = {
  operators: ['AND', 'OR', 'XOR']
};

var config = {
  operators: ['XOR', 'NAND']
};

config = $.extendext(true, 'extend', {}, DEFAULTS, config);

assert.deepEqual(config, {
  operators: ['AND', 'OR', 'XOR', 'NAND']
}) // true;

"default" mode

Same as $.extend.

var DEFAULTS = {
  operators: ['AND', 'OR', 'XOR']
};

var config = {
  operators: ['OR', 'XOR']
};

config = $.extendext(true, 'default', {}, DEFAULTS, config);

assert.deepEqual(config, {
  operators: ['OR', 'XOR', 'XOR']
}) // true;

Tests

A jest test suite is provided in tests directory.

$.extendext is tested against core jQuery tests for $.extend and nrf110/deepmerge tests (with the difference that extendext, like extend, modifies the first argument where deepmerge does not touch it).