Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

map2tree

Package Overview
Dependencies
Maintainers
4
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map2tree - npm Package Compare versions

Comparing version 2.1.0 to 3.0.0

80

lib/cjs/index.js
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,20 +8,12 @@ value: true

exports.map2tree = map2tree;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _isArray = _interopRequireDefault(require("lodash/isArray"));
var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
var _mapValues = _interopRequireDefault(require("lodash/mapValues"));
function visit(parent, visitFn, childrenFn) {
if (!parent) return;
visitFn(parent);
var children = childrenFn(parent);
const children = childrenFn(parent);
if (children) {
var count = children.length;
for (var i = 0; i < count; i++) {
const count = children.length;
for (let i = 0; i < count; i++) {
visit(children[i], visitFn, childrenFn);

@@ -32,23 +23,17 @@ }

}
function getNode(tree, key) {
var node = null;
visit(tree, function (d) {
let node = null;
visit(tree, d => {
if (d.name === key) {
node = d;
}
}, function (d) {
return d.children;
});
}, d => d.children);
return node;
}
function map2tree( // eslint-disable-next-line @typescript-eslint/ban-types
root) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var tree = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
function map2tree(root) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let tree = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
name: options.key || 'state',
children: []
};
// eslint-disable-next-line @typescript-eslint/ban-types

@@ -58,30 +43,30 @@ if (!(0, _isPlainObject.default)(root) && root && !root.toJS) {

}
var _options$key = options.key,
rootNodeKey = _options$key === void 0 ? 'state' : _options$key,
_options$pushMethod = options.pushMethod,
pushMethod = _options$pushMethod === void 0 ? 'push' : _options$pushMethod;
var currentNode = getNode(tree, rootNodeKey);
const {
key: rootNodeKey = 'state',
pushMethod = 'push'
} = options;
const currentNode = getNode(tree, rootNodeKey);
if (currentNode === null) {
return {};
}
(0, _mapValues.default)( // eslint-disable-next-line @typescript-eslint/ban-types
root && root.toJS ? // eslint-disable-next-line @typescript-eslint/ban-types
root.toJS() : // eslint-disable-next-line @typescript-eslint/ban-types
root, // eslint-disable-next-line @typescript-eslint/ban-types
function (maybeImmutable, key) {
var value = maybeImmutable && maybeImmutable.toJS ? maybeImmutable.toJS() : maybeImmutable;
var newNode = {
(0, _mapValues.default)(
// eslint-disable-next-line @typescript-eslint/ban-types
root && root.toJS ?
// eslint-disable-next-line @typescript-eslint/ban-types
root.toJS() :
// eslint-disable-next-line @typescript-eslint/ban-types
root,
// eslint-disable-next-line @typescript-eslint/ban-types
(maybeImmutable, key) => {
const value = maybeImmutable && maybeImmutable.toJS ? maybeImmutable.toJS() : maybeImmutable;
const newNode = {
name: key
};
if ((0, _isArray.default)(value)) {
newNode.children = [];
for (var i = 0; i < value.length; i++) {
newNode.children[pushMethod]((0, _defineProperty2.default)({
name: "".concat(key, "[").concat(i, "]")
}, (0, _isPlainObject.default)(value[i]) ? 'object' : 'value', value[i]));
for (let i = 0; i < value.length; i++) {
newNode.children[pushMethod]({
name: `${key}[${i}]`,
[(0, _isPlainObject.default)(value[i]) ? 'object' : 'value']: value[i]
});
}

@@ -93,7 +78,6 @@ } else if ((0, _isPlainObject.default)(value)) {

}
currentNode.children[pushMethod](newNode);
map2tree(value, {
key: key,
pushMethod: pushMethod
key,
pushMethod
}, tree);

@@ -100,0 +84,0 @@ });

75

lib/esm/index.js

@@ -1,15 +0,11 @@

import _defineProperty from "@babel/runtime/helpers/defineProperty";
import isArray from 'lodash/isArray';
import isPlainObject from 'lodash/isPlainObject';
import mapValues from 'lodash/mapValues';
function visit(parent, visitFn, childrenFn) {
if (!parent) return;
visitFn(parent);
var children = childrenFn(parent);
const children = childrenFn(parent);
if (children) {
var count = children.length;
for (var i = 0; i < count; i++) {
const count = children.length;
for (let i = 0; i < count; i++) {
visit(children[i], visitFn, childrenFn);

@@ -19,23 +15,17 @@ }

}
function getNode(tree, key) {
var node = null;
visit(tree, function (d) {
let node = null;
visit(tree, d => {
if (d.name === key) {
node = d;
}
}, function (d) {
return d.children;
});
}, d => d.children);
return node;
}
export function map2tree( // eslint-disable-next-line @typescript-eslint/ban-types
root) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var tree = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
export function map2tree(root) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let tree = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
name: options.key || 'state',
children: []
};
// eslint-disable-next-line @typescript-eslint/ban-types

@@ -45,30 +35,30 @@ if (!isPlainObject(root) && root && !root.toJS) {

}
var _options$key = options.key,
rootNodeKey = _options$key === void 0 ? 'state' : _options$key,
_options$pushMethod = options.pushMethod,
pushMethod = _options$pushMethod === void 0 ? 'push' : _options$pushMethod;
var currentNode = getNode(tree, rootNodeKey);
const {
key: rootNodeKey = 'state',
pushMethod = 'push'
} = options;
const currentNode = getNode(tree, rootNodeKey);
if (currentNode === null) {
return {};
}
mapValues( // eslint-disable-next-line @typescript-eslint/ban-types
root && root.toJS ? // eslint-disable-next-line @typescript-eslint/ban-types
root.toJS() : // eslint-disable-next-line @typescript-eslint/ban-types
root, // eslint-disable-next-line @typescript-eslint/ban-types
function (maybeImmutable, key) {
var value = maybeImmutable && maybeImmutable.toJS ? maybeImmutable.toJS() : maybeImmutable;
var newNode = {
mapValues(
// eslint-disable-next-line @typescript-eslint/ban-types
root && root.toJS ?
// eslint-disable-next-line @typescript-eslint/ban-types
root.toJS() :
// eslint-disable-next-line @typescript-eslint/ban-types
root,
// eslint-disable-next-line @typescript-eslint/ban-types
(maybeImmutable, key) => {
const value = maybeImmutable && maybeImmutable.toJS ? maybeImmutable.toJS() : maybeImmutable;
const newNode = {
name: key
};
if (isArray(value)) {
newNode.children = [];
for (var i = 0; i < value.length; i++) {
newNode.children[pushMethod](_defineProperty({
name: "".concat(key, "[").concat(i, "]")
}, isPlainObject(value[i]) ? 'object' : 'value', value[i]));
for (let i = 0; i < value.length; i++) {
newNode.children[pushMethod]({
name: `${key}[${i}]`,
[isPlainObject(value[i]) ? 'object' : 'value']: value[i]
});
}

@@ -80,7 +70,6 @@ } else if (isPlainObject(value)) {

}
currentNode.children[pushMethod](newNode);
map2tree(value, {
key: key,
pushMethod: pushMethod
key,
pushMethod
}, tree);

@@ -87,0 +76,0 @@ });

export interface Node {
name: string;
children?: Node[] | null;
children?: this[];
object?: unknown;
value?: unknown;

@@ -5,0 +6,0 @@ }

{
"name": "map2tree",
"version": "2.1.0",
"version": "3.0.0",
"description": "Utility for mapping maps to trees",

@@ -25,3 +25,2 @@ "keywords": [

"types": "lib/types/index.d.ts",
"unpkg": "lib/umd/map2tree.umd.js",
"sideEffects": false,

@@ -32,8 +31,30 @@ "repository": {

},
"dependencies": {
"@babel/runtime": "^7.20.6",
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/cli": "^7.19.3",
"@babel/core": "^7.20.5",
"@babel/eslint-parser": "^7.19.1",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^29.2.4",
"@types/lodash": "^4.14.191",
"@typescript-eslint/eslint-plugin": "^5.47.0",
"@typescript-eslint/parser": "^5.47.0",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.1.7",
"immutable": "^4.1.0",
"jest": "^29.3.1",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
"typescript": "~4.9.4"
},
"scripts": {
"build": "yarn build:cjs && yarn build:esm && yarn build:types && yarn build:umd",
"build": "pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
"build:cjs": "babel src --extensions \".ts\" --out-dir lib/cjs",
"build:esm": "babel src --config-file ./babel.config.esm.json --extensions \".ts\" --out-dir lib/esm",
"build:types": "tsc --emitDeclarationOnly",
"build:umd": "rollup -c",
"clean": "rimraf lib",

@@ -43,37 +64,4 @@ "test": "jest",

"type-check": "tsc --noEmit",
"prepack": "yarn run clean && yarn run build",
"prepublish": "yarn run type-check && yarn run lint && yarn run test"
},
"dependencies": {
"@babel/runtime": "^7.16.7",
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/cli": "^7.16.8",
"@babel/core": "^7.16.12",
"@babel/eslint-parser": "^7.16.5",
"@babel/plugin-transform-runtime": "^7.16.10",
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.3",
"@types/jest": "^27.4.0",
"@types/lodash": "^4.14.178",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"eslint": "^8.7.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-jest": "^25.7.0",
"immutable": "^4.0.0",
"jest": "^27.4.7",
"rimraf": "^3.0.2",
"rollup": "^2.66.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.31.1",
"ts-jest": "^27.1.3",
"tslib": "^2.3.1",
"typescript": "~4.5.5"
},
"gitHead": "238b8896f9cdb8c15c265025d131830a7f3570b9"
}
"prepublish": "pnpm run type-check && pnpm run lint && pnpm run test"
}
}

@@ -7,3 +7,4 @@ import isArray from 'lodash/isArray';

name: string;
children?: Node[] | null;
children?: this[];
object?: unknown;
value?: unknown;

@@ -47,3 +48,2 @@ }

export function map2tree(
// eslint-disable-next-line @typescript-eslint/ban-types
root: unknown,

@@ -50,0 +50,0 @@ options: { key?: string; pushMethod?: 'push' | 'unshift' } = {},

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