common.js
This file
Path
src/common.js
146 lines · 4.2 KiB · JavaScript
Last changed by
feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
History of this fileTake it
The walk stopped after 200 commits: lines older than c018c49 point at it.
Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 1–5why, one step back1export const NOTHING =2 typeof Symbol !== "undefined"3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}5feat: limited class supportaleclarsonbefore adoption9877d64
lines 6–7why, one step back6export const DRAFTABLE =7 typeof Symbol !== "undefined"fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c
line 8why, one step back8 ? Symbol.for("immer-draftable")feat: limited class supportaleclarsonbefore adoption9877d64
lines 9–10why, one step back9 : "__$immer_draftable"10feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11export const DRAFT_STATE =fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c
line 12why, one step back12 typeof Symbol !== "undefined" ? Symbol.for("immer-state") : "__$immer_state"Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 13why, one step back13feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 14–15why, one step back14export function isDraft(value) {15 return !!value && !!value[DRAFT_STATE]Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 16–17why, one step back16}17feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 18why, one step back18export function isDraftable(value) {feat: limited class supportaleclarsonbefore adoption9877d64
line 19why, one step back19 if (!value || typeof value !== "object") return falseAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 20–21why, one step back20 if (Array.isArray(value)) return true21 const proto = Object.getPrototypeOf(value)feat: limited class supportaleclarsonbefore adoption9877d64
line 22why, one step back22 if (!proto || proto === Object.prototype) return truefeat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 23why, one step back23 if (isMap(value)) return truefeat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 24why, one step back24 if (isSet(value)) return truefeat: limited class supportaleclarsonbefore adoption9877d64
line 25why, one step back25 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 26–28why, one step back26}2728export function original(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 29–30why, one step back29 if (value && value[DRAFT_STATE]) {30 return value[DRAFT_STATE].baseAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 31–34why, one step back31 }32 // otherwise return undefined33}34feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 35–52why, one step back35// We use Maps as `drafts` for Sets, not Objects36// See proxy.js37export function assignSet(target, ...mapOverrides) {38 mapOverrides.forEach(override => {39 for (const value of override.values()) {40 // When we add new drafts we have to remove their originals if present41 const originalValue = original(value)42 if (originalValue) {43 target.delete(originalValue)44 }45 target.add(value)46 }47 })48 return target49}5051export function assignMap(target, ...objOverrides) {52 objOverrides.forEach(override => {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 53–55why, one step back53 for (let key in override) {54 if (has(override, key)) {55 target.set(key, override[key])Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 56–57why, one step back56 }57 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 58–60why, one step back58 })59 return target60}feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 61–63why, one step back6162function assignObjectLegacy(target, ...objOverrides) {63 objOverrides.forEach(function(override) {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 64–71why, one step back64 for (let key in override) {65 if (has(override, key)) {66 target[key] = override[key]67 }68 }69 })70 return target71}feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 72why, one step back72export const assign = Object.assign || assignObjectLegacyAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 73why, one step back73feat: limited class supportaleclarsonbefore adoption9877d64
line 74why, one step back74export const ownKeys =test: polyfillsaleclarsonbefore adoption551d877
line 75why, one step back75 typeof Reflect !== "undefined" && Reflect.ownKeysfeat: limited class supportaleclarsonbefore adoption9877d64
line 76why, one step back76 ? Reflect.ownKeysfix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
lines 77–78why, one step back77 : typeof Object.getOwnPropertySymbols !== "undefined"78 ? obj =>feat: limited class supportaleclarsonbefore adoption9877d64
lines 79–81why, one step back79 Object.getOwnPropertyNames(obj).concat(80 Object.getOwnPropertySymbols(obj)81 )fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
line 82why, one step back82 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 83–85why, one step back8384export function shallowCopy(base, invokeGetters = false) {85 if (Array.isArray(base)) return base.slice()feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 86why, one step back86 if (isMap(base)) return new Map(base)feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 87why, one step back87 if (isSet(base)) return new Set(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 88–93why, one step back88 const clone = Object.create(Object.getPrototypeOf(base))89 ownKeys(base).forEach(key => {90 if (key === DRAFT_STATE) {91 return // Never copy over draft state.92 }93 const desc = Object.getOwnPropertyDescriptor(base, key)chore: comments and formattingAlec Larsonbefore adoptionff96173
line 94why, one step back94 let {value} = descfeat: limited class supportaleclarsonbefore adoption9877d64
lines 95–98why, one step back95 if (desc.get) {96 if (!invokeGetters) {97 throw new Error("Immer drafts cannot have computed properties")98 }chore: comments and formattingAlec Larsonbefore adoptionff96173
line 99why, one step back99 value = desc.get.call(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 100–101why, one step back100 }101 if (desc.enumerable) {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 102why, one step back102 clone[key] = valuefeat: limited class supportaleclarsonbefore adoption9877d64
lines 103–104why, one step back103 } else {104 Object.defineProperty(clone, key, {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 105why, one step back105 value,feat: limited class supportaleclarsonbefore adoption9877d64
lines 106–111why, one step back106 writable: true,107 configurable: true108 })109 }110 })111 return cloneAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 112–114why, one step back112}113114export function each(value, cb) {feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 115why, one step back115 if (Array.isArray(value) || isMap(value) || isSet(value)) {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 116–117why, one step back116 value.forEach((entry, index) => cb(index, entry, value))117 returnfeat: limited class supportaleclarsonbefore adoption9877d64
line 118why, one step back118 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 119why, one step back119 ownKeys(value).forEach(key => cb(key, value[key], value))feat: limited class supportaleclarsonbefore adoption9877d64
lines 120–122why, one step back120}121122export function isEnumerable(base, prop) {nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf
lines 123–124why, one step back123 const desc = Object.getOwnPropertyDescriptor(base, prop)124 return !!desc && desc.enumerableAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 125–137why, one step back125}126127export function has(thing, prop) {128 return Object.prototype.hasOwnProperty.call(thing, prop)129}130131export function is(x, y) {132 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js133 if (x === y) {134 return x !== 0 || 1 / x === 1 / y135 } else {136 return x !== x && y !== y137 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 138–141why, one step back138}139140export function isMap(target) {141 return target instanceof Mapfeat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 142–145why, one step back142}143144export function isSet(target) {145 return target instanceof SetAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 146why, one step back146}