Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 233e1d3 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: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

line 8why, one step back
8

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

lines 11–12why, one step back
11}
12

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

lines 14–20why, one step back
14 if (!value) return false
15 if (typeof value !== "object") return false16 if (Array.isArray(value)) return true17 const proto = Object.getPrototypeOf(value)18 return proto === null || proto === Object.prototype19}20

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

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

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

lines 24–27why, one step back
24 }
25 // otherwise return undefined26}27

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

lines 39–40why, one step back
39export function shallowCopy(value) {
40 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 back
41 const target = value.__proto__ === undefined ? Object.create(null) : {}

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 42why, one step back
42 return assign(target, value)

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

lines 43–46why, one step back
43}
4445export function each(value, cb) {46 if (Array.isArray(value)) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

line 48why, one step back
48 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

lines 50–64why, one step back
50 }
51}5253export function has(thing, prop) {54 return Object.prototype.hasOwnProperty.call(thing, prop)55}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}