Skip to content

Instantly share code, notes, and snippets.

@madeinfree
Last active May 10, 2016 02:22
Show Gist options
  • Save madeinfree/0b05d742674958bfd4b10b25cfc0089e to your computer and use it in GitHub Desktop.
Save madeinfree/0b05d742674958bfd4b10b25cfc0089e to your computer and use it in GitHub Desktop.
import Im from 'immutable';
export default class ImUtil {
/*
* new Map()
*/
setEmptyMap = (obj, cursor) => {
return obj.setIn(cursor, new Im.Map());
}
/*
* new List()
*/
addList = (obj, cursor, value) => {
return obj.setIn(cursor, obj.getIn(cursor).push(value));
}
removeList = (obj, cursor, index) => {
return obj.setIn(cursor, obj.getIn(cursor).remove(index));
}
setEmptyList = (obj, cursor) => {
return obj.setIn(cursor, new Im.List());
}
/*
* new Set()
*/
setEmptySet = (obj, cursor) => {
return obj.setIn(cursor, new Im.Set());
}
reverse = (obj, cursor) => {
return obj.setIn(cursor, !obj.getIn(cursor));
}
};
import ImUtil from '../lib/util/im-util';
import Im from 'immutable';
import expect from 'expect';
let imUtil = new ImUtil();
describe('Im', function() {
it('imMap items sould clear', () => {
let imMap = new Im.Map();
imMap = imMap.setIn(['items', 'item'], 1);
expect(Im.Map.isMap(imMap)).toEqual(true);
expect(imMap.getIn(['items']).size).toEqual(1);
const newImMap = imUtil.setEmptyMap(imMap, ['items']);
expect(newImMap.getIn(['items']).size).toEqual(0);
});
it('imArray items sould add and remove', () => {
const imMap = new Im.Map();
const imArray = imMap.setIn(['items'], new Im.List());
expect(Im.List.isList(imArray.getIn(['items']))).toEqual(true);
const newImList = imUtil.addList(imArray, ['items'], 1);
expect(newImList.getIn(['items', 0])).toEqual(1);
const removeList = imUtil.removeList(newImList, ['items'], 0);
expect(removeList.getIn(['items', 0])).toEqual(undefined);
const clearList = imUtil.clearList(newImList, ['items']);
expect(clearList.getIn(['items']).size).toEqual(0);
})
it('sould reverse the set', () => {
const imMap = new Im.Map();
let imArray = imMap.setIn(['items'], new Im.List());
let newImList, revertImList;
expect(Im.List.isList(imArray.getIn(['items']))).toEqual(true);
[1,2,3,4,5,6].forEach((n, index) => {
imArray = imUtil.addList(imArray, ['items'], false);
})
revertImList = imArray;
[0,2,5].forEach((n) => {
revertImList = imUtil.reverse(revertImList, ['items', n]);
})
expect(revertImList.getIn(['items', 0])).toEqual(true);
expect(revertImList.getIn(['items', 2])).toEqual(true);
expect(revertImList.getIn(['items', 5])).toEqual(true);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment