├── .gitignore ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Immutable functions 3 | =================== 4 | 5 | This library provides a wrapper around the package 6 | [immutable](https://github.com/facebook/immutable-js) exposing the same API but 7 | adding static functions to the data types that are exactly the same as the ones 8 | in **immutable** but accepting the object to act on as the last argument. 9 | 10 | Usage 11 | ----- 12 | 13 | This package has immutable as its peer dependency, so you have to install it 14 | too. 15 | 16 | ``` 17 | npm install --save immutable immutable-fns 18 | ``` 19 | 20 | Docs 21 | ---- 22 | 23 | Check out [immutable's API](https://github.com/facebook/immutable-js), this 24 | library provides exactly the same but the object methods have been converted 25 | into static methods on the class taking the object to act on as the last 26 | argument. 27 | 28 | For example: 29 | 30 | ```javascript 31 | // On the immutable-js api 32 | Immutable.Map({}).get('key') 33 | // Added on this library 34 | Immutable.Map.get('key', map) 35 | ``` 36 | 37 | Examples 38 | -------- 39 | 40 | ```javascript 41 | var Immutable = require('immutable-fns'); 42 | var get = Immutable.Map.get; 43 | 44 | // Create an immutable data structure 45 | var m = Immutable.Map({a: 1}); 46 | 47 | // We can use the traditional api (obj.method(param)) or use the functional API 48 | // (fn(...params, obj)) 49 | assert.equal(m.get('a'), get('a', m)); 50 | ``` 51 | 52 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | var Immutable = require('immutable'); 3 | 4 | exports.is = Immutable.is; 5 | exports.fromJS = Immutable.fromJS; 6 | 7 | var types = [ 8 | 'Sequence', 9 | 'Range', 10 | 'Repeat', 11 | 'Vector', 12 | 'Map', 13 | 'OrderedMap', 14 | 'Set', 15 | 'Record' 16 | ]; 17 | 18 | function unbind(method) { 19 | return function () { 20 | var args = [].slice.call(arguments); 21 | var obj = args.pop(); 22 | return method.apply(obj, args); 23 | }; 24 | } 25 | 26 | types.forEach(function(typeStr) { 27 | var Type = Immutable[typeStr]; 28 | var proto = Type.prototype; 29 | 30 | exports[typeStr] = Type; 31 | for (var prop in proto) { 32 | var method = proto[prop]; 33 | if (typeof method === 'function') { 34 | exports[typeStr][prop] = unbind(method); 35 | } 36 | } 37 | }); 38 | 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "immutable-fns", 3 | "version": "0.0.3", 4 | "description": "Immutable js functions for immutable js data structures", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test/index.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "http://github.com/joakin/immutable-fns" 12 | }, 13 | "keywords": [ 14 | "immutable", 15 | "fp", 16 | "functional" 17 | ], 18 | "author": "Joaquin Oltra", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/joakin/immutable-fns/issues" 22 | }, 23 | "homepage": "https://github.com/joakin/immutable-fns", 24 | "peerDependencies": { 25 | "immutable": "^2.0.4" 26 | }, 27 | "devDependencies": { 28 | "tape": "^2.14.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | var test = require('tape'); 3 | 4 | var Immutable = require('../index.js'); 5 | 6 | test('Exposes Immutable data structures', function(t) { 7 | var m = Immutable.Map({a: 1}); 8 | t.equal(m.get('a'), 1); 9 | t.end(); 10 | }); 11 | 12 | test('Exposes Immutable static functions for data structures', function(t) { 13 | var m = Immutable.Map({a: 1}); 14 | var get = Immutable.Map.get; 15 | t.equal(get('a', m), 1); 16 | t.end(); 17 | }); 18 | --------------------------------------------------------------------------------