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

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 #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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 #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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 #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

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 #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 39why, one step back
39export function shallowCopy(value) {

Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85

line 40why, one step back
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 #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 43why, one step back
43}

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 44why, one step back
44

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 45–46why, one step back
45export 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 #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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 #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 50–52why, one step back
50 }
51}52

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 53why, one step back
53export function has(thing, prop) {

Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85

line 54why, one step back
54 return Object.prototype.hasOwnProperty.call(thing, prop)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 55–56why, one step back
55}
56

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

lines 57–64why, one step back
57export 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}