common.js
This file
Path
src/common.js
104 lines · 3.0 KiB · JavaScript
Last changed by
fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
History of this fileTake it
The walk stopped after 200 commits: lines older than 5b8ed13 point at it.
Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 1–5why, one step back1export const NOTHING =2 typeof Symbol !== "undefined"3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}5feat: limited class supportaleclarsonbefore adoption9877d64
lines 6–10why, one step back6export const DRAFTABLE =7 typeof Symbol !== "undefined"8 ? Symbol("immer-draftable")9 : "__$immer_draftable"10feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 11–12why, one step back11export const DRAFT_STATE =12 typeof Symbol !== "undefined" ? Symbol("immer-state") : "__$immer_state"Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 13why, one step back13feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 14–15why, one step back14export function isDraft(value) {15 return !!value && !!value[DRAFT_STATE]Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 16–17why, one step back16}17feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 18why, one step back18export function isDraftable(value) {feat: limited class supportaleclarsonbefore adoption9877d64
line 19why, one step back19 if (!value || typeof value !== "object") return falseUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 20–21why, one step back20 if (Array.isArray(value)) return true21 const proto = Object.getPrototypeOf(value)feat: limited class supportaleclarsonbefore adoption9877d64
lines 22–23why, one step back22 if (!proto || proto === Object.prototype) return true23 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 24–25why, one step back24}25Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
line 26why, one step back26export function original(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 27–28why, one step back27 if (value && value[DRAFT_STATE]) {28 return value[DRAFT_STATE].baseMerge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
lines 29–32why, one step back29 }30 // otherwise return undefined31}32fix: always use `assign` polyfillaleclarsonbefore adoption241a648
line 33why, one step back33export const assign =Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 34–43why, one step back34 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 }43feat: limited class supportaleclarsonbefore adoption9877d64
line 44why, one step back44export const ownKeys =test: polyfillsaleclarsonbefore adoption551d877
line 45why, one step back45 typeof Reflect !== "undefined" && Reflect.ownKeysfeat: limited class supportaleclarsonbefore adoption9877d64
line 46why, one step back46 ? Reflect.ownKeysfix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
lines 47–48why, one step back47 : typeof Object.getOwnPropertySymbols !== "undefined"48 ? obj =>feat: limited class supportaleclarsonbefore adoption9877d64
lines 49–51why, one step back49 Object.getOwnPropertyNames(obj).concat(50 Object.getOwnPropertySymbols(obj)51 )fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
line 52why, one step back52 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 53–78why, one step back5354export 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)62 if (desc.get) {63 if (!invokeGetters) {64 throw new Error("Immer drafts cannot have computed properties")65 }66 desc.value = desc.get.call(base)67 }68 if (desc.enumerable) {69 clone[key] = desc.value70 } else {71 Object.defineProperty(clone, key, {72 value: desc.value,73 writable: true,74 configurable: true75 })76 }77 })78 return cloneUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 79–82why, one step back79}8081export function each(value, cb) {82 if (Array.isArray(value)) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 83why, one step back83 for (let i = 0; i < value.length; i++) cb(i, value[i], value)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 84why, one step back84 } else {feat: limited class supportaleclarsonbefore adoption9877d64
lines 85–90why, one step back85 ownKeys(value).forEach(key => cb(key, value[key], value))86 }87}8889export function isEnumerable(base, prop) {90 return Object.getOwnPropertyDescriptor(base, prop).enumerableUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 91–104why, one step back91}9293export function has(thing, prop) {94 return Object.prototype.hasOwnProperty.call(thing, prop)95}9697export function is(x, y) {98 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js99 if (x === y) {100 return x !== 0 || 1 / x === 1 / y101 } else {102 return x !== x && y !== y103 }104}