Skip to content
Myr

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 file

Take 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 back
1export const NOTHING =
2 typeof Symbol !== "undefined"3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}5

feat: limited class supportaleclarsonbefore adoption9877d64

lines 6–7why, one step back
6export const DRAFTABLE =
7 typeof Symbol !== "undefined"

fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c

line 8why, one step back
8 ? Symbol.for("immer-draftable")

feat: limited class supportaleclarsonbefore adoption9877d64

lines 9–10why, one step back
9 : "__$immer_draftable"
10

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 11why, one step back
11export const DRAFT_STATE =

fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c

line 12why, one step back
12 typeof Symbol !== "undefined" ? Symbol.for("immer-state") : "__$immer_state"

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 13why, one step back
13

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 14–15why, one step back
14export function isDraft(value) {
15 return !!value && !!value[DRAFT_STATE]

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 16–17why, one step back
16}
17

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 18why, one step back
18export function isDraftable(value) {

feat: limited class supportaleclarsonbefore adoption9877d64

line 19why, one step back
19 if (!value || typeof value !== "object") return false

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 20–21why, one step back
20 if (Array.isArray(value)) return true
21 const proto = Object.getPrototypeOf(value)

feat: limited class supportaleclarsonbefore adoption9877d64

line 22why, one step back
22 if (!proto || proto === Object.prototype) return true

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 23why, one step back
23 if (isMap(value)) return true

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 24why, one step back
24 if (isSet(value)) return true

feat: limited class supportaleclarsonbefore adoption9877d64

line 25why, one step back
25 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 26–28why, one step back
26}
2728export function original(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 29–30why, one step back
29 if (value && value[DRAFT_STATE]) {
30 return value[DRAFT_STATE].base

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 31–34why, one step back
31 }
32 // otherwise return undefined33}34

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 35–52why, one step back
35// We use Maps as `drafts` for Sets, not Objects
36// 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 back
53 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 back
56 }
57 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 58–60why, one step back
58 })
59 return target60}

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 61–63why, one step back
61
62function assignObjectLegacy(target, ...objOverrides) {63 objOverrides.forEach(function(override) {

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 64–71why, one step back
64 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 back
72export const assign = Object.assign || assignObjectLegacy

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 73why, one step back
73

feat: limited class supportaleclarsonbefore adoption9877d64

line 74why, one step back
74export const ownKeys =

test: polyfillsaleclarsonbefore adoption551d877

line 75why, one step back
75 typeof Reflect !== "undefined" && Reflect.ownKeys

feat: limited class supportaleclarsonbefore adoption9877d64

line 76why, one step back
76 ? Reflect.ownKeys

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

lines 77–78why, one step back
77 : typeof Object.getOwnPropertySymbols !== "undefined"
78 ? obj =>

feat: limited class supportaleclarsonbefore adoption9877d64

lines 79–81why, one step back
79 Object.getOwnPropertyNames(obj).concat(
80 Object.getOwnPropertySymbols(obj)81 )

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

line 82why, one step back
82 : Object.getOwnPropertyNames

feat: limited class supportaleclarsonbefore adoption9877d64

lines 83–85why, one step back
83
84export function shallowCopy(base, invokeGetters = false) {85 if (Array.isArray(base)) return base.slice()

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 86why, one step back
86 if (isMap(base)) return new Map(base)

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 87why, one step back
87 if (isSet(base)) return new Set(base)

feat: limited class supportaleclarsonbefore adoption9877d64

lines 88–93why, one step back
88 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 back
94 let {value} = desc

feat: limited class supportaleclarsonbefore adoption9877d64

lines 95–98why, one step back
95 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 back
99 value = desc.get.call(base)

feat: limited class supportaleclarsonbefore adoption9877d64

lines 100–101why, one step back
100 }
101 if (desc.enumerable) {

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 102why, one step back
102 clone[key] = value

feat: limited class supportaleclarsonbefore adoption9877d64

lines 103–104why, one step back
103 } else {
104 Object.defineProperty(clone, key, {

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 105why, one step back
105 value,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 106–111why, one step back
106 writable: true,
107 configurable: true108 })109 }110 })111 return clone

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 112–114why, one step back
112}
113114export function each(value, cb) {

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 115why, one step back
115 if (Array.isArray(value) || isMap(value) || isSet(value)) {

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 116–117why, one step back
116 value.forEach((entry, index) => cb(index, entry, value))
117 return

feat: limited class supportaleclarsonbefore adoption9877d64

line 118why, one step back
118 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 119why, one step back
119 ownKeys(value).forEach(key => cb(key, value[key], value))

feat: limited class supportaleclarsonbefore adoption9877d64

lines 120–122why, one step back
120}
121122export function isEnumerable(base, prop) {

nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf

lines 123–124why, one step back
123 const desc = Object.getOwnPropertyDescriptor(base, prop)
124 return !!desc && desc.enumerable

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 125–137why, one step back
125}
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 back
138}
139140export function isMap(target) {141 return target instanceof Map

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 142–145why, one step back
142}
143144export function isSet(target) {145 return target instanceof Set

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 146why, one step back
146}