common.js
This file
Path
src/common.js
108 lines · 3.1 KiB · JavaScript
Last changed by
fix: ensure Symbol.for exists before calling it (#366)Fabrizio Catittibefore adoptionc8ecbad
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
line 6why, one step back6export const DRAFTABLE =fix: ensure Symbol.for exists before calling it (#366)Fabrizio Catittibefore adoptionc8ecbad
line 7why, one step back7 typeof Symbol !== "undefined" && Symbol.forfix: 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: ensure Symbol.for exists before calling it (#366)Fabrizio Catittibefore adoptionc8ecbad
lines 12–14why, one step back12 typeof Symbol !== "undefined" && Symbol.for13 ? Symbol.for("immer-state")14 : "__$immer_state"Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 15why, one step back15feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 16–17why, one step back16export function isDraft(value) {17 return !!value && !!value[DRAFT_STATE]Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 18–19why, one step back18}19feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 20why, one step back20export function isDraftable(value) {feat: limited class supportaleclarsonbefore adoption9877d64
line 21why, one step back21 if (!value || typeof value !== "object") return falseAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 22–23why, one step back22 if (Array.isArray(value)) return true23 const proto = Object.getPrototypeOf(value)feat: limited class supportaleclarsonbefore adoption9877d64
lines 24–25why, one step back24 if (!proto || proto === Object.prototype) return true25 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}34fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 35–40why, one step back35export const assign =36 Object.assign ||37 function assign(target, value) {38 for (let key in value) {39 if (has(value, key)) {40 target[key] = value[key]Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 41–42why, one step back41 }42 }fix: revert Map supportAlec Larsonbefore adoption47529d3
line 43why, one step back43 return targetAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 44–45why, one step back44 }45feat: limited class supportaleclarsonbefore adoption9877d64
line 46why, one step back46export const ownKeys =test: polyfillsaleclarsonbefore adoption551d877
line 47why, one step back47 typeof Reflect !== "undefined" && Reflect.ownKeysfeat: limited class supportaleclarsonbefore adoption9877d64
line 48why, one step back48 ? Reflect.ownKeysfix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
lines 49–50why, one step back49 : typeof Object.getOwnPropertySymbols !== "undefined"50 ? obj =>feat: limited class supportaleclarsonbefore adoption9877d64
lines 51–53why, one step back51 Object.getOwnPropertyNames(obj).concat(52 Object.getOwnPropertySymbols(obj)53 )fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
line 54why, one step back54 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 55–63why, one step back5556export function shallowCopy(base, invokeGetters = false) {57 if (Array.isArray(base)) return base.slice()58 const clone = Object.create(Object.getPrototypeOf(base))59 ownKeys(base).forEach(key => {60 if (key === DRAFT_STATE) {61 return // Never copy over draft state.62 }63 const desc = Object.getOwnPropertyDescriptor(base, key)chore: comments and formattingAlec Larsonbefore adoptionff96173
line 64why, one step back64 let {value} = descfeat: limited class supportaleclarsonbefore adoption9877d64
lines 65–68why, one step back65 if (desc.get) {66 if (!invokeGetters) {67 throw new Error("Immer drafts cannot have computed properties")68 }chore: comments and formattingAlec Larsonbefore adoptionff96173
line 69why, one step back69 value = desc.get.call(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 70–71why, one step back70 }71 if (desc.enumerable) {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 72why, one step back72 clone[key] = valuefeat: limited class supportaleclarsonbefore adoption9877d64
lines 73–74why, one step back73 } else {74 Object.defineProperty(clone, key, {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 75why, one step back75 value,feat: limited class supportaleclarsonbefore adoption9877d64
lines 76–81why, one step back76 writable: true,77 configurable: true78 })79 }80 })81 return cloneAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 82–84why, one step back82}8384export function each(value, cb) {fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 85–88why, one step back85 if (Array.isArray(value)) {86 for (let i = 0; i < value.length; i++) cb(i, value[i], value)87 } else {88 ownKeys(value).forEach(key => cb(key, value[key], value))feat: limited class supportaleclarsonbefore adoption9877d64
lines 89–92why, one step back89 }90}9192export function isEnumerable(base, prop) {nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoptionb967bab
lines 93–94why, one step back93 const desc = Object.getOwnPropertyDescriptor(base, prop)94 return !!desc && desc.enumerableAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 95–108why, one step back95}9697export function has(thing, prop) {98 return Object.prototype.hasOwnProperty.call(thing, prop)99}100101export function is(x, y) {102 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js103 if (x === y) {104 return x !== 0 || 1 / x === 1 / y105 } else {106 return x !== x && y !== y107 }108}