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

jsprops

v0.3.1

Published

Properties for JavaScript Prototypes. Class based. Extended by a Signal implementation.

Downloads

3

Readme

jsprops

Properties for JavaScript Prototypes.

Library provides class based properties that can be bound directly to a prototype, which reduces redundancy of coping the same property among instances.

Additionally, we can easily inherit from Properties class, extending it by our own getters/setter logic.

Default property's value is inited in a lazy way, first time it's accessed.

Example

{ property } = require 'jsprops'

class Klass
	foo: property 'foo'
	bar: property('bar', null, 'def_value')
	baz: property('baz',
		set: (set, val) ->
			set val.replace /a/, 'b'
	)
	woof: property('woof',
		init: (set) -> set null
		get: (get) -> get().replace /z/, 'b'
		set: (set, val) ->
			set val.replace /a/, 'b'
		'dev_value'
	)

instance = new Klass
instance.foo() # returns 'foo'
instance.foo 'bar'
instance.foo() # returns 'bar'

Repeated name in a declaration is required by the design.

Signals

Signals are extended properties, adding nice bindings to an event emitter. An interesting feature is the support for inheritance. You can define an overriding signal in a child class, while still preserving listener defined in the super class. The only condition is the requirement of using @signal instead of signal (needed for a prototype chain resolution).

For signals, set is an emit and get returns temp object with binding functions (on, once) and an init function. All of them can be defined inside a declaration. They are also compatible with EventEmitter2Async (with callback and after/before events). Additionally, each signal needs to be initialized (manually or using SignalsMixin#initSignals).

Example

{
	signal
	SignalsMixin
} = require 'jsprops'

# This example actually lacks event emitter, but mixins are out of it's scope.
class Klass extends SignalsMixin
	foo: signal('foo',
		on: -> console.log 'klass1'
	)

	on: -> # forward to a composed event emitter
	emit: -> # forward to a composed event emitter

class Klass2 extends Klass
	constructor: ->
		@initSignals()

	foo: @signal('foo',
		on: -> console.log 'klass2'
	)

instance = new Klass
instance.foo().on ->
	console.log 'listener'
instance.foo() # prints 'klass1', 'klass2', 'listener'

JavaScript examples

property = require('jsprops').property

function Klass() {}

Klass.prototype.foo = property('foo')
Klass.prototype.bar = property('bar', null, 'def_value')
Klass.prototype.baz = property('baz', {
	set: function(set, val) {
		set(val.replace(/a/, 'b'))
	}
})
Klass.prototype.baz = property('woof', {
		init: function(set) {
			set(null)
		},
		get: function(get) {
			return get().replace(/z/, 'b')
		},
		set: (set, val) ->
			set(val.replace(/a/, 'b'))
	}, 'dev_value'
)

Contracts

This library provides contracts (read - runtime type checking) based on contracts.coffee library. There's a special build for that, so don't worry when using the default one. To make it work, a flag global.contracts is needed.