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

strict-extend

v1.1.0

Published

A shallow extend method that removes extra properties from the target object.

Downloads

11

Readme

strict-extend

A shallow extend method that removes extra properties from the target object.

Inspired by jQuery's .extend() and Object.assign(), but any properties on the target argument that aren't on any of the source arguments will be removed. Useful for filling in defaults for options parameters.

Installation

This package is available on npm as strict-extend.

npm install strict-extend

Usage

Syntax

strictExtend(target, ...sources)

Parameters

target: The target object.

sources: The source object(s).

Description

strictExtend will return a new object containing all of the properties from all of the sources object(s). Any properties already present on target will not be overwritten. However, any properties present on target which are not present on any of the sources object(s) will not be copied to the return object. Values for properties which are on multiple sources objects will be resolved by taking the value from the last source object.

Note that an error will not be thrown for null or undefined parameters. Instead, an empty object will be returned.

Examples

Default Options

import strictExtend from 'strict-extend'; // or const strictExtend = require('strictExtend');

const defaults = {
    color: 'red',
    name: 'Clifford',
};

function dog(options) {
    return strictExtend(options, defaults);
}

console.log(dog()); // { color: 'red', name: 'Clifford' }
console.log(dog({ color: 'blue', name: 'Sparky'})); // { color: 'blue', name: 'Sparky' }
console.log(dog({ name: 'Rover', hasCollarOn: true })); // { color: 'red', name: 'Rover' }
// 'hasCollarOn' was ignored

Multiple Sources and Last In Behavior

const strictExtend = require('strict-extend') // or import strictExtend from 'strict-extend';

const target = {
    kept: 'should not be overwritten',
    removed: 'should NOT be copied'
};
const source1 = {
    kept: 'this will be ignored',
    newProp1: 'this will be copied',
    newProp2: 'this value will be ignored due to the property on source2'
};
const source2 = {
    newProp2: 'last in wins!'
};
console.log(strictExtend(target, source1, source2));
//  {
//      kept: 'should not be overwritten', (copied from target)
//      newProp1: 'this will be copied', (copied from source1)
//      newProp2: 'last in wins!', (copied from source2)
//  }
//  notice 'removed' from target not copied

null/undefined Behavior

import strictExtend from 'strict-extend';
console.log(strictExtend()); // {}
console.log(strictExtend(null)); // {}
console.log(strictExtend(null, null)); // {}
console.log(strictExtend({}, null )); // {}
console.log(strictExtend(null, {})); // {}

Development

Install

  1. Clone the repository.
  2. Run npm install in the directory you cloned into.

Tests

npm test assumes you have Chrome and Firefox installed. If that is not the case, you can run npm run test-ci to only run tests with PhantomJS (which is a dev dependency). Or, you can run the tests and specify which browsers you'd like to test with using the --browsers option with karma start. For example, to test only with Firefox, you can run karma start --browsers Firefox.

License

Licensed under the MIT License.