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

@mdaemon/items-model

v2.1.0

Published

A basic items manipulation model

Downloads

95

Readme

Dynamic JSON Badge Static Badge install size Dynamic JSON Badge Node.js CI

@mdaemon/items-model, A basic items Map model library with TypeScript support

Includes MDaemon's Emitter library as a dependency

[ @mdaemon/items-model on npm ]

The "items-model" provides basic methods for manipulating an array of objects based on a constructor

Install

  $ npm install @mdaemon/items-model --save  

Node CommonJS

    const ItemsModel = require("@mdaemon/items-model/dist/items-model.cjs");

Node Modules

    import ItemsModel from "@mdaemon/items-model/dist/items-model.mjs";  

Web

    <script type="text/javascript" src="/path_to_modules/dist/items-model.umd.js"></script>
    <!-- necessary for versions < 2.0.0 -->
    <script type="text/javascript">window.ItemsModel = window["items-model"];</script>

ItemsModel

    
  class Car {
    id = 0;
    make = "";
    model = "";
    color = "";
  }
  
  class Cars extends ItemsModel {
    constructor() {
      super({
        itemConstructor: Car,
        itemName: "Car"
      });
    }
  }
  
  // or
  
  // if you do not include an id attribute like below, a numbered id will be assigned
  function Car(config) {
    this.make = "";
    this.model = "";
    this.color = "";

    Object.assign(this, config);
  }
    
  function Cars() { 
    Object.assign(this, new ItemsModel({
      itemConstructor: Car,
      itemName: "Car"
    }));
  }
    
  const carsModel = new Cars();

  // returns the name set for the model items
  carsModel.getName(); // "Car";

  // empties the internal items Map
  carsModel.clear(); 
    
  // adds a new Car to the items Map with only the config object
  carsModel.add({ make: "Honda", model: "Element", color: "gray" });
  // emits "added-Car" and "indexed-Car"
  
  // gets the internal items Map
  carsModel.getAll(); // [Car]

  // gets all the ids from the items Map
  carsModel.getAllIds();

  // gets a copy of the internal items Map, so that manipulation of the items in the array do not impact the internal array
  carsModel.getCopies(); // [Car]

  // gets a copy of the requested item by id, so that manipulation of the item does not impact the internal item
  carsModel.getCopy(0); // Car
  carsModel.getCopy(1); // null

  // gets an item from the internal items Map based on the id
  carsModel.getById(0); // Car
  carsModel.getById(1); // null

  // gets the index of a given item in the internal array
  carsModel.getIndex(0); // 0
  carsModel.getIndex(1); // -1

  // gets the first item from the internal items Map based on an attribute/value combination
  carsModel.getByAttribute("make", "Honda"); // Car
  carsModel.getByAttribute("model", "Odyssey"); // null

  carsModel.getFirstByAttribute("make", "Honda"); // Car -- this is an alias for getByAttribute

  // gets all the items from the internal array based on an attribute/value combination
  carsModel.getAllByAttribute("make", "Honda"); // [Car]
  carsModel.getAllByAttribute("model", "Odyssey"); // []

  // sets the values passed in an object based on the id
  // returns success true or false
  carsModel.setAttributes(0, { model: "Odyssey", color: "blue" }); // true
  carsModel.setAttributes(1, { model: "Odyssey" }); // false

  // sets the values for all matching items
  // returns an array of objects that use the id and success true or false
  carsModel.setAttributesByAttr("model", "Odyssey", { color: "gray" }); // [{ 0: true }]

  // inserts or updates the passed items after the parent id
  // returns success true or false
  carsModel.insert(0, [{ make: "Toyota", model: "Camry", color: "tan" }]); // true
  carsModel.insert(2, [{ make: "Toyota", model: "Carolla", color: "red" }]); // false because a parent of id 2
  //emits "inserted-Car"

  // upsert is an alias for insert
  carsModel.upsert(1, [{ id: 0, model: "Element" }]); // true

  // similar to set attributes, except the full object is expected (including the id)
  // returns success true or false
  carsModel.update({ id: 1, make: "Toyota", model: "Camry", color: "brown" }); // true
  // emits "updated-Car"
  
  // removes the item from the internal items Map based on the id
  // returns success true or false
  carsModel.remove(1); // true
  carsModel.remove(2); // false
  // emits "removed-Car"

v2.0.0

Breaking change: Instead of window["items-model"], window.ItemsModel must be used for umd builds.

See @mdaemon/emitter for how to details on the event emitter

License

Published under the LGPL-2.1 license.

Published by MDaemon Technologies, Ltd. Simple Secure Email https://www.mdaemon.com