common.js
This file
Path
src/common.js
151 lines · 4.2 KiB · JavaScript
Last changed by
feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
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–35why, one step back34// We use Maps as `drafts` for Sets, not Objects35// See proxy.jsfeat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 36–41why, one step back36export function assignSet(target, override) {37 for (const value of override.values()) {38 // When we add new drafts we have to remove their originals if present39 const originalValue = original(value)40 if (originalValue) {41 target.delete(originalValue)feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 42why, one step back42 }feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 43–44why, one step back43 target.add(value)44 }feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 45–47why, one step back45 return target46}47feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 48–52why, one step back48// We use Maps as `drafts` for Maps, not Objects49// See proxy.js50export function assignMap(target, override) {51 override.forEach((value, key) => {52 target.set(key, value)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 53–55why, one step back53 })54 return target55}feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 56–58why, one step back5657function assignObjectLegacy(target, ...objOverrides) {58 objOverrides.forEach(function(override) {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 59–66why, one step back59 for (let key in override) {60 if (has(override, key)) {61 target[key] = override[key]62 }63 }64 })65 return target66}feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 67why, one step back67export const assign = Object.assign || assignObjectLegacyAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 68why, one step back68feat: limited class supportaleclarsonbefore adoption9877d64
line 69why, one step back69export const ownKeys =test: polyfillsaleclarsonbefore adoption551d877
line 70why, one step back70 typeof Reflect !== "undefined" && Reflect.ownKeysfeat: limited class supportaleclarsonbefore adoption9877d64
line 71why, one step back71 ? Reflect.ownKeysfix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
lines 72–73why, one step back72 : typeof Object.getOwnPropertySymbols !== "undefined"73 ? obj =>feat: limited class supportaleclarsonbefore adoption9877d64
lines 74–76why, one step back74 Object.getOwnPropertyNames(obj).concat(75 Object.getOwnPropertySymbols(obj)76 )fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
line 77why, one step back77 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 78–80why, one step back7879export function shallowCopy(base, invokeGetters = false) {80 if (Array.isArray(base)) return base.slice()feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 81why, one step back81 if (isMap(base)) return new Map(base)feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 82why, one step back82 if (isSet(base)) return new Set(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 83–88why, one step back83 const clone = Object.create(Object.getPrototypeOf(base))84 ownKeys(base).forEach(key => {85 if (key === DRAFT_STATE) {86 return // Never copy over draft state.87 }88 const desc = Object.getOwnPropertyDescriptor(base, key)chore: comments and formattingAlec Larsonbefore adoptionff96173
line 89why, one step back89 let {value} = descfeat: limited class supportaleclarsonbefore adoption9877d64
lines 90–93why, one step back90 if (desc.get) {91 if (!invokeGetters) {92 throw new Error("Immer drafts cannot have computed properties")93 }chore: comments and formattingAlec Larsonbefore adoptionff96173
line 94why, one step back94 value = desc.get.call(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 95–96why, one step back95 }96 if (desc.enumerable) {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 97why, one step back97 clone[key] = valuefeat: limited class supportaleclarsonbefore adoption9877d64
lines 98–99why, one step back98 } else {99 Object.defineProperty(clone, key, {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 100why, one step back100 value,feat: limited class supportaleclarsonbefore adoption9877d64
lines 101–106why, one step back101 writable: true,102 configurable: true103 })104 }105 })106 return cloneAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 107–109why, one step back107}108109export function each(value, cb) {feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 110why, one step back110 if (Array.isArray(value) || isMap(value) || isSet(value)) {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 111–112why, one step back111 value.forEach((entry, index) => cb(index, entry, value))112 returnfeat: limited class supportaleclarsonbefore adoption9877d64
line 113why, one step back113 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 114why, one step back114 ownKeys(value).forEach(key => cb(key, value[key], value))feat: limited class supportaleclarsonbefore adoption9877d64
lines 115–117why, one step back115}116117export function isEnumerable(base, prop) {nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf
lines 118–119why, one step back118 const desc = Object.getOwnPropertyDescriptor(base, prop)119 return !!desc && desc.enumerableAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 120–122why, one step back120}121122export function has(thing, prop) {feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 123–125why, one step back123 if (isMap(thing)) {124 return thing.has(prop)125 }Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 126why, one step back126 return Object.prototype.hasOwnProperty.call(thing, prop)feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 127–133why, one step back127}128129export function get(thing, prop) {130 if (isMap(thing)) {131 return thing.get(prop)132 }133 return thing[prop]Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 134–142why, one step back134}135136export function is(x, y) {137 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js138 if (x === y) {139 return x !== 0 || 1 / x === 1 / y140 } else {141 return x !== x && y !== y142 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 143–146why, one step back143}144145export function isMap(target) {146 return target instanceof Mapfeat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 147–150why, one step back147}148149export function isSet(target) {150 return target instanceof SetAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 151why, one step back151}