common.js
This file
Path
src/common.js
131 lines · 3.7 KiB · JavaScript
Last changed by
fix: add Map getter for DRAFT_STATE, make isEnumerable more resilientkeenondrumsbefore adoption7bbec0a
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: 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: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 34–38why, one step back34function assignMap(target, overrides) {35 overrides.forEach(function(override) {36 for (let key in override) {37 if (has(override, key)) {38 target.set(key, override[key])Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 39–40why, one step back39 }40 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 41–56why, one step back41 })42 return target43}44function assignObjectLegacy(target, overrides) {45 overrides.forEach(function(override) {46 for (let key in override) {47 if (has(override, key)) {48 target[key] = override[key]49 }50 }51 })52 return target53}54export function assign(target, ...overrides) {55 if (isMap(target)) {56 return assignMap(target, overrides)Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 57why, one step back57 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 58–62why, one step back58 if (Object.assign) {59 return Object.assign(target, ...overrides)60 }61 return assignObjectLegacy(target, overrides)62}Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 63why, one step back63feat: limited class supportaleclarsonbefore adoption9877d64
line 64why, one step back64export const ownKeys =test: polyfillsaleclarsonbefore adoption551d877
line 65why, one step back65 typeof Reflect !== "undefined" && Reflect.ownKeysfeat: limited class supportaleclarsonbefore adoption9877d64
line 66why, one step back66 ? Reflect.ownKeysfix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
lines 67–68why, one step back67 : typeof Object.getOwnPropertySymbols !== "undefined"68 ? obj =>feat: limited class supportaleclarsonbefore adoption9877d64
lines 69–71why, one step back69 Object.getOwnPropertyNames(obj).concat(70 Object.getOwnPropertySymbols(obj)71 )fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
line 72why, one step back72 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 73–75why, one step back7374export function shallowCopy(base, invokeGetters = false) {75 if (Array.isArray(base)) return base.slice()feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 76why, one step back76 if (isMap(base)) return new Map(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 77–82why, one step back77 const clone = Object.create(Object.getPrototypeOf(base))78 ownKeys(base).forEach(key => {79 if (key === DRAFT_STATE) {80 return // Never copy over draft state.81 }82 const desc = Object.getOwnPropertyDescriptor(base, key)chore: comments and formattingAlec Larsonbefore adoptionff96173
line 83why, one step back83 let {value} = descfeat: limited class supportaleclarsonbefore adoption9877d64
lines 84–87why, one step back84 if (desc.get) {85 if (!invokeGetters) {86 throw new Error("Immer drafts cannot have computed properties")87 }chore: comments and formattingAlec Larsonbefore adoptionff96173
line 88why, one step back88 value = desc.get.call(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 89–90why, one step back89 }90 if (desc.enumerable) {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 91why, one step back91 clone[key] = valuefeat: limited class supportaleclarsonbefore adoption9877d64
lines 92–93why, one step back92 } else {93 Object.defineProperty(clone, key, {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 94why, one step back94 value,feat: limited class supportaleclarsonbefore adoption9877d64
lines 95–100why, one step back95 writable: true,96 configurable: true97 })98 }99 })100 return cloneAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 101–103why, one step back101}102103export function each(value, cb) {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 104–106why, one step back104 if (Array.isArray(value) || isMap(value)) {105 value.forEach((entry, index) => cb(index, entry, value))106 returnfeat: limited class supportaleclarsonbefore adoption9877d64
line 107why, one step back107 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 108why, one step back108 ownKeys(value).forEach(key => cb(key, value[key], value))feat: limited class supportaleclarsonbefore adoption9877d64
lines 109–111why, one step back109}110111export function isEnumerable(base, prop) {fix: add Map getter for DRAFT_STATE, make isEnumerable more resilientkeenondrumsbefore adoption7bbec0a
lines 112–113why, one step back112 const descriptor = Object.getOwnPropertyDescriptor(base, prop)113 return descriptor && descriptor.enumerableAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 114–126why, one step back114}115116export function has(thing, prop) {117 return Object.prototype.hasOwnProperty.call(thing, prop)118}119120export function is(x, y) {121 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js122 if (x === y) {123 return x !== 0 || 1 / x === 1 / y124 } else {125 return x !== x && y !== y126 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 127–130why, one step back127}128129export function isMap(target) {130 return target instanceof MapAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 131why, one step back131}