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

value-object

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

value-object

value-object.js - simple value objects

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
195
increased by20.37%
Maintainers
1
Weekly downloads
 
Created
Source

value-object.js

Value Object - objects that matter only as the combination of their properties. Two value objects with the same values for all their properties are considered equal.

This library provides a convenient way to define strict, immutable value objects represented by ES6 classes.

Install

npm install value-object

Defining value objects

Use subclasses to define value objects with type constraints:

const ValueObject = require('value-object')

class Currency extends ValueObject.define({
  code: 'string',
  name: 'string'
}) {}

class Money extends ValueObject.define({
  currency: Currency,
  amount: 'number'
}) {}

Instantiating value objects

Use the new keyword:

const gbp = new Currency({ code: 'GBP', name: 'British Pounds' })
const price = new Money({ currency: gbp, amount: 12.34 })

The type constraints prevent value objects from being instantiated with incorrect arguments:

new Currency({ code: 'USD', name: 123 })
// => TypeError: Currency(code:string, name:string) called with wrong types (code:string, name:number)

new Currency({ code: 'NZD', name: 'New Zealand Dollars', colour: 'All black' })
// => TypeError: Currency({code, name}) called with {code, name, colour}

> new Money({ amount: 123 })
// => TypeError: Money({currency, amount}) called with {amount}

Equality

Value objects are considered to be equal if their properties are equal:

gbp.isEqualTo(new Currency({ code: 'GBP', name: 'British Pounds' }))
// => true

gbp.isEqualTo(new Currency({ code: 'EUR', name: 'Euros' }))
// => false

Creating new value objects from existing value objects

Use with(newAttributes) to create new value objects

var salePrice = price.with({ amount: 12.00 })
salePrice.currency.code
// => 'GBP'

Converting value objects to JSON

Use toJSON() to create an object that can be passed to JSON.stringify()

JSON.stringify(gbp.toJSON())
// => '{"__type__":"Currency","code":"GBP","name":"British Pounds"}'

Converting value objects from JSON

Use ValueObject.deserializeForNamespaces() to create a deserialize function that can turn the resulting JSON back into objects

const deserialize = ValueObject.deserializeForNamespaces([{ Currency }])
const gbp2 = deserialize('{"__type__":"Currency","code":"GBP","name":"British Pounds"}')
gbp2.isEqualTo(gbp)
// => true

Immutability

Value objects cannot be updated. Use strict mode: to throw errors when attempts to set property values are made.

gbp.code = 'USD'
// TypeError:Cannot assign to read only property 'amount' of object '#<Currency>

Keywords

FAQs

Package last updated on 10 Mar 2017

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