common.js
This file
Path
src/common.js
64 lines · 1.7 KiB · JavaScript
Last changed by
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
History of this fileTake it
The walk stopped after 200 commits: lines older than 5e16b30 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: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 6–7why, one step back6export const DRAFT_STATE =7 typeof Symbol !== "undefined" ? Symbol("immer-state") : "__$immer_state"Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
line 8why, one step back8feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 9–10why, one step back9export function isDraft(value) {10 return !!value && !!value[DRAFT_STATE]Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
lines 11–12why, one step back11}12feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 13why, one step back13export function isDraftable(value) {Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
lines 14–20why, one step back14 if (!value) return false15 if (typeof value !== "object") return false16 if (Array.isArray(value)) return true17 const proto = Object.getPrototypeOf(value)18 return proto === null || proto === Object.prototype19}20Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
line 21why, one step back21export function original(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 22–23why, one step back22 if (value && value[DRAFT_STATE]) {23 return value[DRAFT_STATE].baseMerge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
lines 24–27why, one step back24 }25 // otherwise return undefined26}27fix: always use `assign` polyfillaleclarsonbefore adoption241a648
line 28why, one step back28export const assign =Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 29–38why, one step back29 Object.assign ||30 function assign(target, value) {31 for (let key in value) {32 if (has(value, key)) {33 target[key] = value[key]34 }35 }36 return target37 }38Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
line 39why, one step back39export function shallowCopy(value) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 40why, one step back40 if (Array.isArray(value)) return value.slice()Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
line 41why, one step back41 const target = value.__proto__ === undefined ? Object.create(null) : {}Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 42why, one step back42 return assign(target, value)Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
lines 43–46why, one step back43}4445export function each(value, cb) {46 if (Array.isArray(value)) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 47why, one step back47 for (let i = 0; i < value.length; i++) cb(i, value[i], value)Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
line 48why, one step back48 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 49why, one step back49 for (let key in value) cb(key, value[key], value)Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
lines 50–53why, one step back50 }51}5253export function has(thing, prop) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 54why, one step back54 return Object.prototype.hasOwnProperty.call(thing, prop)Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
lines 55–64why, one step back55}5657export function is(x, y) {58 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js59 if (x === y) {60 return x !== 0 || 1 / x === 1 / y61 } else {62 return x !== x && y !== y63 }64}