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 fileTake 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 back1export const NOTHING =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 2–4why, one step back2 typeof Symbol !== "undefined"3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 5why, one step back5feat: limited class supportaleclarsonbefore adoption9877d64
line 6why, one step back6export const DRAFTABLE =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 7–9why, one step back7 typeof Symbol !== "undefined" && Symbol.for8 ? Symbol.for("immer-draftable")9 : "__$immer_draftable"feat: limited class supportaleclarsonbefore adoption9877d64
line 10why, one step back10feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11export const DRAFT_STATE =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 12–14why, one step back12 typeof Symbol !== "undefined" && Symbol.for13 ? Symbol.for("immer-state")14 : "__$immer_state"fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 15why, one step back15feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 16why, one step back16export function isDraft(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 17why, one step back17 return !!value && !!value[DRAFT_STATE]fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 18–19why, one step back18}19feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 20why, one step back20export function isDraftable(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 21–25why, one step back21 if (!value || typeof value !== "object") return false22 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 back26}2728export function original(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 29–32why, one step back29 if (value && value[DRAFT_STATE]) {30 return value[DRAFT_STATE].base31 }32 // otherwise return undefinedfix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 33–34why, one step back33}34fix: revert Map supportAlec Larsonbefore adoption47529d3
line 35why, one step back35export const assign =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 36–44why, one step back36 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 back45feat: limited class supportaleclarsonbefore adoption9877d64
line 46why, one step back46export const ownKeys =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 47–54why, one step back47 typeof Reflect !== "undefined" && Reflect.ownKeys48 ? Reflect.ownKeys49 : typeof Object.getOwnPropertySymbols !== "undefined"50 ? obj =>51 Object.getOwnPropertyNames(obj).concat(52 Object.getOwnPropertySymbols(obj)53 )54 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 55–56why, one step back5556export function shallowCopy(base, invokeGetters = false) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 57–81why, one step back57 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 clonefix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 82–84why, one step back82}8384export function each(value, cb) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 85–89why, one step back85 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 back90}9192export function isEnumerable(base, prop) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 93–94why, one step back93 const desc = Object.getOwnPropertyDescriptor(base, prop)94 return !!desc && desc.enumerablefix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 95–97why, one step back95}9697export function has(thing, prop) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 98why, one step back98 return Object.prototype.hasOwnProperty.call(thing, prop)fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 99–101why, one step back99}100101export function is(x, y) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 102–107why, one step back102 // 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 }fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 108why, one step back108}