common.js
This file
Path
src/common.js
145 lines · 4.1 KiB · JavaScript
Last changed by
nit: merge isMap and isSet checksAlec Larsonbefore adoption4164f0d
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 truenit: merge isMap and isSet checksAlec Larsonbefore adoption4164f0d
line 23why, one step back23 if (isMap(value) || isSet(value)) return truefeat: limited class supportaleclarsonbefore adoption9877d64
line 24why, one step back24 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 25–27why, one step back25}2627export function original(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 28–29why, one step back28 if (value && value[DRAFT_STATE]) {29 return value[DRAFT_STATE].baseAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 30–33why, one step back30 }31 // otherwise return undefined32}33feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 34–51why, one step back34// We use Maps as `drafts` for Sets, not Objects35// See proxy.js36export function assignSet(target, ...mapOverrides) {37 mapOverrides.forEach(override => {38 for (const value of override.values()) {39 // When we add new drafts we have to remove their originals if present40 const originalValue = original(value)41 if (originalValue) {42 target.delete(originalValue)43 }44 target.add(value)45 }46 })47 return target48}4950export function assignMap(target, ...objOverrides) {51 objOverrides.forEach(override => {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 52–54why, one step back52 for (let key in override) {53 if (has(override, key)) {54 target.set(key, override[key])Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 55–56why, one step back55 }56 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 57–59why, one step back57 })58 return target59}feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 60–62why, one step back6061function assignObjectLegacy(target, ...objOverrides) {62 objOverrides.forEach(function(override) {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 63–70why, one step back63 for (let key in override) {64 if (has(override, key)) {65 target[key] = override[key]66 }67 }68 })69 return target70}feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 71why, one step back71export const assign = Object.assign || assignObjectLegacyAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 72why, one step back72feat: limited class supportaleclarsonbefore adoption9877d64
line 73why, one step back73export const ownKeys =test: polyfillsaleclarsonbefore adoption551d877
line 74why, one step back74 typeof Reflect !== "undefined" && Reflect.ownKeysfeat: limited class supportaleclarsonbefore adoption9877d64
line 75why, one step back75 ? Reflect.ownKeysfix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
lines 76–77why, one step back76 : typeof Object.getOwnPropertySymbols !== "undefined"77 ? obj =>feat: limited class supportaleclarsonbefore adoption9877d64
lines 78–80why, one step back78 Object.getOwnPropertyNames(obj).concat(79 Object.getOwnPropertySymbols(obj)80 )fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
line 81why, one step back81 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 82–84why, one step back8283export function shallowCopy(base, invokeGetters = false) {84 if (Array.isArray(base)) return base.slice()feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 85why, one step back85 if (isMap(base)) return new Map(base)feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 86why, one step back86 if (isSet(base)) return new Set(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 87–92why, one step back87 const clone = Object.create(Object.getPrototypeOf(base))88 ownKeys(base).forEach(key => {89 if (key === DRAFT_STATE) {90 return // Never copy over draft state.91 }92 const desc = Object.getOwnPropertyDescriptor(base, key)chore: comments and formattingAlec Larsonbefore adoptionff96173
line 93why, one step back93 let {value} = descfeat: limited class supportaleclarsonbefore adoption9877d64
lines 94–97why, one step back94 if (desc.get) {95 if (!invokeGetters) {96 throw new Error("Immer drafts cannot have computed properties")97 }chore: comments and formattingAlec Larsonbefore adoptionff96173
line 98why, one step back98 value = desc.get.call(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 99–100why, one step back99 }100 if (desc.enumerable) {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 101why, one step back101 clone[key] = valuefeat: limited class supportaleclarsonbefore adoption9877d64
lines 102–103why, one step back102 } else {103 Object.defineProperty(clone, key, {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 104why, one step back104 value,feat: limited class supportaleclarsonbefore adoption9877d64
lines 105–110why, one step back105 writable: true,106 configurable: true107 })108 }109 })110 return cloneAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 111–113why, one step back111}112113export function each(value, cb) {feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 114why, one step back114 if (Array.isArray(value) || isMap(value) || isSet(value)) {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 115–116why, one step back115 value.forEach((entry, index) => cb(index, entry, value))116 returnfeat: limited class supportaleclarsonbefore adoption9877d64
line 117why, one step back117 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 118why, one step back118 ownKeys(value).forEach(key => cb(key, value[key], value))feat: limited class supportaleclarsonbefore adoption9877d64
lines 119–121why, one step back119}120121export function isEnumerable(base, prop) {nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf
lines 122–123why, one step back122 const desc = Object.getOwnPropertyDescriptor(base, prop)123 return !!desc && desc.enumerableAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 124–136why, one step back124}125126export function has(thing, prop) {127 return Object.prototype.hasOwnProperty.call(thing, prop)128}129130export function is(x, y) {131 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js132 if (x === y) {133 return x !== 0 || 1 / x === 1 / y134 } else {135 return x !== x && y !== y136 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 137–140why, one step back137}138139export function isMap(target) {140 return target instanceof Mapfeat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 141–144why, one step back141}142143export function isSet(target) {144 return target instanceof SetAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 145why, one step back145}