How to use the fromJS function from immutable

Find comprehensive JavaScript immutable.fromJS code examples handpicked from public code repositorys.

100
101
102
103
104
105
106
107
108
109
  getAt: (arr, idx) => arr[idx],
  setAt: (arr, idx, val) => timm.replaceAt(arr, idx, val),
};

_solImmutableJs = {
  init: () => Immutable.fromJS(INITIAL_OBJECT), // deep
  get: (obj, key) => obj.get(key),
  set: (obj, key, val) => obj.set(key, val),
  getDeep: (obj, key1, key2) => obj.getIn([key1, key2]),
  setDeep: (obj, key1, key2, val) => obj.setIn([key1, key2], val),
fork icon23
star icon265
watch icon8

+ 13 other calls in file

117
118
119
120
121
122
123
124
125
126
this.key = options.key || utils.generateRandomKey();

this._queuedChange = false;
this.current = options.data;
if (!isImmutableStructure(this.current) || !this.current) {
  this.current = Immutable.fromJS(this.current || {});
}

if (!!options.history) {
  this.history = Immutable.List.of(this.current);
fork icon21
star icon374
watch icon0

43
44
45
46
47
48
49
50
51
52
    obj.setIn(['data', 'value'], Math.random());
  }
};

exports.objectSetInImmutableJs = (cycles) => {
  const obj = ImmutableJs.fromJS({
    data: {value}
  });

  for (let i = 0; i < cycles; i++) {
fork icon12
star icon212
watch icon0

+ 27 other calls in file

84
85
86
87
88
89
90
91
92
93
const TreeUtils = require('immutable-treeutils');
// import TreeUtils from 'immutable-treeutils';

let treeUtils = new TreeUtils();

let data = Immutable.fromJS({
        id: 'root',
        name: 'My Documents',
        type: 'folder',
        childNodes: [
fork icon8
star icon77
watch icon3

208
209
210
211
212
213
214
215
216
217
},
hasIn: function (key = []) {
  return this.map.hasIn(key);
},
setIn: function (key = [], val) {
  this.map = this.map.setIn(key, Immutable.fromJS(val));
},
getIn: function (key = []) {
  const val = this.map.getIn(key);
  if (_.isUndefined(val) || val === null) { return null; }
fork icon0
star icon26
watch icon3

+ 27 other calls in file

13
14
15
16
17
18
19
20
21
22
23
24
25


var CHANGE_EVENT = 'change';


var explicitStepHistory = ["begin"]; // components, prescribable


var state = Immutable.fromJS({
  elt: 0,
  q: "",
  options: {
    "ingredient": [],
fork icon17
star icon13
watch icon8

+ 2 other calls in file

7
8
9
10
11
12
13
14
15
16

mimeReg.register(
    'application/json',
    {
        read(str) {
            return Immutable.fromJS(JSON.parse(str));
        },

        write(obj) {
            return JSON.stringify(obj.toJS());
fork icon4
star icon4
watch icon10

4
5
6
7
8
9
10
11
12
13
14
15
var sample = require('lodash/sample');


var denormalize = require('../src').denormalize;


var immutable = require('immutable');
var fromJS = immutable.fromJS;
var Map = immutable.Map;
var Record = immutable.Record;
var List = immutable.List;

fork icon3
star icon9
watch icon2

+ 8 other calls in file

29
30
31
32
33
34
35
36
37
38
set (key, value) {
  if (typeof key !== 'string') {
    throw new Error('key must be string.')
  }
  const pathArray = key.split('.')
  this.stateMap = this.stateMap.setIn(pathArray, Immutable.fromJS(value))
}

setIn (path, value) {
  if (!Array.isArray(path)) {
fork icon3
star icon5
watch icon1

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
  { id: 1, type: "default", value: "New course available" },
  { id: 2, type: "urgent", value: "New resume available" },
  { id: 3, type: "urgent", value: "New data available" }
];

const state = fromJS({
  filter: "DEFAULT",
  notifications: notificationsNormalizer([
    { id: 1, isRead: false, type: "default", value: "New course available" },
    { id: 2, isRead: false, type: "urgent", value: "New resume available" },
fork icon1
star icon2
watch icon1

-3
fork icon0
star icon2
watch icon3

+ 9 other calls in file

1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
	CSE: '^', 
	// replace
	REP: '='
}),
P: {
	Mod: Imm.fromJS({ CLR: '@', IDX: '#', COL: '?' }),
	Prm: {
		CLR: { C: '', B: '', S: '', F: '' },
		IDX: { P: '', '<': '', '>': '' },
		COL: { C: '', W: '', R: '' }
fork icon0
star icon1
watch icon2

+ 65 other calls in file

35
36
37
38
39
40
41
42
43
const fieldsToRemove = _.difference(prevFields, currentFields);
let newList = prevList;

fieldsToRemove.forEach((field) => {
  const index = newList.indexOf(field);
  const manager = new Manager(state, prevList, keys, index, fromJS(layout.attributes || {}));
  const attrToRemoveInfos = manager.attrToRemoveInfos; // Retrieve the removed item infos
  const arrayOfLastLineElements = manager.arrayOfEndLineElements;
  const isRemovingAFullWidthNode = attrToRemoveInfos.bootstrapCol === 12;
fork icon0
star icon1
watch icon1

+ 4 other calls in file

143
144
145
146
147
148
149
150
151
152
	case "ADD_SIMPLE":
		return state.push(Immutable.fromJS(action.entity))
	case "REMOVE_SIMPLE":
		return state.filter(item => item[action.keyField] !== action.key);
	case "UPDATE_SIMPLE":
		return Immutable.fromJS([{ id : 235235, x : 5.23, y : 9, state : "complete" }]);
	default:
		return state;
	}
}
fork icon0
star icon1
watch icon0

+ 6 other calls in file

31
32
33
34
35
36
37
38
39
40
```

## Nested maps

```js
var nested = Immutable.fromJS({ user: { profile: { name: 'John' } } })

nested
  .mergeDeep({ user: { profile: { age: 90 } } })
  .setIn([ 'user', 'profile', 'name' ], 'Jack')
fork icon0
star icon1
watch icon2

+ 3 other calls in file

16
17
18
19
20
21
22
23
24
const ui         = bs.ui;
const optPath    = config.OPT_PATH;
var pluginActive = true;
const itemsPath  = config.OPT_PATH.concat('items');
const items   = opts.routes && opts.routes.length
    ? Immutable.fromJS(opts.routes)
    : Immutable.List([]);

var mutableItems = [];
fork icon0
star icon2
watch icon3

1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
/**
 * Updates a modules filters
 * @return Object
 */
function updateFilters(state, filters) {
  return state.mergeDeep({ filters: Immutable.fromJS(filters) });
}


/**
 * Clears a modules filters
fork icon1
star icon0
watch icon2

+ 9 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15


var _ = require('lodash');
const { Map, fromJS } = require('immutable');


describe('Test courseReducer.js', () => {
  const state = fromJS(coursesNormalizer([
    { id: 1, name: "ES6", isSelected: false, credit: 60 },
    { id: 2, name: "Webpack", isSelected: false, credit: 20 },
    { id: 3, name: "React", isSelected: false, credit: 40 }
  ]));
fork icon0
star icon0
watch icon1

604
605
606
607
608
609
610
611
612
613

const initialProfile = Immutable.fromJS({
  bio: 'Working on Awesome Game'
});

const replacementProfile = Immutable.fromJS({
  bio: 'Working on Design'
});

const profileWithId = Immutable.fromJS({
fork icon0
star icon0
watch icon2

+ 224 other calls in file

515
516
517
518
519
520
521
522
523
524
 * Create instance of the model
 * @param {Object} properties
 * @returns {Model}
 */
_factory(properties) {
  const immutable = Immutable.fromJS(properties);
  
  if (this.args.type && typeof this.args.type.factory === 'function') {
    return this.args.type.factory(immutable);
  } else {
fork icon0
star icon0
watch icon2

+ 8 other calls in file