Skip to content
Myr

common.js

This file

Path

src/common.js

105 lines · 3.0 KiB · JavaScript

Last changed by

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

lines 22–23why, one step back
22 if (!proto || proto === Object.prototype) return true
23 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 29–32why, one step back
29 }
30 // otherwise return undefined31}32

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

line 33why, one step back
33export const assign =

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: polyfillsaleclarsonbefore adoption551d877

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 68–69why, one step back
68 }
69 if (desc.enumerable) {

chore: comments and formattingAlec Larsonbefore adoptionff96173

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 73why, one step back
73 value,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 74–79why, one step back
74 writable: true,
75 configurable: true76 })77 }78 })79 return clone

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 80–83why, one step back
80}
8182export function each(value, cb) {83 if (Array.isArray(value)) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 84why, one step back
84 for (let i = 0; i < value.length; i++) cb(i, value[i], value)

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 85why, one step back
85 } else {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 86–91why, one step back
86 ownKeys(value).forEach(key => cb(key, value[key], value))
87 }88}8990export function isEnumerable(base, prop) {91 return Object.getOwnPropertyDescriptor(base, prop).enumerable

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 92–105why, one step back
92}
9394export function has(thing, prop) {95 return Object.prototype.hasOwnProperty.call(thing, prop)96}9798export function is(x, y) {99 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js100 if (x === y) {101 return x !== 0 || 1 / x === 1 / y102 } else {103 return x !== x && y !== y104 }105}