Implemented currying and argument checking
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 9cb4046 · Browse files · 4 files·+82 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+82 added−16 removed
- tests:+42 added−3 removed
- other:+40 added−13 removed
Provenance
- Commit
4ec6076- Tree
2a63a6d- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
9cb4046
4 files changed+82 added−16 removed
added:__tests__/curry.js+39 added
123456789101112131415161718192021222324252627282930313233343536373839"use strict"import * as immerProxy from ".."import * as immerEs5 from "../es5"
runTests("proxy", immerProxy)runTests("es5", immerEs5)
function runTests(name, lib) { describe("auto freeze - " + name, () => { const produce = lib.default
it("should check arguments", () => { expect(() => produce()).toThrow(/produce expects 1 or 2 arguments/) expect(() => produce(() => {}, {})).toThrow( /the first argument to produce should be a plain object or array/ ) expect(() => produce(1, () => {})).toThrow( /the first argument to produce should be a plain object or array/ ) expect(() => produce({}, () => {}, [])).toThrow( /produce expects 1 or 2 arguments/ ) })
it("should support currying", () => { const state = [{}, {}, {}] const mapper = produce((item, index) => { item.index = index })
expect(state.map(mapper)).not.toBe(state) expect(state.map(mapper)).toEqual([ {index: 0}, {index: 1}, {index: 2} ]) }) })}modified:__tests__/frozen.js+3 added-3 removed
1234567678991011121344 unmodified lines"use strict"import * as immerProxy from ".."import * as immerEs5 from "../es5"import deepFreeze from "deep-freeze"
runBaseTest("proxy", immerProxy)runBaseTest("es5", immerEs5)runTests("proxy", immerProxy)runTests("es5", immerEs5)
function runBaseTest(name, lib) {function runTests(name, lib) { describe("auto freeze - " + name, () => { const immer = lib.default const baseState = { object: {a: 1},44 unmodified linesmodified:es5.js+25 added-4 removed
10 unmodified lines11 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned12 *13 * @export14 * @param {any} baseState - the state to start with15- * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified15+ * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified16 * @returns {any} a new state, or the base state if nothing was modified17 */18-function immer(baseState, thunk) {18+function produce(baseState, producer) {19+ // curried invocation20+ if (arguments.length === 1) {21+ const producer = baseState22+ // prettier-ignore23+ if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")24+ return function() {25+ const args = arguments26+ return produce(args[0], draft => {27+ args[0] = draft // blegh!28+ baseState.apply(null, args)29+ })30+ }31+ }32+33+ // prettier-ignore34+ {35+ if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)36+ if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))37+ if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")38+ }39+40 let finalizing = false41 let finished = false42 const descriptors = {}43 const states = []147 unmodified lines191 192 // create proxy for root193 const rootClone = createProxy(undefined, baseState)194 // execute the thunk174- const maybeVoidReturn = thunk(rootClone)195+ const maybeVoidReturn = producer(rootClone)196 //values either than undefined will trigger warning;197 !Object.is(maybeVoidReturn, undefined) &&198 console.warn(199 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`72 unmodified lines272 autoFreeze = enableAutoFreeze273 }274 275 createHiddenProperty(exports, "__esModule", true)255-module.exports.default = immer276+module.exports.default = produce277 module.exports.setAutoFreeze = setAutoFreezemodified:immer.js+15 added-9 removed
70 unmodified lines71 }72 }73 74 /**75- * Immer takes a state, and runs a function against it.75+ * produce takes a state, and runs a function against it.76 * That function can freely mutate the state, as it will create copies-on-write.77 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned78 *79 * @export80 * @param {any} baseState - the state to start with81- * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified81+ * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified82 * @returns {any} a new state, or the base state if nothing was modified83 */84-function immer(baseState, thunk) {84+function produce(baseState, producer) {85 // curried invocation86 if (arguments.length === 1) {87- // TODO: check arg is fn87+ const producer = baseState88+ // prettier-ignore89+ if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")90 return function() {89- // TODO: check arg1 is object / array91 const args = arguments91- return immer(args[0], draft => {92+ return produce(args[0], draft => {93 args[0] = draft // blegh!94 baseState.apply(null, args)95 })96 }97 }98 98- // TODO: check args99+ // prettier-ignore100+ {101+ if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)102+ if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))103+ if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")104+ }105 const revocableProxies = []106 107 class State {108 constructor(parent, base) {111 unmodified lines220 221 // create proxy for root222 const rootClone = createProxy(undefined, baseState)223 // execute the thunk218- const maybeVoidReturn = thunk(rootClone)224+ const maybeVoidReturn = producer(rootClone)225 //values either than undefined will trigger warning;226 !Object.is(maybeVoidReturn, undefined) &&227 console.warn(228 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`42 unmodified lines271 272 Object.defineProperty(exports, "__esModule", {273 value: true274 })269-module.exports.default = immer275+module.exports.default = produce276 module.exports.setAutoFreeze = setAutoFreeze