common.js
This file
Path
src/common.js
129 lines · 3.2 KiB · JavaScript
Last changed by
BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
History of this fileTake it
The walk stopped after 200 commits: lines older than 72850a9 point at it.
fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 1why, one step back1export const NOTHING =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 2–4why, one step back2 typeof Symbol !== "undefined"3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 5why, one step back5feat: limited class supportaleclarsonbefore adoption9877d64
line 6why, one step back6export const DRAFTABLE =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 7–9why, one step back7 typeof Symbol !== "undefined" && Symbol.for8 ? Symbol.for("immer-draftable")9 : "__$immer_draftable"feat: limited class supportaleclarsonbefore adoption9877d64
line 10why, one step back10feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11export const DRAFT_STATE =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 12–14why, one step back12 typeof Symbol !== "undefined" && Symbol.for13 ? Symbol.for("immer-state")14 : "__$immer_state"fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 15why, one step back15feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 16why, one step back16export function isDraft(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 17why, one step back17 return !!value && !!value[DRAFT_STATE]fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 18–19why, one step back18}19feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 20why, one step back20export function isDraftable(value) {Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883
lines 21–27why, one step back21 if (!value) return false22 return (23 isPlainObject(value) || !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]24 )25}2627export function isPlainObject(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 28–30why, one step back28 if (!value || typeof value !== "object") return false29 if (Array.isArray(value)) return true30 const proto = Object.getPrototypeOf(value)Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883
line 31why, one step back31 return !proto || proto === Object.prototypefix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 32–34why, one step back32}3334export function original(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 35–38why, one step back35 if (value && value[DRAFT_STATE]) {36 return value[DRAFT_STATE].base37 }38 // otherwise return undefinedfix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 39–40why, one step back39}40fix: revert Map supportAlec Larsonbefore adoption47529d3
line 41why, one step back41export const assign =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 42–50why, one step back42 Object.assign ||43 function assign(target, value) {44 for (let key in value) {45 if (has(value, key)) {46 target[key] = value[key]47 }48 }49 return target50 }fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 51why, one step back51feat: limited class supportaleclarsonbefore adoption9877d64
line 52why, one step back52export const ownKeys =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 53–60why, one step back53 typeof Reflect !== "undefined" && Reflect.ownKeys54 ? Reflect.ownKeys55 : typeof Object.getOwnPropertySymbols !== "undefined"56 ? obj =>57 Object.getOwnPropertyNames(obj).concat(58 Object.getOwnPropertySymbols(obj)59 )60 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 61–62why, one step back6162export function shallowCopy(base, invokeGetters = false) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 63–87why, one step back63 if (Array.isArray(base)) return base.slice()64 const clone = Object.create(Object.getPrototypeOf(base))65 ownKeys(base).forEach(key => {66 if (key === DRAFT_STATE) {67 return // Never copy over draft state.68 }69 const desc = Object.getOwnPropertyDescriptor(base, key)70 let {value} = desc71 if (desc.get) {72 if (!invokeGetters) {73 throw new Error("Immer drafts cannot have computed properties")74 }75 value = desc.get.call(base)76 }77 if (desc.enumerable) {78 clone[key] = value79 } else {80 Object.defineProperty(clone, key, {81 value,82 writable: true,83 configurable: true84 })85 }86 })87 return clonefix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 88–90why, one step back88}8990export function each(value, cb) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 91–95why, one step back91 if (Array.isArray(value)) {92 for (let i = 0; i < value.length; i++) cb(i, value[i], value)93 } else {94 ownKeys(value).forEach(key => cb(key, value[key], value))95 }feat: limited class supportaleclarsonbefore adoption9877d64
lines 96–98why, one step back96}9798export function isEnumerable(base, prop) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 99–100why, one step back99 const desc = Object.getOwnPropertyDescriptor(base, prop)100 return !!desc && desc.enumerablefix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 101–103why, one step back101}102103export function has(thing, prop) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 104why, one step back104 return Object.prototype.hasOwnProperty.call(thing, prop)fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 105–107why, one step back105}106107export function is(x, y) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 108–113why, one step back108 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js109 if (x === y) {110 return x !== 0 || 1 / x === 1 / y111 } else {112 return x !== x && y !== y113 }Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883
lines 114–121why, one step back114}115116export function clone(obj) {117 if (!isDraftable(obj)) return obj118 if (Array.isArray(obj)) return obj.map(clone)119 const cloned = Object.create(Object.getPrototypeOf(obj))120 for (const key in obj) cloned[key] = clone(obj[key])121 return clonedfix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 122why, one step back122}BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
lines 123–129why, one step back123124export function deepFreeze(obj) {125 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return126 Object.freeze(obj)127 if (Array.isArray(obj)) obj.forEach(deepFreeze)128 else for (const key in obj) deepFreeze(obj[key])129}