Skip to content
Myr

common.js

This file

Path

src/common.js

129 lines · 3.2 KiB · JavaScript

Last changed by

BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581

History of this file

Take it

The walk stopped after 200 commits: lines older than 72850a9 point at it.

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 1why, one step back
1export const NOTHING =

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 2–4why, one step back
2 typeof Symbol !== "undefined"
3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 5why, one step back
5

feat: limited class supportaleclarsonbefore adoption9877d64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 7–9why, one step back
7 typeof Symbol !== "undefined" && Symbol.for
8 ? Symbol.for("immer-draftable")9 : "__$immer_draftable"

feat: limited class supportaleclarsonbefore adoption9877d64

line 10why, one step back
10

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 11why, one step back
11export const DRAFT_STATE =

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 15why, one step back
15

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 16why, one step back
16export function isDraft(value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 17why, one step back
17 return !!value && !!value[DRAFT_STATE]

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

lines 21–27why, one step back
21 if (!value) return false
22 return (23 isPlainObject(value) || !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]24 )25}2627export function isPlainObject(value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 28–30why, one step back
28 if (!value || typeof value !== "object") return false
29 if (Array.isArray(value)) return true30 const proto = Object.getPrototypeOf(value)

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

line 31why, one step back
31 return !proto || proto === Object.prototype

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 32–34why, one step back
32}
3334export function original(value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 35–38why, one step back
35 if (value && value[DRAFT_STATE]) {
36 return value[DRAFT_STATE].base37 }38 // otherwise return undefined

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 39–40why, one step back
39}
40

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 42–50why, one step back
42 Object.assign ||
43 function assign(target, value) {44 for (let key in value) {45 if (has(value, key)) {46 target[key] = value[key]47 }48 }49 return target50 }

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 51why, one step back
51

feat: limited class supportaleclarsonbefore adoption9877d64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 53–60why, one step back
53 typeof Reflect !== "undefined" && Reflect.ownKeys
54 ? Reflect.ownKeys55 : typeof Object.getOwnPropertySymbols !== "undefined"56 ? obj =>57 Object.getOwnPropertyNames(obj).concat(58 Object.getOwnPropertySymbols(obj)59 )60 : Object.getOwnPropertyNames

feat: limited class supportaleclarsonbefore adoption9877d64

lines 61–62why, one step back
61
62export function shallowCopy(base, invokeGetters = false) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 63–87why, one step back
63 if (Array.isArray(base)) return base.slice()
64 const clone = Object.create(Object.getPrototypeOf(base))65 ownKeys(base).forEach(key => {66 if (key === DRAFT_STATE) {67 return // Never copy over draft state.68 }69 const desc = Object.getOwnPropertyDescriptor(base, key)70 let {value} = desc71 if (desc.get) {72 if (!invokeGetters) {73 throw new Error("Immer drafts cannot have computed properties")74 }75 value = desc.get.call(base)76 }77 if (desc.enumerable) {78 clone[key] = value79 } else {80 Object.defineProperty(clone, key, {81 value,82 writable: true,83 configurable: true84 })85 }86 })87 return clone

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 88–90why, one step back
88}
8990export function each(value, cb) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 91–95why, one step back
91 if (Array.isArray(value)) {
92 for (let i = 0; i < value.length; i++) cb(i, value[i], value)93 } else {94 ownKeys(value).forEach(key => cb(key, value[key], value))95 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 96–98why, one step back
96}
9798export function isEnumerable(base, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 101–103why, one step back
101}
102103export function has(thing, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 105–107why, one step back
105}
106107export function is(x, y) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 108–113why, one step back
108 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
109 if (x === y) {110 return x !== 0 || 1 / x === 1 / y111 } else {112 return x !== x && y !== y113 }

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

lines 114–121why, one step back
114}
115116export function clone(obj) {117 if (!isDraftable(obj)) return obj118 if (Array.isArray(obj)) return obj.map(clone)119 const cloned = Object.create(Object.getPrototypeOf(obj))120 for (const key in obj) cloned[key] = clone(obj[key])121 return cloned

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 122why, one step back
122}

BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581

lines 123–129why, one step back
123
124export function deepFreeze(obj) {125 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return126 Object.freeze(obj)127 if (Array.isArray(obj)) obj.forEach(deepFreeze)128 else for (const key in obj) deepFreeze(obj[key])129}