forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfromJS.js
More file actions
103 lines (85 loc) · 2.66 KB
/
fromJS.js
File metadata and controls
103 lines (85 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
/**
* Immutable JS conversion utils
* @deprecated
* @module utils/fromJS
*/
import * as I from "immutable";
import { isFunction } from "lodash";
// hasOwnProperty is defensive because it is possible that the
// object that we're creating a map for has a `hasOwnProperty` field
function hasOwnProperty(value, key) {
if (value.hasOwnProperty && isFunction(value.hasOwnProperty)) {
return value.hasOwnProperty(key);
}
if (value.prototype && value.prototype.hasOwnProperty) {
return value.prototype.hasOwnProperty(key);
}
return false;
}
/*
creates an immutable map, where each of the value's
items are transformed into their own map.
NOTE: we guard against `length` being a property because
length confuses Immutable's internal algorithm.
*/
function createMap(value) {
const hasLength = hasOwnProperty(value, "length");
const length = value.length;
if (hasLength) {
value.length = `${value.length}`;
}
let map = I.Seq(value)
.map(fromJS)
.toMap();
if (hasLength) {
map = map.set("length", length);
value.length = length;
}
return map;
}
function createList(value) {
return I.Seq(value)
.map(fromJS)
.toList();
}
/**
* When our app state is fully typed, we should be able to get rid of
* this function. This is only temporarily necessary to support
* converting typed objects to immutable.js, which usually happens in
* reducers.
*
* @memberof utils/fromJS
* @static
*/
function fromJS(value: any): any {
if (Array.isArray(value)) {
return createList(value);
}
if (value && value.constructor && value.constructor.meta) {
// This adds support for tcomb objects which are native JS objects
// but are not "plain", so the above checks fail. Since they
// behave the same we can use the same constructors, but we need
// special checks for them.
const kind = value.constructor.meta.kind;
if (kind === "struct") {
return createMap(value);
} else if (kind === "list") {
return createList(value);
}
}
// If it's a primitive type, just return the value. Note `==` check
// for null, which is intentionally used to match either `null` or
// `undefined`.
if (value == null || typeof value !== "object") {
return value;
}
// Otherwise, treat it like an object. We can't reliably detect if
// it's a plain object because we might be objects from other JS
// contexts so `Object !== Object`.
return createMap(value);
}
module.exports = fromJS;