Renamed `index.js` -> `immer.js`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 3a9d6d1 · Browse files · 8 files·+74 added−67 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+74 added−67 removed
- tests:+31 added−24 removed
- other:+43 added−43 removed
Provenance
- Commit
c4f5667- Tree
ed44a37- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
3a9d6d1
8 files changed+74 added−67 removed
modified:__tests__/flow/flow.js+27 added-20 removed
123456457891078111213101112141516171418191617202122192023242522232627282526293031282932333431323536373839// @flowimport produce, {setAutoFreeze, setUseProxies} from "../../src/immer"
describe("it should support flow", () => { test("type checking should work", () => { if (parseInt("2" + "321"[2]) !== 21) {setAutoFreeze(true)setUseProxies(true) setAutoFreeze(true) setUseProxies(true)
// we really don't want this code to actually execute..const result = produce({x: 3}, draft => { draft.x = 4 const result = produce({x: 3}, draft => { draft.x = 4
// $ExpectError console.log(draft.y)}) // $ExpectError console.log(draft.y) })
console.log(result.x) console.log(result.x)
const f2 = produce(draft => {})f2({x: 3}) const f2 = produce(draft => {}) f2({x: 3})
// $ExpectErrorsetAutoFreeze(3) // $ExpectError setAutoFreeze(3)
// $ExpectErrorconsole.log(result.y) // $ExpectError console.log(result.y)
// $ExpectErrorproduce() // $ExpectError produce()
// $ExpectErrorproduce({x: 3}) // $ExpectError produce({x: 3})
// $ExpectErrorproduce({x: 3}, []) // $ExpectError produce({x: 3}, []) } })})modified:__tests__/base.js+1 added-1 removed
1 "use strict"2-import produce, {setAutoFreeze, setUseProxies} from "../src"2+import produce, {setAutoFreeze, setUseProxies} from "../src/immer"3 import deepFreeze from "deep-freeze"4 import cloneDeep from "lodash.clonedeep"5 6 jest.setTimeout(1000)763 unmodified linesmodified:__tests__/curry.js+1 added-1 removed
122345637 unmodified lines"use strict"import produce, {setUseProxies} from "../src/"import produce, {setUseProxies} from "../src/immer"
runTests("proxy", true)runTests("es5", false)
37 unmodified linesmodified:__tests__/frozen.js+1 added-1 removed
1233456751 unmodified lines"use strict"import deepFreeze from "deep-freeze"import produce, {setUseProxies, setAutoFreeze} from "../src/"import produce, {setUseProxies, setAutoFreeze} from "../src/immer"
runTests("proxy", true)runTests("es5", false)
51 unmodified linesmodified:__tests__/readme.js+1 added-1 removed
122345630 unmodified lines"use strict"import produce from "../src/"import produce from "../src/immer"
describe("readme example", () => { it("works", () => { const baseState = [30 unmodified linesadded:src/immer.js+42 added
123456789101112131415161718192021222324252627282930313233343536373839404142export {setAutoFreeze, setUseProxies} from "./common"
import {isProxyable, getUseProxies} from "./common"import {produceProxy} from "./proxy"import {produceEs5} from "./es5"
/** * produce takes a state, and runs a function against it. * That function can freely mutate the state, as it will create copies-on-write. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned * * @export * @param {any} baseState - the state to start with * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified * @returns {any} a new state, or the base state if nothing was modified */export default function produce(baseState, producer) { // curried invocation if (arguments.length === 1) { const producer = baseState // prettier-ignore if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function") return function() { const args = arguments return produce(args[0], draft => { args[0] = draft // blegh! producer.apply(draft, args) }) } }
// prettier-ignore { if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length) if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState)) if (typeof producer !== "function") throw new Error("the second argument to produce should be a function") }
return getUseProxies() ? produceProxy(baseState, producer) : produceEs5(baseState, producer)}deleted:src/index.js-42 removed
1-export {setAutoFreeze, setUseProxies} from "./common"2-3-import {isProxyable, getUseProxies} from "./common"4-import {produceProxy} from "./proxy"5-import {produceEs5} from "./es5"6-7-/**8- * produce takes a state, and runs a function against it.9- * That function can freely mutate the state, as it will create copies-on-write.10- * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned11- *12- * @export13- * @param {any} baseState - the state to start with14- * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified15- * @returns {any} a new state, or the base state if nothing was modified16- */17-export default function produce(baseState, producer) {18- // curried invocation19- if (arguments.length === 1) {20- const producer = baseState21- // prettier-ignore22- if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")23- return function() {24- const args = arguments25- return produce(args[0], draft => {26- args[0] = draft // blegh!27- producer.apply(draft, args)28- })29- }30- }31-32- // prettier-ignore33- {34- if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)35- if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))36- if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")37- }38-39- return getUseProxies()40- ? produceProxy(baseState, producer)41- : produceEs5(baseState, producer)42-}modified:rollup.config.js+1 added-1 removed
5 unmodified lines6 import babel from "rollup-plugin-babel"7 8 function getConfig(dest, format, ugly) {9 const conf = {10- input: "src/index.js",10+ input: "src/immer.js",11 output: {12 exports: "named",13 file: dest,14 format,55 unmodified lines