Socket
Socket
Sign inDemoInstall

immutable

Package Overview
Dependencies
5
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    immutable

immutable data-structures in javascript.


Version published
Maintainers
1
Install size
2.91 MB
Created

Package description

What is immutable?

The immutable npm package provides persistent immutable data structures that do not change once created, enabling advanced memoization, change detection, and functional programming techniques. It offers a variety of data structures such as List, Map, Set, and Record.

What are immutable's main functionalities?

Persistent Immutable List

Create and manipulate immutable lists, where modifications return new lists without altering the original.

const { List } = require('immutable');
const list1 = List([1, 2, 3]);
const list2 = list1.push(4);
console.log(list1.size); // 3
console.log(list2.size); // 4

Persistent Immutable Map

Create and manipulate immutable maps, with key-value pairs. Changes produce new maps without changing the original.

const { Map } = require('immutable');
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);
console.log(map1.get('b')); // 2
console.log(map2.get('b')); // 50

Persistent Immutable Set

Create and manipulate immutable sets, which are collections of unique values. Adding existing values does not change the set.

const { Set } = require('immutable');
const set1 = Set([1, 2, 3]);
const set2 = set1.add(3).add(4);
console.log(set1.has(3)); // true
console.log(set2.has(4)); // true

Record Factory

Define 'Record' factories to create immutable records with default values and specific shapes.

const { Record } = require('immutable');
const Person = Record({ name: null, age: null });
const person1 = new Person({ name: 'Alice', age: 30 });
const person2 = person1.set('name', 'Bob');
console.log(person1.name); // Alice
console.log(person2.name); // Bob

Other packages similar to immutable

Readme

Source

Warning: API Unstable (even more so than most < 1.0.0 releases)

immutable

immutable neatly packages immutable equivalents to JavaScript's Objects and Arrays.

Why?

Mutability causes headaches; immutability soothes them. JavaScript's Object and Array are crying out for immutable counterparts to complement first-class functions.

Support

Current support is limited to ECMAScript 5 compliant environments; although ECMAScript 3 compliance is a goal of this project.

browser support

Example

var i = require('immutable'),
    person = i.object({ firstName: 'hugh', secondName: 'jackson' })

var personWithAge = person.set({ age: 24 })

person.has('age')          //= false
personWithAge.has('age')   //= true
personWithAge.get('age')   //= 24

Install

npm install immutable

immutable.object([Object]) -> object

Creates an empty object, or sets the attributes if an object is passed.

var o = i.object()

// or
var you = i.object({ wise: true, willUseThisLib: true })

.set(String, Value) OR .set(Object) -> object

Returns a new object with the additional attribute(s).

var o = i.object()

var changed = o.set('x', 3).set({ y: 4, z: 5 })

changed.has('x') //= true
changed.get('y') //= 4

.get(String) -> value

Gets an attribute.

var o = i.object({ x: 3, y: 4 })

o.get('x') //= 3

.has(String) -> Boolean

Returns true or false; same as key in object for regular objects:

var o = i.object({ x: 3, y: 4 })

o.has('x') //= true
o.has('z') //= false

.remove(String) alias: .delete(String) -> object

Returns a new object with the key removed.

var o = i.object({
    foo: 'bar',
    baz: 'quux'
})

var updated = o.remove('foo').remove('baz')
updated.has('foo') //= false
o.has('foo')       //= true

.transient() -> Object

Returns a seperate, mutable object with the same attrs.

var o = i.object({
    foo: 'bar',
    baz: 'quux'
})

var trans = o.transient()
delete trans.foo

o.has('foo') //= true

immutable.array(Array) -> array

Shares the same API as immutable.object, except:

.transient() -> Array

Returns a seperate, mutable array with the same attrs.

var arr1 = i.array([1, 2, 3]),
    arr2 = arr1.transient()

arr2.splice(1)

arr1[1] !== arr2[1] //= true

Native Methods

The following native methods return a new instance of i.array:

  • map
  • sort
  • filter
  • splice
  • slice
  • reverse
  • concat
  • pop
  • push
  • shift
  • unshift

The following native methods work as expected for a regular array:

  • toString
  • toLocaleString
  • join
  • forEach
  • indexOf
  • lastIndexOf
  • every
  • some
  • reduce
  • reduceRight

Resources

Based on Bagwell (2001), and Clojure's immutable implementation of a Hash Array Mapped Trie.

Keywords

FAQs

Last updated on 28 Feb 2013

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc