1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var Call = require('./Call');
|
---|
6 | var CreateDataProperty = require('./CreateDataProperty');
|
---|
7 | var EnumerableOwnProperties = require('./EnumerableOwnProperties');
|
---|
8 | var Get = require('./Get');
|
---|
9 | var IsArray = require('./IsArray');
|
---|
10 | var ToLength = require('./ToLength');
|
---|
11 | var ToString = require('./ToString');
|
---|
12 | var Type = require('./Type');
|
---|
13 |
|
---|
14 | var forEach = require('../helpers/forEach');
|
---|
15 |
|
---|
16 | // https://262.ecma-international.org/8.0/#sec-internalizejsonproperty
|
---|
17 |
|
---|
18 | // note: `reviver` was implicitly closed-over until ES2020, where it becomes a third argument
|
---|
19 |
|
---|
20 | module.exports = function InternalizeJSONProperty(holder, name, reviver) {
|
---|
21 | if (Type(holder) !== 'Object') {
|
---|
22 | throw new $TypeError('Assertion failed: `holder` is not an Object');
|
---|
23 | }
|
---|
24 | if (typeof name !== 'string') {
|
---|
25 | throw new $TypeError('Assertion failed: `name` is not a String');
|
---|
26 | }
|
---|
27 | if (typeof reviver !== 'function') {
|
---|
28 | throw new $TypeError('Assertion failed: `reviver` is not a Function');
|
---|
29 | }
|
---|
30 |
|
---|
31 | var val = Get(holder, name); // step 1
|
---|
32 |
|
---|
33 | if (Type(val) === 'Object') { // step 2
|
---|
34 | var isArray = IsArray(val); // step 2.a
|
---|
35 | if (isArray) { // step 2.b
|
---|
36 | var I = 0; // step 2.b.i
|
---|
37 |
|
---|
38 | var len = ToLength(Get(val, 'length')); // step 2.b.ii
|
---|
39 |
|
---|
40 | while (I < len) { // step 2.b.iii
|
---|
41 | var newElement = InternalizeJSONProperty(val, ToString(I), reviver); // step 2.b.iv.1
|
---|
42 |
|
---|
43 | if (typeof newElement === 'undefined') { // step 2.b.iii.2
|
---|
44 | delete val[ToString(I)]; // step 2.b.iii.2.a
|
---|
45 | } else { // step 2.b.iii.3
|
---|
46 | CreateDataProperty(val, ToString(I), newElement); // step 2.b.iii.3.a
|
---|
47 | }
|
---|
48 |
|
---|
49 | I += 1; // step 2.b.iii.4
|
---|
50 | }
|
---|
51 | } else { // step 2.c
|
---|
52 | var keys = EnumerableOwnProperties(val, 'key'); // step 2.c.i
|
---|
53 |
|
---|
54 | forEach(keys, function (P) { // step 2.c.ii
|
---|
55 | // eslint-disable-next-line no-shadow
|
---|
56 | var newElement = InternalizeJSONProperty(val, P, reviver); // step 2.c.ii.1
|
---|
57 |
|
---|
58 | if (typeof newElement === 'undefined') { // step 2.c.ii.2
|
---|
59 | delete val[P]; // step 2.c.ii.2.a
|
---|
60 | } else { // step 2.c.ii.3
|
---|
61 | CreateDataProperty(val, P, newElement); // step 2.c.ii.3.a
|
---|
62 | }
|
---|
63 | });
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | return Call(reviver, holder, [name, val]); // step 3
|
---|
68 | };
|
---|