Skip to content
Myr

common.js

This file

Path

src/common.js

108 lines · 3.1 KiB · JavaScript

Last changed by

fix: ensure Symbol.for exists before calling it (#366)Fabrizio Catittibefore adoptionc8ecbad

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

line 6why, one step back
6export const DRAFTABLE =

fix: ensure Symbol.for exists before calling it (#366)Fabrizio Catittibefore adoptionc8ecbad

line 7why, one step back
7 typeof Symbol !== "undefined" && Symbol.for

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: ensure Symbol.for exists before calling it (#366)Fabrizio Catittibefore adoptionc8ecbad

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 15why, one step back
15

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 18–19why, one step back
18}
19

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 24–25why, one step back
24 if (!proto || proto === Object.prototype) return true
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

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 35–40why, one step back
35export const assign =
36 Object.assign ||37 function assign(target, value) {38 for (let key in value) {39 if (has(value, key)) {40 target[key] = value[key]

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 41–42why, one step back
41 }
42 }

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 43why, one step back
43 return target

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 44–45why, one step back
44 }
45

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: polyfillsaleclarsonbefore adoption551d877

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 51–53why, one step back
51 Object.getOwnPropertyNames(obj).concat(
52 Object.getOwnPropertySymbols(obj)53 )

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 55–63why, one step back
55
56export function shallowCopy(base, invokeGetters = false) {57 if (Array.isArray(base)) return base.slice()58 const clone = Object.create(Object.getPrototypeOf(base))59 ownKeys(base).forEach(key => {60 if (key === DRAFT_STATE) {61 return // Never copy over draft state.62 }63 const desc = Object.getOwnPropertyDescriptor(base, key)

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 65–68why, one step back
65 if (desc.get) {
66 if (!invokeGetters) {67 throw new Error("Immer drafts cannot have computed properties")68 }

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 70–71why, one step back
70 }
71 if (desc.enumerable) {

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 75why, one step back
75 value,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 76–81why, one step back
76 writable: true,
77 configurable: true78 })79 }80 })81 return clone

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 82–84why, one step back
82}
8384export function each(value, cb) {

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 85–88why, one step back
85 if (Array.isArray(value)) {
86 for (let i = 0; i < value.length; i++) cb(i, value[i], value)87 } else {88 ownKeys(value).forEach(key => cb(key, value[key], value))

feat: limited class supportaleclarsonbefore adoption9877d64

lines 89–92why, one step back
89 }
90}9192export function isEnumerable(base, prop) {

nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoptionb967bab

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 95–108why, one step back
95}
9697export function has(thing, prop) {98 return Object.prototype.hasOwnProperty.call(thing, prop)99}100101export function is(x, y) {102 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js103 if (x === y) {104 return x !== 0 || 1 / x === 1 / y105 } else {106 return x !== x && y !== y107 }108}