You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
38 KiB
1 line
38 KiB
4 years ago
|
{"version":3,"file":"immer.umd.js","sources":["../src/common.js","../src/patches.js","../src/es5.js","../src/proxy.js","../src/immer.js","../src/index.js"],"sourcesContent":["export const NOTHING =\n typeof Symbol !== \"undefined\"\n ? Symbol(\"immer-nothing\")\n : {[\"immer-nothing\"]: true}\n\nexport const DRAFT_STATE =\n typeof Symbol !== \"undefined\" ? Symbol(\"immer-state\") : \"__$immer_state\"\n\nexport function isDraft(value) {\n return !!value && !!value[DRAFT_STATE]\n}\n\nexport function isDraftable(value) {\n if (!value) return false\n if (typeof value !== \"object\") return false\n if (Array.isArray(value)) return true\n const proto = Object.getPrototypeOf(value)\n return proto === null || proto === Object.prototype\n}\n\nexport function original(value) {\n if (value && value[DRAFT_STATE]) {\n return value[DRAFT_STATE].base\n }\n // otherwise return undefined\n}\n\nexport const assign =\n Object.assign ||\n function assign(target, value) {\n for (let key in value) {\n if (has(value, key)) {\n target[key] = value[key]\n }\n }\n return target\n }\n\nexport function shallowCopy(value) {\n if (Array.isArray(value)) return value.slice()\n const target = value.__proto__ === undefined ? Object.create(null) : {}\n return assign(target, value)\n}\n\nexport function each(value, cb) {\n if (Array.isArray(value)) {\n for (let i = 0; i < value.length; i++) cb(i, value[i], value)\n } else {\n for (let key in value) cb(key, value[key], value)\n }\n}\n\nexport function has(thing, prop) {\n return Object.prototype.hasOwnProperty.call(thing, prop)\n}\n\nexport function is(x, y) {\n // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js\n if (x === y) {\n return x !== 0 || 1 / x === 1 / y\n } else {\n return x !== x && y !== y\n }\n}\n","import {each} from \"./common\"\n\nexport function generatePatches(state, basePath, patches, inversePatches) {\n Array.isArray(state.base)\n ? generateArrayPatches(state, basePath, patches, inversePatches)\n : generateObjectPatches(state, basePath, patches, inversePatches)\n}\n\nexport function generateArrayPatches(state, basePath, patches, inversePatches) {\n const {base, copy, assigned} = state\n const minLength = Math.min(base.length, copy.length)\n\n // Look for replaced indices.\n for (let i = 0; i < minLength; i++) {\n if (assigned[i] && base[i] !== copy[i]) {\n const path = basePath.concat(i)\n patches.push({op: \"replace\", path, value: copy[i]})\n inversePatches.push({op: \"replace\", path, value: base[i]})\n }\n }\n\n // Did the array expand?\n if (minLength < copy.length) {\n for (let i = minLength; i < copy.length; i++) {\n patches.push({\n op: \"add\",\n path: basePath.concat(i),\n value: copy[i]\n })\n }\n inversePatches.push({\n op: \"replace\",\n path: basePath.concat(\"length\"),\n value: base.length\n })\n }\n\n // ...or did it shrink?\n else if (minLength < base.length) {\n patches.push({\n op: \"replace\",\n path: basePath.concat(\"length\"),\n value: copy.length\n })\n for (let i = minLength; i < base.length; i++) {\n inversePatches.push({\n op: \"add\",\n path: basePath.concat(i),\n value: base[i]\n })\n }\n }\n}\n\nfunction generateObjectPatches(state, basePath, patches, inversePatches) {\n const {base, copy} = state\n each(state.assigned, (key, assignedValue) => {\n const origValue = base[key]\n const value = copy[key]\n const op = !assignedValue ? \"remove\" : key in base ? \"replace\" : \"add\"\n if (origValue === base && op === \"replace
|