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