New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

xcomponent

Package Overview
Dependencies
Maintainers
2
Versions
306
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xcomponent - npm Package Compare versions

Comparing version 6.0.59 to 6.0.60

153

dist/module/component/child/index.js

@@ -12,2 +12,4 @@ 'use strict';

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

@@ -75,9 +77,40 @@

_this.component.xchild = _this;
_this.setProps(_this.getInitialProps(), (0, _window.getParentDomain)());
var _arr = [_this.component, window];
// update logLevel with prop.logLevel to override defaultLogLevel configured when creating component
var _loop = function _loop() {
var item = _arr[_i];var _arr2 = [['xchild', function () {
return _this;
}], ['xprops', function () {
return _this.props;
}]];
if (_this.props.logLevel) {
(0, _lib.setLogLevel)(_this.props.logLevel);
var _loop2 = function _loop2() {
var _arr2$_i = _slicedToArray(_arr2[_i2], 2),
name = _arr2$_i[0],
getter = _arr2$_i[1];
// $FlowFixMe
Object.defineProperty(item, name, {
configurable: true,
get: function get() {
if (!_this.props) {
_this.setProps(_this.getInitialProps(), (0, _window.getParentDomain)());
}
// $FlowFixMe
delete item[name];
// $FlowFixMe
item[name] = getter();
// $FlowFixMe
return item[name];
}
});
};
for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
_loop2();
}
};
for (var _i = 0; _i < _arr.length; _i++) {
_loop();
}

@@ -126,6 +159,8 @@

this.sendToParent(_constants.POST_MESSAGE.ONRESIZE, {}, { fireAndForget: true });
window.addEventListener('resize', function () {
_this2.sendToParent(_constants.POST_MESSAGE.ONRESIZE, {}, { fireAndForget: true });
});
if (this.component.listenForResize) {
this.sendToParent(_constants.POST_MESSAGE.ONRESIZE, {}, { fireAndForget: true });
window.addEventListener('resize', function () {
_this2.sendToParent(_constants.POST_MESSAGE.ONRESIZE, {}, { fireAndForget: true });
});
}
}

@@ -192,3 +227,3 @@ }, {

props = global.props[componentMeta.uid];
props = JSON.parse(global.props[componentMeta.uid]);
} else {

@@ -211,3 +246,3 @@ throw new Error('Unrecognized props type: ' + props.type);

if (typeof func !== 'function') {
throw new TypeError('Expected ' + (typeof func === 'undefined' ? 'undefined' : _typeof(func)) + ' to be function');
throw new TypeError('Expected ' + fullKey + ' to be function, got ' + (typeof func === 'undefined' ? 'undefined' : _typeof(func)));
}

@@ -228,14 +263,15 @@

(0, _lib.extend)(this.props, normalizedProps);
window.xprops = this.props;
this.component.xprops = this.props;
for (var _iterator = this.onPropHandlers, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
if (this.props.logLevel) {
(0, _lib.setLogLevel)(this.props.logLevel);
}
for (var _iterator = this.onPropHandlers, _isArray = Array.isArray(_iterator), _i3 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref3;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref3 = _iterator[_i++];
if (_i3 >= _iterator.length) break;
_ref3 = _iterator[_i3++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref3 = _i.value;
_i3 = _iterator.next();
if (_i3.done) break;
_ref3 = _i3.value;
}

@@ -475,12 +511,12 @@

for (var _iterator2 = history, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
for (var _iterator2 = history, _isArray2 = Array.isArray(_iterator2), _i4 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref6;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref6 = _iterator2[_i2++];
if (_i4 >= _iterator2.length) break;
_ref6 = _iterator2[_i4++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref6 = _i2.value;
_i4 = _iterator2.next();
if (_i4.done) break;
_ref6 = _i4.value;
}

@@ -597,67 +633,2 @@

return ChildComponent;
}(_base.BaseComponent);
if (__XCOMPONENT__.__CHILD_WINDOW_ENFORCE_LOG_LEVEL__) {
if ((0, _window.isXComponentWindow)() && window.console) {
var _loop = function _loop() {
if (_isArray3) {
if (_i3 >= _iterator3.length) return 'break';
_ref7 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) return 'break';
_ref7 = _i3.value;
}
var level = _ref7;
try {
var original = window.console[level];
if (!original || !original.apply) {
return 'continue';
}
window.console[level] = function consoleLevel() {
try {
var logLevel = window.LOG_LEVEL;
if (!logLevel || _client.logLevels.indexOf(logLevel) === -1) {
return original.apply(this, arguments);
}
if (_client.logLevels.indexOf(level) > _client.logLevels.indexOf(logLevel)) {
return;
}
return original.apply(this, arguments);
} catch (err2) {// eslint-disable-line unicorn/catch-error-name
// pass
}
};
if (level === 'info') {
window.console.log = window.console[level];
}
} catch (err) {
// pass
}
};
_loop2: for (var _iterator3 = _client.logLevels, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref7;
var _ret = _loop();
switch (_ret) {
case 'break':
break _loop2;
case 'continue':
continue;}
}
}
}
}(_base.BaseComponent);

@@ -134,2 +134,3 @@ 'use strict';

_this.addProp(options, 'scrolling');
_this.addProp(options, 'listenForResize');

@@ -278,12 +279,7 @@ _this.addProp(options, 'version', 'latest');

if ((0, _window.isXComponentWindow)()) {
_src2.ZalgoPromise['try'](function () {
var componentMeta = (0, _window.getComponentMeta)();
if (componentMeta.tag === _this2.tag) {
window.xchild = new _child.ChildComponent(_this2);
window.xprops = window.xchild.props;
}
});
}
return _src2.ZalgoPromise['try'](function () {
if (_this2.isChild()) {
return new _child.ChildComponent(_this2);
}
});
}

@@ -480,3 +476,3 @@ }, {

value: function isChild() {
return (0, _window.isXComponentWindow)() && window.xprops && (0, _window.getComponentMeta)().tag === this.tag;
return (0, _window.isXComponentWindow)() && (0, _window.getComponentMeta)().tag === this.tag;
}

@@ -483,0 +479,0 @@ }, {

@@ -140,3 +140,3 @@ 'use strict';

// When the component ePperiences an error
// When the component experiences an error

@@ -148,5 +148,12 @@ onError: {

sendToChild: true,
once: true
once: true,
def: function def() {
return function onError(err) {
setTimeout(function () {
throw err;
});
};
}
}
};
}

@@ -419,3 +419,3 @@ 'use strict';

if (props.type === _constants.INITIAL_PROPS.UID) {
_lib.global.props[uid] = sProps;
_lib.global.props[uid] = JSON.stringify(sProps);

@@ -422,0 +422,0 @@ this.clean.register(function () {

@@ -30,2 +30,10 @@ 'use strict';

function encode(str) {
return _hiBase2['default'].encode(str).replace(/\=/g, '').toLowerCase(); // eslint-disable-line no-useless-escape
}
function decode(str) {
return _hiBase2['default'].decode(str.toUpperCase());
}
/* Build Child Window Name

@@ -52,3 +60,3 @@ -----------------------

var encodedVersion = normalize(version);
var encodedOptions = _hiBase2['default'].encode(JSON.stringify(options)).replace(/\=/g, '').toLowerCase(); // eslint-disable-line no-useless-escape
var encodedOptions = encode(JSON.stringify(options));

@@ -109,5 +117,5 @@ if (!encodedName) {

try {
componentMeta = JSON.parse(_hiBase2['default'].decode(encodedOptions.toUpperCase()));
componentMeta = JSON.parse(decode(encodedOptions));
} catch (err) {
throw new Error('Can not decode component-meta');
throw new Error('Can not decode component-meta: ' + encodedOptions + ' ' + (0, _lib.stringifyError)(err));
}

@@ -114,0 +122,0 @@

@@ -92,4 +92,4 @@ 'use strict';

scopeProps = (0, _lib.replaceObject)(scopeProps, function (value) {
if (typeof value === 'function') {
scopeProps = (0, _lib.replaceObject)(scopeProps, {
'function': function _function(value) {
return function angularWrapped() {

@@ -96,0 +96,0 @@ var result = value.apply(this, arguments);

@@ -27,12 +27,14 @@ 'use strict';

var getProps = function getProps(component) {
return (0, _lib.replaceObject)(_extends({}, component.internalProps, component.props), function (value) {
if (typeof value === 'function') {
return function angular2Wrapped() {
var _this = this,
_arguments = arguments;
return (0, _lib.replaceObject)(_extends({}, component.internalProps, component.props), {
'function': function _function(value) {
if (typeof value === 'function') {
return function angular2Wrapped() {
var _this = this,
_arguments = arguments;
return component.zone.run(function () {
return value.apply(_this, _arguments);
});
};
return component.zone.run(function () {
return value.apply(_this, _arguments);
});
};
}
}

@@ -39,0 +41,0 @@ });

@@ -110,4 +110,4 @@ 'use strict';

function serializeFunctions(obj) {
return (0, _util.replaceObject)(obj, function (value) {
if (typeof value === 'function') {
return (0, _util.replaceObject)(obj, {
'function': function _function() {
return {

@@ -121,9 +121,11 @@ __type__: '__function__'

function deserializeFunctions(obj, handler) {
return (0, _util.replaceObject)(obj, function (value, key, fullKey) {
if (value && value.__type__ === '__function__') {
return function deserializedFunctionWrapper() {
return handler({ key: key, fullKey: fullKey, self: this, args: arguments });
};
return (0, _util.replaceObject)(obj, {
'object': function object(value, key, fullKey) {
if (value && value.__type__ === '__function__') {
return function deserializedFunctionWrapper() {
return handler({ key: key, fullKey: fullKey, self: this, args: arguments });
};
}
}
});
}

@@ -8,2 +8,3 @@ 'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/* eslint max-lines: off */

@@ -261,25 +262,113 @@ exports.urlEncode = urlEncode;

function replaceObject(obj, callback) {
var parentKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
function replaceObject(item, replacers) {
var fullKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
var newobj = Array.isArray(obj) ? [] : {};
if (Array.isArray(item)) {
var _ret = function () {
var length = item.length;
var result = [];
each(obj, function (item, key) {
var _loop = function _loop(i) {
Object.defineProperty(result, i, {
configurable: true,
enumerable: true,
get: function get() {
var itemKey = fullKey ? fullKey + '.' + i : '' + i;
var child = item[i];
var fullKey = parentKey ? parentKey + '.' + key : key;
var type = typeof child === 'undefined' ? 'undefined' : _typeof(child);
var replacer = replacers[type];
if (replacer) {
var replaced = replacer(child, i, itemKey);
if (typeof replaced !== 'undefined') {
result[i] = replaced;
return result[i];
}
}
var result = callback(item, key, fullKey);
if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' && child !== null) {
result[i] = replaceObject(child, replacers, itemKey);
return result[i];
}
if (result !== undefined) {
newobj[key] = result;
} else if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && item !== null) {
newobj[key] = replaceObject(item, callback, fullKey);
} else {
newobj[key] = item;
}
});
result[i] = child;
return result[i];
},
set: function set(value) {
delete result[i];
result[i] = value;
}
});
};
// $FlowFixMe
return newobj;
for (var i = 0; i < length; i++) {
_loop(i);
}
// $FlowFixMe
return {
v: result
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
} else if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && item !== null) {
var _ret3 = function () {
var result = {};
var _loop2 = function _loop2(key) {
if (!item.hasOwnProperty(key)) {
return 'continue';
}
Object.defineProperty(result, key, {
configurable: true,
enumerable: true,
get: function get() {
var itemKey = fullKey ? fullKey + '.' + key : '' + key;
// $FlowFixMe
var child = item[key];
var type = typeof child === 'undefined' ? 'undefined' : _typeof(child);
var replacer = replacers[type];
if (replacer) {
var replaced = replacer(child, key, itemKey);
if (typeof replaced !== 'undefined') {
result[key] = replaced;
return result[key];
}
}
if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' && child !== null) {
result[key] = replaceObject(child, replacers, itemKey);
return result[key];
}
result[key] = child;
return result[key];
},
set: function set(value) {
delete result[key];
result[key] = value;
}
});
};
for (var key in item) {
var _ret4 = _loop2(key);
if (_ret4 === 'continue') continue;
}
// $FlowFixMe
return {
v: result
};
}();
if ((typeof _ret3 === 'undefined' ? 'undefined' : _typeof(_ret3)) === "object") return _ret3.v;
} else {
throw new Error('Pass an object or array');
}
}

@@ -286,0 +375,0 @@

@@ -8,5 +8,4 @@ /* @flow */

__XCOMPONENT__: {
__POPUP_SUPPORT__: true,
__CHILD_WINDOW_ENFORCE_LOG_LEVEL__: false
__POPUP_SUPPORT__: true
}
});
{
"name": "xcomponent",
"version": "6.0.59",
"version": "6.0.60",
"description": "Cross domain components.",

@@ -5,0 +5,0 @@ "main": "index.js",

/* @flow */
/* eslint max-lines: 0 */
import { flush, logLevels } from 'beaver-logger/client';
import { flush } from 'beaver-logger/client';
import { isSameDomain, matchDomain, getDomain, type CrossDomainWindowType } from 'cross-domain-utils/src';

@@ -10,3 +10,3 @@ import { send } from 'post-robot/src';

import { BaseComponent } from '../base';
import { getParentComponentWindow, getComponentMeta, getParentDomain, getParentRenderWindow, isXComponentWindow } from '../window';
import { getParentComponentWindow, getComponentMeta, getParentDomain, getParentRenderWindow } from '../window';
import { extend, deserializeFunctions, get, onDimensionsChange, trackDimensions, dimensionsMatchViewport, stringify,

@@ -65,9 +65,20 @@ cycle, globalFor, setLogLevel, getElement, documentReady, noop, stringifyError } from '../../lib';

this.component.xchild = this;
this.setProps(this.getInitialProps(), getParentDomain());
// update logLevel with prop.logLevel to override defaultLogLevel configured when creating component
if (this.props.logLevel) {
setLogLevel(this.props.logLevel);
for (let item of [ this.component, window ]) {
for (let [ name, getter ] of [ [ 'xchild', () => this ], [ 'xprops', () => this.props ] ]) {
// $FlowFixMe
Object.defineProperty(item, name, {
configurable: true,
get: () => {
if (!this.props) {
this.setProps(this.getInitialProps(), getParentDomain());
}
// $FlowFixMe
delete item[name];
// $FlowFixMe
item[name] = getter();
// $FlowFixMe
return item[name];
}
});
}
}

@@ -109,6 +120,8 @@

listenForResize() {
this.sendToParent(POST_MESSAGE.ONRESIZE, {}, { fireAndForget: true });
window.addEventListener('resize', () => {
if (this.component.listenForResize) {
this.sendToParent(POST_MESSAGE.ONRESIZE, {}, { fireAndForget: true });
});
window.addEventListener('resize', () => {
this.sendToParent(POST_MESSAGE.ONRESIZE, {}, { fireAndForget: true });
});
}
}

@@ -166,3 +179,3 @@

props = global.props[componentMeta.uid];
props = JSON.parse(global.props[componentMeta.uid]);

@@ -176,3 +189,3 @@ } else {

}
return deserializeFunctions(props, ({ fullKey, self, args }) => {

@@ -183,3 +196,3 @@ return this.onInit.then(() => {

if (typeof func !== 'function') {
throw new TypeError(`Expected ${ typeof func } to be function`);
throw new TypeError(`Expected ${ fullKey } to be function, got ${ typeof func }`);
}

@@ -198,4 +211,5 @@

extend(this.props, normalizedProps);
window.xprops = this.props;
this.component.xprops = this.props;
if (this.props.logLevel) {
setLogLevel(this.props.logLevel);
}
for (let handler of this.onPropHandlers) {

@@ -501,44 +515,1 @@ handler.call(this, this.props);

}
if (__XCOMPONENT__.__CHILD_WINDOW_ENFORCE_LOG_LEVEL__) {
if (isXComponentWindow() && window.console) {
for (let level of logLevels) {
try {
let original = window.console[level];
if (!original || !original.apply) {
continue;
}
window.console[level] = function consoleLevel() : void {
try {
let logLevel = window.LOG_LEVEL;
if (!logLevel || logLevels.indexOf(logLevel) === -1) {
return original.apply(this, arguments);
}
if (logLevels.indexOf(level) > logLevels.indexOf(logLevel)) {
return;
}
return original.apply(this, arguments);
} catch (err2) { // eslint-disable-line unicorn/catch-error-name
// pass
}
};
if (level === 'info') {
window.console.log = window.console[level];
}
} catch (err) {
// pass
}
}
}
}

@@ -50,2 +50,3 @@ /* @flow */

autoResize? : boolean | { width? : boolean, height? : boolean, element? : string },
listenForResize? : boolean,

@@ -92,2 +93,3 @@ defaultLogLevel? : string,

autoResize : ?(boolean | { width? : boolean, height? : boolean, element? : string })
listenForResize : ?boolean

@@ -154,2 +156,3 @@ defaultLogLevel : string

this.addProp(options, 'scrolling');
this.addProp(options, 'listenForResize');

@@ -261,13 +264,8 @@ this.addProp(options, 'version', 'latest');

registerChild() {
if (isXComponentWindow()) {
ZalgoPromise.try(() => {
let componentMeta = getComponentMeta();
if (componentMeta.tag === this.tag) {
window.xchild = new ChildComponent(this);
window.xprops = window.xchild.props;
}
});
}
registerChild() : ZalgoPromise<?ChildComponent<P>> {
return ZalgoPromise.try(() => {
if (this.isChild()) {
return new ChildComponent(this);
}
});
}

@@ -433,3 +431,3 @@

isChild() : boolean {
return isXComponentWindow() && window.xprops && getComponentMeta().tag === this.tag;
return isXComponentWindow() && getComponentMeta().tag === this.tag;
}

@@ -436,0 +434,0 @@

@@ -235,3 +235,3 @@ /* @flow */

// When the component ePperiences an error
// When the component experiences an error

@@ -243,5 +243,12 @@ onError: {

sendToChild: true,
once: true
once: true,
def() : (() => void) {
return function onError(err : mixed) {
setTimeout(() => {
throw err;
});
};
}
}
};
}

@@ -385,3 +385,3 @@ /* @flow */

if (props.type === INITIAL_PROPS.UID) {
global.props[uid] = sProps;
global.props[uid] = JSON.stringify(sProps);

@@ -388,0 +388,0 @@ this.clean.register(() => {

@@ -6,3 +6,3 @@ /* @flow */

import { memoize, uniqueID, globalFor } from '../lib';
import { memoize, uniqueID, globalFor, stringifyError } from '../lib';
import { XCOMPONENT, WINDOW_REFERENCES } from '../constants';

@@ -16,3 +16,11 @@ import type { DimensionsType, PositionType } from '../types';

function encode(str : string) : string {
return base32.encode(str).replace(/\=/g, '').toLowerCase(); // eslint-disable-line no-useless-escape
}
function decode(str : string) : string {
return base32.decode(str.toUpperCase());
}
/* Build Child Window Name

@@ -37,3 +45,3 @@ -----------------------

let encodedVersion = normalize(version);
let encodedOptions = base32.encode(JSON.stringify(options)).replace(/\=/g, '').toLowerCase(); // eslint-disable-line no-useless-escape
let encodedOptions = encode(JSON.stringify(options));

@@ -93,5 +101,5 @@ if (!encodedName) {

try {
componentMeta = JSON.parse(base32.decode(encodedOptions.toUpperCase()));
componentMeta = JSON.parse(decode(encodedOptions));
} catch (err) {
throw new Error(`Can not decode component-meta`);
throw new Error(`Can not decode component-meta: ${ encodedOptions } ${ stringifyError(err) }`);
}

@@ -98,0 +106,0 @@

@@ -76,4 +76,4 @@ /* @flow */

scopeProps = replaceObject(scopeProps, (value) => {
if (typeof value === 'function') {
scopeProps = replaceObject(scopeProps, {
'function': (value) => {
return function angularWrapped() : mixed {

@@ -80,0 +80,0 @@ let result = value.apply(this, arguments);

@@ -36,7 +36,9 @@ /* @flow */

let getProps = (component) => {
return replaceObject({ ...component.internalProps, ...component.props }, (value) => {
if (typeof value === 'function') {
return function angular2Wrapped() : void {
return component.zone.run(() => value.apply(this, arguments));
};
return replaceObject({ ...component.internalProps, ...component.props }, {
'function': (value) => {
if (typeof value === 'function') {
return function angular2Wrapped() : void {
return component.zone.run(() => value.apply(this, arguments));
};
}
}

@@ -43,0 +45,0 @@ });

@@ -92,4 +92,4 @@ /* @flow */

export function serializeFunctions<T : Object | Array<mixed>>(obj : T) : T {
return replaceObject(obj, (value) => {
if (typeof value === 'function') {
return replaceObject(obj, {
'function': () => {
return {

@@ -103,9 +103,11 @@ __type__: '__function__'

export function deserializeFunctions<T : Object | Array<mixed>>(obj : T, handler : Function) : T {
return replaceObject(obj, (value, key, fullKey) => {
if (value && value.__type__ === '__function__') {
return function deserializedFunctionWrapper() : mixed {
return handler({ key, fullKey, self: this, args: arguments });
};
return replaceObject(obj, {
'object': (value, key, fullKey) => {
if (value && value.__type__ === '__function__') {
return function deserializedFunctionWrapper() : mixed {
return handler({ key, fullKey, self: this, args: arguments });
};
}
}
});
}
/* @flow */
/* eslint max-lines: off */

@@ -244,23 +245,89 @@ import { WeakMap } from 'cross-domain-safe-weakmap/src';

export function replaceObject<T : Object | Array<mixed>>(obj : T, callback : Function, parentKey : string = '') : T {
export function replaceObject<T : Object | Array<mixed>> (item : T, replacers : { [string] : Function }, fullKey : string = '') : T {
let newobj = Array.isArray(obj) ? [] : {};
if (Array.isArray(item)) {
let length = item.length;
let result = [];
each(obj, (item, key) => {
for (let i = 0; i < length; i++) {
Object.defineProperty(result, i, {
configurable: true,
enumerable: true,
get: () => {
let itemKey = fullKey ? `${ fullKey }.${ i }` : `${ i }`;
let child = item[i];
let fullKey = parentKey ? `${ parentKey }.${ key }` : key;
let type = (typeof child);
let replacer = replacers[type];
if (replacer) {
let replaced = replacer(child, i, itemKey);
if (typeof replaced !== 'undefined') {
result[i] = replaced;
return result[i];
}
}
let result = callback(item, key, fullKey);
if (typeof child === 'object' && child !== null) {
result[i] = replaceObject(child, replacers, itemKey);
return result[i];
}
if (result !== undefined) {
newobj[key] = result;
} else if (typeof item === 'object' && item !== null) {
newobj[key] = replaceObject(item, callback, fullKey);
} else {
newobj[key] = item;
result[i] = child;
return result[i];
},
set: (value) => {
delete result[i];
result[i] = value;
}
});
}
});
// $FlowFixMe
return newobj;
// $FlowFixMe
return result;
} else if (typeof item === 'object' && item !== null) {
let result = {};
for (let key in item) {
if (!item.hasOwnProperty(key)) {
continue;
}
Object.defineProperty(result, key, {
configurable: true,
enumerable: true,
get: () => {
let itemKey = fullKey ? `${ fullKey }.${ key }` : `${ key }`;
// $FlowFixMe
let child = item[key];
let type = (typeof child);
let replacer = replacers[type];
if (replacer) {
let replaced = replacer(child, key, itemKey);
if (typeof replaced !== 'undefined') {
result[key] = replaced;
return result[key];
}
}
if (typeof child === 'object' && child !== null) {
result[key] = replaceObject(child, replacers, itemKey);
return result[key];
}
result[key] = child;
return result[key];
},
set: (value) => {
delete result[key];
result[key] = value;
}
});
}
// $FlowFixMe
return result;
} else {
throw new Error(`Pass an object or array`);
}
}

@@ -267,0 +334,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc