Skip to content
Myr

common.js

This file

Path

src/common.js

145 lines · 4.1 KiB · JavaScript

Last changed by

nit: merge isMap and isSet checksAlec Larsonbefore adoption4164f0d

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–51why, one step back
34// We use Maps as `drafts` for Sets, not Objects
35// See proxy.js36export function assignSet(target, ...mapOverrides) {37 mapOverrides.forEach(override => {38 for (const value of override.values()) {39 // When we add new drafts we have to remove their originals if present40 const originalValue = original(value)41 if (originalValue) {42 target.delete(originalValue)43 }44 target.add(value)45 }46 })47 return target48}4950export function assignMap(target, ...objOverrides) {51 objOverrides.forEach(override => {

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 52–54why, one step back
52 for (let key in override) {
53 if (has(override, key)) {54 target.set(key, override[key])

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 55–56why, one step back
55 }
56 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 57–59why, one step back
57 })
58 return target59}

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 63–70why, one step back
63 for (let key in override) {
64 if (has(override, key)) {65 target[key] = override[key]66 }67 }68 })69 return target70}

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 71why, one step back
71export const assign = Object.assign || assignObjectLegacy

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 72why, one step back
72

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: polyfillsaleclarsonbefore adoption551d877

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 87–92why, one step back
87 const clone = Object.create(Object.getPrototypeOf(base))
88 ownKeys(base).forEach(key => {89 if (key === DRAFT_STATE) {90 return // Never copy over draft state.91 }92 const desc = Object.getOwnPropertyDescriptor(base, key)

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 93why, one step back
93 let {value} = desc

feat: limited class supportaleclarsonbefore adoption9877d64

lines 94–97why, one step back
94 if (desc.get) {
95 if (!invokeGetters) {96 throw new Error("Immer drafts cannot have computed properties")97 }

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 98why, one step back
98 value = desc.get.call(base)

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 104why, one step back
104 value,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 105–110why, one step back
105 writable: true,
106 configurable: true107 })108 }109 })110 return clone

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 111–113why, one step back
111}
112113export function each(value, cb) {

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 117why, one step back
117 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 119–121why, one step back
119}
120121export function isEnumerable(base, prop) {

nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 124–136why, one step back
124}
125126export function has(thing, prop) {127 return Object.prototype.hasOwnProperty.call(thing, prop)128}129130export function is(x, y) {131 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js132 if (x === y) {133 return x !== 0 || 1 / x === 1 / y134 } else {135 return x !== x && y !== y136 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 137–140why, one step back
137}
138139export function isMap(target) {140 return target instanceof Map

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 141–144why, one step back
141}
142143export function isSet(target) {144 return target instanceof Set

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 145why, one step back
145}