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

extend-module

v0.4.1

Published

Extend given module(s) with specified object. Use for extending object modules that you require and install as npm packages.

Downloads

3

Readme

Extend Module

Extend a NPM module(s) by requiring and then merging with the given object(s).

Why?

Often I find myself writting code that does the same thing and writting the code elsewhere and then merging those common parts can save time on both development and maintenance.

Installation

Installation is simple:

$ npm i extend-module

Usage

In Sails you can define your base model as follows:

eg: api/base/models/Details.js base model

module.exports = {
  attributes: {
    name: {
        type: 'string',
    },
    age: {
         type: 'number',
    },
  },
}

An extending model can then use this package to extend the above base model as follows:

eg: api/models/Person.js model

const extend = require('extend-module');

module.exports = extend('../base/models/Details', {
  attributes: {
    name: {
        description: 'the name of the person',
        required: true,
    },
    sex: {
         type: 'string',
    },
  },
});

The resulting model will be as follows:

eg: resulting model

{
  attributes: {
    name: {
        type: 'string',
        description: 'the name of the person',
        required: true,
    },
    age: {
        type: 'number',
    },
    sex: {
         type: 'string',
    },
  },
}

Using the same method other models can inherit the same base attributes by extending the base details model.

Tips:

  • You can use actual NPM packages to create your base models and use them in different apps:
    • ... extend('my-base-model', {...});
    • In that example my-base-model is a NPM package installed as a dependency using NPM ie: npm i my-base-model.
  • You can specify multiple modules to require by passing an array of module names or paths:
    • ... extend(['my-base-model', 'my-other-base-model', ... ], {...});
    • Each module will extend the one before it in the order of left to right
    • The final module will be a merge of all the fiven modules
  • You can specify multiple extending objects, all of them will extend the given module(s):
    • ... extend(['my-base-model', 'my-other-base-model', ... ], {...}, {...}, {...}, ... );
  • To extend using only modules pass in an empty extending object:
    • ... extend(['my-base-model', 'my-other-base-model', ... ], {});
    • The modules will extend each other as usual, but the extending object will just be nothing.
    • It works the same if you pass just the first param ... extend(['my-base-model', 'my-other-base-model', ... ]);

Author

Emmanuel Mahuni. (c) 2018 MIT

Attributions

Andy Sutherland Sails Model Extension Example