Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

blimpy

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

blimpy

Easily set the class of an object.

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

blimpy blimp

If you need to quickly assign a prototype to an object,
such as one from a database, blimpy is the best way to do it.

Why?

Most databases don't store an object's prototype. This means if you're doing something like

firebaseRef.set(someFoodObject);

and then retrieving it later, it won't have the methods from FoodClass.

Using blimpy makes it easy to add a prototype to an object.

  • blimpy is small (blimpy.js is less than 1000 bytes).
  • blimpy is simple (blimpy only uses 2 built-in JS functions).
  • blimpy is well-tested (blimpy has 8 tests using MochaJS)

Sample Usage

  1. Get some object. It doesn't matter where from.

    • …from a database…

      let foodObject = getFoodObjectFromDatabase('apple');
      
    • …or with an object literal…

      let foodObject = { name: 'apple', price: 5 };
      
  2. Use blimpy.

    let food = blimpy.withClass(FoodClass, foodObject);
    
  3. Profit. (Use the class's methods.)

    food.eat();
    console.log(food.getPriceStr());
    

Install

npm install --save blimpy

Full Docs

withClass

Adds a class's prototype to an object (without mutating it).

blimpy.withClass(someClass, someObject)

  • someClass: The class to add.
  • someObject: The object to add it to.
  • Returns: The new object with the class's prototype.
withNoClass

Removes an object's prototype (without mutating it). Actually, this sets the object's prototype to Object.prototype (the default object prototype).

blimpy.withNoClass(someObject)

  • someObject: The object to remove the class from.
  • Returns: The new object without a prototype.

Full Example File

let blimpy = require('blimpy');

// Arbitrary class.
class FoodClass {
    constructor(name) {
        this.name = name;
        this.price = name.length;
    }

    getPriceStr() {
        return '$' + this.price.toString();
    }
}

// Some food object without the `Food` class prototype, in this case
// an object literal.
let foodObject = {
    name: 'apple',
    price: 5,
}

// Add the prototype with blimpy.
let food = blimpy.withClass(FoodClass, foodObject);

// Logs `$5`.
console.log(food.getPriceStr());

FAQs

Package last updated on 27 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc