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

value-object-js

v1.0.4

Published

Introduces value objects in JavaScript.

Downloads

15

Readme

Value Object JS

The Value Object is a usefull, well known design pattern in a lot of languages. JavaScript is not one of them, until now. This plugin allows you to create your own 'Data types' as value object in order to better structure your application logic.

Installation

Bower

bower install --save value-object-js

<script src="/bower_components/value-object-js/dist/value-object.min.js"></script>
<script>
	var Email = ValueObject.define('Email', function() { ... });
	
	// ...
</script>

Besides the value-object.js file, this package also provides a set of predefined value objects like DateRang, Email, URL, YoutubeVideoURL and more. This library can be loaded on its own (value-object-library.min.js) or as part of the main file (value-ibject.with-library.min.js).

NPM

npm install --save value-object-js

var ValueObject = require('value-object-js');

var Email = ValueObject.define('Email', function() { ... });

// ...

Usage

Define a value object

A value object only requires a name and a validation function testing the passed value.

var Email = ValueObject.define('Email', function(value) {
	return /(.+)@(.+){2,}\.(.+){2,}/.test(value);
});

Using the defined value object is now as simple as instantiating any object using the new keyword. Defined value objects can also be found in the library.

var customerEmail = new Email('example.domain.com'); // Throws InvalidArgumentException

var customerEmail = new Email('[email protected]'); // Works!

var customerEmail = new ValueObject.Library.Email('[email protected]'); // Works!

The value object can now be handled like you would a String object.

console.log(customerEmail); // logs an object

console.log('Hi! Mail me at: ' + customerEmail); // logs 'Hi! Mail me at: [email protected]'

Comparing value objects

Objects in javascript are equal on identity, not on value. Value objects, however, are equal on value and can be compared with strict equality. They even can be tested with instanceOf.

var email1 = new Email('[email protected]');
var email2 = new Email('[email protected]');

console.log(email1 === email2); // logs true
console.log(email1 instanceof Email); // logs true

Adding methods

An email address is basicly a string, so it would be helpful if we could use the same methods. This can be done by specifying which data type (or other value object!) should be extended in the definition. You can also add your own methods.

var Email = ValueObject.create('Email', {

	// Extends all methods from String (like substr, split, match, etc.)
	extends: 'String',
	
	validate: function(value) { // your validation here };
	
	getDomainPart: function() {
		return this.value.split('@').pop();
	}
});

var email = new Email('[email protected]');

console.log(email.substr(0, 7)); // logs 'example'
console.log(email.getDomainPart()); // logs 'domain.com'

Documentation

For full documentation see http://dev.tsoffereins.com/value-object-js/documentation/.

Support

You can reach me via Twitter: @tsoffereins

Please file issues here at GitHub.