Skip to content
Myr

common.js

This file

Path

src/common.js

110 lines · 3.1 KiB · JavaScript

Last changed by

test: polyfillsaleclarsonbefore adoption551d877

History of this file

Take it

The walk stopped after 200 commits: lines older than 2cb5496 point at it.

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 6–10why, one step back
6export const DRAFTABLE =
7 typeof Symbol !== "undefined"8 ? Symbol("immer-draftable")9 : "__$immer_draftable"10

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 11–12why, one step back
11export const DRAFT_STATE =
12 typeof Symbol !== "undefined" ? Symbol("immer-state") : "__$immer_state"

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 13why, one step back
13

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 14–15why, one step back
14export function isDraft(value) {
15 return !!value && !!value[DRAFT_STATE]

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 16–17why, one step back
16}
17

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 19why, one step back
19 if (!value || typeof value !== "object") return false

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 20–21why, one step back
20 if (Array.isArray(value)) return true
21 const proto = Object.getPrototypeOf(value)

feat: limited class supportaleclarsonbefore adoption9877d64

lines 22–23why, one step back
22 if (!proto || proto === Object.prototype) return true
23 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 24–25why, one step back
24}
25

Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2

line 26why, one step back
26export function original(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 27–28why, one step back
27 if (value && value[DRAFT_STATE]) {
28 return value[DRAFT_STATE].base

Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2

lines 29–32why, one step back
29 }
30 // otherwise return undefined31}32

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 34–43why, one step back
34 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 }43

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: polyfillsaleclarsonbefore adoption551d877

line 45why, one step back
45 typeof Reflect !== "undefined" && Reflect.ownKeys

feat: limited class supportaleclarsonbefore adoption9877d64

lines 46–76why, one step back
46 ? Reflect.ownKeys
47 : obj =>48 Object.getOwnPropertyNames(obj).concat(49 Object.getOwnPropertySymbols(obj)50 )5152export function shallowCopy(base, invokeGetters = false) {53 if (Array.isArray(base)) return base.slice()54 const clone = Object.create(Object.getPrototypeOf(base))55 ownKeys(base).forEach(key => {56 if (key === DRAFT_STATE) {57 return // Never copy over draft state.58 }59 const desc = Object.getOwnPropertyDescriptor(base, key)60 if (desc.get) {61 if (!invokeGetters) {62 throw new Error("Immer drafts cannot have computed properties")63 }64 desc.value = desc.get.call(base)65 }66 if (desc.enumerable) {67 clone[key] = desc.value68 } else {69 Object.defineProperty(clone, key, {70 value: desc.value,71 writable: true,72 configurable: true73 })74 }75 })76 return clone

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 77–80why, one step back
77}
7879export function each(value, cb) {80 if (Array.isArray(value)) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 81why, one step back
81 for (let i = 0; i < value.length; i++) cb(i, value[i], value)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 82why, one step back
82 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 83why, one step back
83 for (let key in value) cb(key, value[key], value)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 84why, one step back
84 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 85–96why, one step back
85}
8687export function eachOwn(value, cb) {88 if (Array.isArray(value)) {89 for (let i = 0; i < value.length; i++) cb(i, value[i], value)90 } else {91 ownKeys(value).forEach(key => cb(key, value[key], value))92 }93}9495export function isEnumerable(base, prop) {96 return Object.getOwnPropertyDescriptor(base, prop).enumerable

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 97–110why, one step back
97}
9899export function has(thing, prop) {100 return Object.prototype.hasOwnProperty.call(thing, prop)101}102103export function is(x, y) {104 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js105 if (x === y) {106 return x !== 0 || 1 / x === 1 / y107 } else {108 return x !== x && y !== y109 }110}