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

lighter-type

v2.2.1

Published

A lightweight JavaScript inheritance utility.

Downloads

154

Readme

lighter-type

Chat Version Downloads Build Coverage Style

The lighter-type module is a prototypal inheritance utility with better performance than similar inheritance utilities.

It supports:

  • Constructors
  • Prototypal inheritance
  • Multiple inheritance
  • Non-enumerable property definitions

Installation

From your project directory, install and save as a dependency:

npm install --save lighter-type

Usage

The lighter-type module outputs a constructor with several methods.

Type.extend([constructor], [prototypeProps], [constructorProps])

Define and return a sub type of the Type object, with a prototype decorated with optional prototypeProps (a map of additional prototype properties) and optional constructorProps (a map additional type properties. The sub type itself also inherits the properties of its super type (such as the extend method).

When the prototypeProps argument includes a property called init, it is used as the constructor for the sub type rather than being added as a prototype property.

var Type = require('lighter-type')

// Construct a new person with a name.
var Person = Type.extend(function Person (name) {
  this.name = name
}, {
  // Give a person a default salutation of "Hello".
  salutation: 'Hello',

  // Greet a person with their defined salutation.
  greet: function () {
    console.log(this.salutation + ', ' + this.name + '!')
  }
}, {
  // Identify this type, if asked.
  getTypeName: function () {
    return 'Person'
  }
})

// Make a Friend sub type by extending the Person type.
var Friend = Person.extend({
  // Be a bit more informal with friends.
  salutation: 'Hi'
}, {
  // Identify this type, if asked.
  getTypeName: function () {
    return 'Friend'
  }
})

// Instantiate Bob, and greet him.
var bob = new Person('Bob')
bob.greet()
//> "Hello, Bob!"

// Instantiate Joe, and greet him.
var joe = new Friend('Joe')
joe.greet()
//> "Hi, Joe!"

// Make sure a Friend is a Friend.
console.log(Friend.getTypeName())
//> "Friend"

Each type's prototype has _super property which references its parent prototype, and each type has a _super property which references its parent type.

var Type = require('lighter-type')

var Robot = Type.extend({
  bend: function (object) {
    object.isBent = true
  }
})

var Bender = Robot.extend({
  bend: function (object) {
    this._super.bend(object)
    console.log('Bite my shiny metal ass.')
  }
})

var beam = {}
var bender = new Bender()
bender.bend(beam)
//> Bite my shiny metal ass.

console.log(beam.isBent)
//> true

Type.init(object[, overwrite][, args])

Decorate an object with the prototype of a Type, and call its constructor on the object with an args array, unless args is false, in which case the constructor will be skipped.

Any object that extends Type (such as lighter-emitter) will inherit Type.init and other Type methods.

// The `lighter-emitter` module extends `lighter-type`.
var Emitter = require('lighter-emitter')

// Make the `console` object into an emitter.
Emitter.init(console)

console.on('hi', function (name) {
  console.log('Hi, ' + name + '!')
})

console.emit('hi', 'Sam')
//> Hi, Sam!

Type.decorate(object[, map][, overwrite])

Decorate an object with a map of additional properties (or overriding properties if overwrite is truthy). If the map is not specified, the Type prototype will decorate the object prototype instead.

var Type = require('lighter-type')

// Add a few methods to the Array object's prototype.
Type.decorate(Array.prototype, {
  first: function () {
    return this[0]
  },
  last: function () {
    return this[this.length - 1]
  },
  sum: function () {
    var s = 0
    for (var i = 0, l = this.length; i < l; i++) {
      s += this[i]
    }
    return s
  }
})

// Create a plain old array of numbers.
var a = [1, 2, 3]

console.log(a.first())
//> 1

console.log(a.last())
//> 3

console.log(a.sum())
//> 6

Type.include(type[, overwrite])

Implement multiple inheritance by decorating one Type's prototype with the prototype properties of another.

var Type = require('lighter-type')

// A vehicle might work on land or water.
var Vehicle = Type.extend({

  // Return isLandVehicle as a boolean.
  worksOnLand: function () {
    return !!this.isLandVehicle
  },

  // Return isWaterVehicle as a boolean.
  worksOnWater: function () {
    return !!this.isWaterVehicle
  }

})

// A car is a land vehicle.
var Car = Vehicle.extend({
  isLandVehicle: true
})

// A boat is a water vehicle.
var Boat = Vehicle.extend({
  isWaterVehicle: true
})

// A hovercraft is also a vehicle.
var Hovercraft = Vehicle.extend({})

// A hovercraft includes the functionality of a Car and a Boat.
Hovercraft.include(Car)
Hovercraft.include(Boat)

// Create a new Hovercraft.
var hovercraft = new Hovercraft()

console.log(hovercraft.worksOnLand())
//> true

console.log(hovercraft.worksOnWater())
//> true

Type.is(type)

Check whether this Type is descended from another Type.

var Foo = Type.extend({})
var Bar = Foo.extend({})
var Baz = Bar.extend({})

console.log(Baz.is(Foo))
//> true

console.log(Foo.is(Baz))
//> false

Type.has(type)

Check whether this Type has acquired the functionality of another type via the extend method or the include method.

var Type = require('lighter-type')

// Create an object with a visible (i.e. enumerable) method.
var object = {
  visible: function () {
    console.log('I am visible.')
  }
}

// Add a non-enumerable property called "hidden".
Type.hide(object, 'hidden', function () {
  console.log('I am hidden.')
})

// Verify that the "hidden" method exists.
object.hidden()
//> "I exist."

// Verify that only the "visible" method is enumerable.
for (var key in object) {
  console.log(key)
}
//> "visible"

Type.hide(object, key, value)

Create a property of object named key with value value, and "hide" it by making it non-enumerable.

var Type = require('lighter-type')

// Create an object with a visible (i.e. enumerable) method.
var object = {
  visible: function () {
    console.log('I am visible.')
  }
}

// Add a non-enumerable property called "hidden".
Type.hide(object, 'hidden', function () {
  console.log('I am hidden.')
})

// Verify that the "hidden" method exists.
object.hidden()
//> "I exist."

// Verify that only the "visible" method is enumerable.
for (var key in object) {
  console.log(key)
}
//> "visible"

More on lighter-type...