forked from ljharb/es-abstract
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New]
ES2015
+: add InternalizeJSONProperty
- Loading branch information
Showing
32 changed files
with
763 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
var $TypeError = require('es-errors/type'); | ||
|
||
var Call = require('./Call'); | ||
var CreateDataProperty = require('./CreateDataProperty'); | ||
var EnumerableOwnNames = require('./EnumerableOwnNames'); | ||
var Get = require('./Get'); | ||
var IsArray = require('./IsArray'); | ||
var ToLength = require('./ToLength'); | ||
var ToString = require('./ToString'); | ||
var Type = require('./Type'); | ||
|
||
var forEach = require('../helpers/forEach'); | ||
|
||
// https://262.ecma-international.org/6.0/#sec-internalizejsonproperty | ||
|
||
// note: `reviver` was implicitly closed-over until ES2020, where it becomes a third argument | ||
|
||
module.exports = function InternalizeJSONProperty(holder, name, reviver) { | ||
if (Type(holder) !== 'Object') { | ||
throw new $TypeError('Assertion failed: `holder` is not an Object'); | ||
} | ||
if (typeof name !== 'string') { | ||
throw new $TypeError('Assertion failed: `name` is not a String'); | ||
} | ||
if (typeof reviver !== 'function') { | ||
throw new $TypeError('Assertion failed: `reviver` is not a Function'); | ||
} | ||
|
||
var val = Get(holder, name); // step 1 | ||
|
||
if (Type(val) === 'Object') { // step 3 | ||
var isArray = IsArray(val); // step 3.a | ||
if (isArray) { // step 3.c | ||
var I = 0; // step 3.c.i | ||
|
||
var len = ToLength(Get(val, 'length')); // step 3.b.ii | ||
|
||
while (I < len) { // step 3.b.iv | ||
var newElement = InternalizeJSONProperty(val, ToString(I), reviver); // step 3.b.iv.1 | ||
|
||
if (typeof newElement === 'undefined') { // step 3.b.iv.3 | ||
delete val[ToString(I)]; // step 3.b.iv.3.a | ||
} else { // step 3.b.iv.4 | ||
CreateDataProperty(val, ToString(I), newElement); // step 3.b.iv.4.a | ||
} | ||
|
||
I += 1; // step 3.b.iv.6 | ||
} | ||
} else { | ||
var keys = EnumerableOwnNames(val); // step 3.d.i | ||
|
||
forEach(keys, function (P) { // step 3.d.iii | ||
// eslint-disable-next-line no-shadow | ||
var newElement = InternalizeJSONProperty(val, P, reviver); // step 3.d.iii.1 | ||
|
||
if (typeof newElement === 'undefined') { // step 3.d.iii.3 | ||
delete val[P]; // step 3.d.iii.3.a | ||
} else { // step 3.d.iii.4 | ||
CreateDataProperty(val, P, newElement); // step 3.d.iii.4.a | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return Call(reviver, holder, [name, val]); // step 4 | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
var $TypeError = require('es-errors/type'); | ||
|
||
var Call = require('./Call'); | ||
var CreateDataProperty = require('./CreateDataProperty'); | ||
var EnumerableOwnProperties = require('./EnumerableOwnProperties'); | ||
var Get = require('./Get'); | ||
var IsArray = require('./IsArray'); | ||
var ToLength = require('./ToLength'); | ||
var ToString = require('./ToString'); | ||
var Type = require('./Type'); | ||
|
||
var forEach = require('../helpers/forEach'); | ||
|
||
// https://262.ecma-international.org/8.0/#sec-internalizejsonproperty | ||
|
||
// note: `reviver` was implicitly closed-over until ES2020, where it becomes a third argument | ||
|
||
module.exports = function InternalizeJSONProperty(holder, name, reviver) { | ||
if (Type(holder) !== 'Object') { | ||
throw new $TypeError('Assertion failed: `holder` is not an Object'); | ||
} | ||
if (typeof name !== 'string') { | ||
throw new $TypeError('Assertion failed: `name` is not a String'); | ||
} | ||
if (typeof reviver !== 'function') { | ||
throw new $TypeError('Assertion failed: `reviver` is not a Function'); | ||
} | ||
|
||
var val = Get(holder, name); // step 1 | ||
|
||
if (Type(val) === 'Object') { // step 2 | ||
var isArray = IsArray(val); // step 2.a | ||
if (isArray) { // step 2.b | ||
var I = 0; // step 2.b.i | ||
|
||
var len = ToLength(Get(val, 'length')); // step 2.b.ii | ||
|
||
while (I < len) { // step 2.b.iii | ||
var newElement = InternalizeJSONProperty(val, ToString(I), reviver); // step 2.b.iv.1 | ||
|
||
if (typeof newElement === 'undefined') { // step 2.b.iii.2 | ||
delete val[ToString(I)]; // step 2.b.iii.2.a | ||
} else { // step 2.b.iii.3 | ||
CreateDataProperty(val, ToString(I), newElement); // step 2.b.iii.3.a | ||
} | ||
|
||
I += 1; // step 2.b.iii.4 | ||
} | ||
} else { // step 2.c | ||
var keys = EnumerableOwnProperties(val, 'key'); // step 2.c.i | ||
|
||
forEach(keys, function (P) { // step 2.c.ii | ||
// eslint-disable-next-line no-shadow | ||
var newElement = InternalizeJSONProperty(val, P, reviver); // step 2.c.ii.1 | ||
|
||
if (typeof newElement === 'undefined') { // step 2.c.ii.2 | ||
delete val[P]; // step 2.c.ii.2.a | ||
} else { // step 2.c.ii.3 | ||
CreateDataProperty(val, P, newElement); // step 2.c.ii.3.a | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return Call(reviver, holder, [name, val]); // step 3 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
var $TypeError = require('es-errors/type'); | ||
|
||
var Call = require('./Call'); | ||
var CreateDataProperty = require('./CreateDataProperty'); | ||
var EnumerableOwnPropertyNames = require('./EnumerableOwnPropertyNames'); | ||
var Get = require('./Get'); | ||
var IsArray = require('./IsArray'); | ||
var ToLength = require('./ToLength'); | ||
var ToString = require('./ToString'); | ||
var Type = require('./Type'); | ||
|
||
var forEach = require('../helpers/forEach'); | ||
|
||
// https://262.ecma-international.org/9.0/#sec-internalizejsonproperty | ||
|
||
// note: `reviver` was implicitly closed-over until ES2020, where it becomes a third argument | ||
|
||
module.exports = function InternalizeJSONProperty(holder, name, reviver) { | ||
if (Type(holder) !== 'Object') { | ||
throw new $TypeError('Assertion failed: `holder` is not an Object'); | ||
} | ||
if (typeof name !== 'string') { | ||
throw new $TypeError('Assertion failed: `name` is not a String'); | ||
} | ||
if (typeof reviver !== 'function') { | ||
throw new $TypeError('Assertion failed: `reviver` is not a Function'); | ||
} | ||
|
||
var val = Get(holder, name); // step 1 | ||
|
||
if (Type(val) === 'Object') { // step 2 | ||
var isArray = IsArray(val); // step 2.a | ||
if (isArray) { // step 2.b | ||
var I = 0; // step 2.b.i | ||
|
||
var len = ToLength(Get(val, 'length')); // step 2.b.ii | ||
|
||
while (I < len) { // step 2.b.iii | ||
var newElement = InternalizeJSONProperty(val, ToString(I), reviver); // step 2.b.iv.1 | ||
|
||
if (typeof newElement === 'undefined') { // step 2.b.iii.2 | ||
delete val[ToString(I)]; // step 2.b.iii.2.a | ||
} else { // step 2.b.iii.3 | ||
CreateDataProperty(val, ToString(I), newElement); // step 2.b.iii.3.a | ||
} | ||
|
||
I += 1; // step 2.b.iii.4 | ||
} | ||
} else { // step 2.c | ||
var keys = EnumerableOwnPropertyNames(val, 'key'); // step 2.c.i | ||
|
||
forEach(keys, function (P) { // step 2.c.ii | ||
// eslint-disable-next-line no-shadow | ||
var newElement = InternalizeJSONProperty(val, P, reviver); // step 2.c.ii.1 | ||
|
||
if (typeof newElement === 'undefined') { // step 2.c.ii.2 | ||
delete val[P]; // step 2.c.ii.2.a | ||
} else { // step 2.c.ii.3 | ||
CreateDataProperty(val, P, newElement); // step 2.c.ii.3.a | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return Call(reviver, holder, [name, val]); // step 3 | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.