From a247cf92c5b9ba25825d529f9e73b7272dcb2863 Mon Sep 17 00:00:00 2001 From: Zach Silveira Date: Thu, 4 Aug 2016 17:27:24 -0400 Subject: [PATCH] allow setting --- package.json | 2 +- src/fromJS.js | 13 +++++++++++++ src/index.js | 4 ++-- src/map.js | 9 --------- tests/fromJS.js | 27 +++++++++++++++++++++++++++ tests/map.js | 9 --------- 6 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 src/fromJS.js delete mode 100644 src/map.js create mode 100644 tests/fromJS.js delete mode 100644 tests/map.js diff --git a/package.json b/package.json index f98f599..b43e629 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "access immutable values more easily", "main": "lib/index.js", "peerDependencies": { - "immutable": "^3.7.6" + "immutable": "^3.8.1" }, "devDependencies": { "babel-cli": "^6.6.5", diff --git a/src/fromJS.js b/src/fromJS.js new file mode 100644 index 0000000..19cd2e3 --- /dev/null +++ b/src/fromJS.js @@ -0,0 +1,13 @@ +import { fromJS } from 'immutable' + +export default initialData => { + let immutableMap = fromJS(initialData) + + return new Proxy(immutableMap, { + get: (proxy, name) => immutableMap.get(name) || immutableMap[name], + set: (proxy, name, value) => { + immutableMap = immutableMap.set(name, value) + return immutableMap + } + }) +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index db29eb7..e825fd0 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ import Immutable from 'immutable' import List from './list' -import Map from './map' +import fromJS from './fromJS' module.exports = { ...Immutable, List, - Map + fromJS, } diff --git a/src/map.js b/src/map.js deleted file mode 100644 index 6277ef9..0000000 --- a/src/map.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Map } from 'immutable' - -export default initialData => { - const immutableMap = Map(initialData) - - return new Proxy(immutableMap, { - get: (proxy, name) => immutableMap.get(name) || immutableMap[name] - }) -} \ No newline at end of file diff --git a/tests/fromJS.js b/tests/fromJS.js new file mode 100644 index 0000000..c1b23bc --- /dev/null +++ b/tests/fromJS.js @@ -0,0 +1,27 @@ +import fromJS from '../src/fromJS' +import { expect } from 'chai' + +describe('fromJS Proxy', () => { + it('should access value without calling .get', () => { + const data = fromJS({first: 'yo'}) + expect(data.first).to.equal('yo') + }) + + it('should access value without calling .getIn', () => { + const data = fromJS({ nested: fromJS({ first: 'yo' })}) + expect(data.nested.first).to.equal('yo') + }) + + it('should set value without calling .set', () => { + const data = fromJS({ first: 'yo' }) + data.first = 'changed' + expect(data.first).to.equal('changed') + }) + + it('should set value without calling .setIn', () => { + const data = fromJS({ nested: fromJS({ first: 'yo' })}) + data.nested.first = 'changed' + + expect(data.nested.first).to.equal('changed') + }) +}) \ No newline at end of file diff --git a/tests/map.js b/tests/map.js deleted file mode 100644 index 867e890..0000000 --- a/tests/map.js +++ /dev/null @@ -1,9 +0,0 @@ -import Map from '../src/map' -import { expect } from 'chai' - -describe('Map Proxy', () => { - it('should access value without calling .get', () => { - const data = Map({first: 'yo'}) - expect(data.first).to.equal('yo') - }) -}) \ No newline at end of file