Skip to content
Myr

common.js

This file

Path

src/common.js

108 lines · 2.6 KiB · JavaScript

Last changed by

style: use tabsuse-tabsbefore adoptiona9c0fd0

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) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 21–25why, one step back
21 if (!value || typeof value !== "object") return false
22 if (Array.isArray(value)) return true23 const proto = Object.getPrototypeOf(value)24 if (!proto || proto === Object.prototype) return true25 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 29–32why, one step back
29 if (value && value[DRAFT_STATE]) {
30 return value[DRAFT_STATE].base31 }32 // otherwise return undefined

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 33–34why, one step back
33}
34

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 45why, one step back
45

feat: limited class supportaleclarsonbefore adoption9877d64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 47–54why, one step back
47 typeof Reflect !== "undefined" && Reflect.ownKeys
48 ? Reflect.ownKeys49 : typeof Object.getOwnPropertySymbols !== "undefined"50 ? obj =>51 Object.getOwnPropertyNames(obj).concat(52 Object.getOwnPropertySymbols(obj)53 )54 : Object.getOwnPropertyNames

feat: limited class supportaleclarsonbefore adoption9877d64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 57–81why, one step back
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)64 let {value} = desc65 if (desc.get) {66 if (!invokeGetters) {67 throw new Error("Immer drafts cannot have computed properties")68 }69 value = desc.get.call(base)70 }71 if (desc.enumerable) {72 clone[key] = value73 } else {74 Object.defineProperty(clone, key, {75 value,76 writable: true,77 configurable: true78 })79 }80 })81 return clone

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 85–89why, 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))89 }

feat: limited class supportaleclarsonbefore adoption9877d64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 95–97why, one step back
95}
9697export function has(thing, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 99–101why, one step back
99}
100101export function is(x, y) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 102–107why, one step back
102 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
103 if (x === y) {104 return x !== 0 || 1 / x === 1 / y105 } else {106 return x !== x && y !== y107 }

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 108why, one step back
108}