Skip to content

Instantly share code, notes, and snippets.

@HellMagic
Last active March 23, 2016 08:20
Show Gist options
  • Save HellMagic/2326d26eaf0438c2c233 to your computer and use it in GitHub Desktop.
Save HellMagic/2326d26eaf0438c2c233 to your computer and use it in GitHub Desktop.
临时收集的一些Immutable库的高级使用例子,需要整理一下~
var list = Immutable.fromJS([
{id:'A', label: 'A', checked: true},
{id:'B',label: 'B'},
{id:'C',label: 'C',
children: [
{id:'C-A',label: 'C-A'},
{id:'C-B',label: 'C-B', checked: true},
{id:'C-C',label: 'C-C',
children: [
{id:'C-C-A',label: 'C-C-A'},
{id:'C-C-B',label: 'C-C-B'},
{id:'C-C-C',label: 'C-C-C', checked: true}
]
}
]
},
{id:'D',label: 'D'}
]);
function mapper(item) {
if (item.has('children')) { item = item.get('children').map(mapper); }
if (item.get('id') === keyToChange) {
return item.set('checked', false);
}
return item;
}
var keyToChange = 'C-C-C';
var list2 = list.map(mapper);
console.dir(list2.toJS());
import {Record} from 'immutable'
import {getUserId} from '../user/store'
export const RoomType = {
// Private room only for two contacts.
CONTACT: 'contact',
// Private room, available and visible only for members.
PRIVATE: 'private',
// Public room, available and visible for everyone.
PUBLIC: 'public'
}
const RoomRecord = Record({
createdAt: 0,
createdByUserDisplayName: '',
createdByUserId: '',
description: '',
id: '',
isArchived: false,
name: '',
type: RoomType.PUBLIC,
urlName: ''
})
//---------------------------------------------
export default class Room extends RoomRecord {
get isContact() {
return this.type === RoomType.CONTACT
}
get contactId() {
// TODO: Add invariant for this.type != RoomType.CONTACT.
// Contact room id consists of two user ids joined with space. This makes
// contact room id always unique for two same users.
const compositeKey = window.atob(this.id)
const contactId = compositeKey.replace(getUserId(), '').trim()
return contactId
}
}
//-------------------------------------------------------------------
var state = {
id: 1,
points: 100,
name: "Goran"
};
var newState = {
..state,
points: 120
}
console.log(newState);
/*
{
id: 1,
points: 120,
name: "Goran"
}
*/
var state = [1,1,1,1];
// state[2]++ // [1,1,2,1]
var index = 2;
var newState = [
...state.slice(0, index),
state[index] + 1,
...state.slice(index + 1)
];
console.log(newState);
/*
[1,1,2,1]
*/
const items = {
1: {
id: 1,
name: "Goran"
},
2: {
id: 2,
name: "Petar"
}
};
const filterId = 1;
const filteredItems = Object.keys(items).reduce( (accumulator, key) => (
items[key].id === filterId ? accumulator : {
...accumulator,
[key]: items[key]
}
), {});
console.log(filteredItems);
/*
{
2: {
id: 2,
name: "Petar"
}
}
}
*/
var state = [
{name: "Goran"},
{name: "Peter"}
]
// you can use es6 Array.prototype.findIndex to find index of the object
// let index = state.findIndex(({name}) => name === "Peter");
var index = 1;
var field = 'name';
var value = 'Zika';
var newState = [
...state.slice(0, index),
{
...state[index],
[field]: value
},
...state.slice(index + 1)
];
console.log(newState);
/*
[
{name: "Goran"},
{name: "Zika"}
]
*/
var items = {1: {name: "Airplane", id: 1}, 2: {name: "Spaceship", id:2}};
var receivedItems = [{id: 3, name: "Quadrocopter"}, {id: 4, name: "Helicopter"}];
var newState = {
...items,
...receivedItems.reduce((obj, item) => ({
...obj,
[item.id]: item
}), {})
}
console.log(newState);
/*
{
1: {name: "Airplane", id: 1},
2: {name: "Spaceship", id: 2},
3: {name: "Quadrocopter", id: 3},
4: {name: "Helicopter", id: 4}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment