Skip to content
Myr

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 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: 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: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 34–38why, one step back
34function 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 back
39 }
40 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 41–56why, one step back
41 })
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 back
57 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 58–62why, one step back
58 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 back
63

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: polyfillsaleclarsonbefore adoption551d877

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 69–71why, one step back
69 Object.getOwnPropertyNames(obj).concat(
70 Object.getOwnPropertySymbols(obj)71 )

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 73–75why, one step back
73
74export function shallowCopy(base, invokeGetters = false) {75 if (Array.isArray(base)) return base.slice()

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 89–90why, one step back
89 }
90 if (desc.enumerable) {

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 94why, one step back
94 value,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 95–100why, one step back
95 writable: true,
96 configurable: true97 })98 }99 })100 return clone

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 101–103why, one step back
101}
102103export function each(value, cb) {

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 104–106why, one step back
104 if (Array.isArray(value) || isMap(value)) {
105 value.forEach((entry, index) => cb(index, entry, value))106 return

feat: limited class supportaleclarsonbefore adoption9877d64

line 107why, one step back
107 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 109–111why, one step back
109}
110111export function isEnumerable(base, prop) {

fix: add Map getter for DRAFT_STATE, make isEnumerable more resilientkeenondrumsbefore adoption7bbec0a

lines 112–113why, one step back
112 const descriptor = Object.getOwnPropertyDescriptor(base, prop)
113 return descriptor && descriptor.enumerable

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 114–126why, one step back
114}
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 back
127}
128129export function isMap(target) {130 return target instanceof Map

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 131why, one step back
131}