Skip to content
Myr

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 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

nit: merge isMap and isSet checksAlec Larsonbefore adoption4164f0d

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 25–27why, one step back
25}
2627export function original(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 30–33why, one step back
30 }
31 // otherwise return undefined32}33

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 34–35why, one step back
34// We use Maps as `drafts` for Sets, not Objects
35// See proxy.js

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

lines 36–41why, one step back
36export 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 back
42 }

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

lines 43–44why, one step back
43 target.add(value)
44 }

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 45–47why, one step back
45 return target
46}47

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

lines 48–52why, one step back
48// We use Maps as `drafts` for Maps, not Objects
49// 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 back
53 })
54 return target55}

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 56–58why, one step back
56
57function assignObjectLegacy(target, ...objOverrides) {58 objOverrides.forEach(function(override) {

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 59–66why, one step back
59 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 back
67export const assign = Object.assign || assignObjectLegacy

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 68why, one step back
68

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: polyfillsaleclarsonbefore adoption551d877

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 74–76why, one step back
74 Object.getOwnPropertyNames(obj).concat(
75 Object.getOwnPropertySymbols(obj)76 )

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 78–80why, one step back
78
79export function shallowCopy(base, invokeGetters = false) {80 if (Array.isArray(base)) return base.slice()

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 95–96why, one step back
95 }
96 if (desc.enumerable) {

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 100why, one step back
100 value,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 101–106why, one step back
101 writable: true,
102 configurable: true103 })104 }105 })106 return clone

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 107–109why, one step back
107}
108109export function each(value, cb) {

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 113why, one step back
113 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 115–117why, one step back
115}
116117export function isEnumerable(base, prop) {

nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

lines 123–125why, one step back
123 if (isMap(thing)) {
124 return thing.has(prop)125 }

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 126why, one step back
126 return Object.prototype.hasOwnProperty.call(thing, prop)

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

lines 127–133why, one step back
127}
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 back
134}
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 back
143}
144145export function isMap(target) {146 return target instanceof Map

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 147–150why, one step back
147}
148149export function isSet(target) {150 return target instanceof Set

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 151why, one step back
151}