New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@corex/flatten

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@corex/flatten - npm Package Compare versions

Comparing version
4.0.19
to
4.0.22
+55
dist/@types/__fixtures__/data.d.ts
export declare const sampleInput: {
1: {
a: string;
2: {
b: string;
};
};
here: {
is: string;
object: string;
stuff: string;
};
empty: {};
emptyNested: {
hello: null;
};
emptyArr: never[];
emptyArrNested: {
hello: never[];
world: null[];
};
containing: {
many: (string | {
nest: {
hello: string;
arr?: undefined;
};
} | {
nest: {
arr: number[];
hello?: undefined;
};
} | null | undefined)[];
};
};
export declare const sampleResult: {
'1.a': string;
'1.2.b': string;
'here.is': string;
'here.object': string;
'here.stuff': string;
'containing.many[0]': string;
'containing.many[1]': string;
'containing.many[2].nest.hello': string;
'containing.many[3]': undefined;
'containing.many[4]': null;
'containing.many[5].nest.arr[0]': number;
'containing.many[5].nest.arr[1]': number;
'containing.many[5].nest.arr[2]': number;
empty: {};
emptyArr: never[];
'emptyArrNested.hello': never[];
'emptyArrNested.world[0]': null;
'emptyNested.hello': null;
};
export declare const flatten: (obj: unknown) => {};
export * from './flatten.js';
export * from './unflatten.js';
export declare const unflatten: (data: any) => any;
"use strict";
exports.__esModule = true;
exports.sampleResult = exports.sampleInput = void 0;
exports.sampleInput = {
1: {
a: 'aaa',
2: {
b: 'bbb'
}
},
here: {
is: 'my',
object: 'with',
stuff: 'in it'
},
empty: {},
emptyNested: {
hello: null
},
emptyArr: [],
emptyArrNested: {
hello: [],
world: [null]
},
containing: {
many: [
'things',
'inside',
{
nest: {
hello: 'world'
}
},
undefined,
null,
{
nest: {
arr: [1, 2, 4]
}
},
]
}
};
exports.sampleResult = {
'1.a': 'aaa',
'1.2.b': 'bbb',
'here.is': 'my',
'here.object': 'with',
'here.stuff': 'in it',
'containing.many[0]': 'things',
'containing.many[1]': 'inside',
'containing.many[2].nest.hello': 'world',
'containing.many[3]': undefined,
'containing.many[4]': null,
'containing.many[5].nest.arr[0]': 1,
'containing.many[5].nest.arr[1]': 2,
'containing.many[5].nest.arr[2]': 4,
empty: {},
emptyArr: [],
'emptyArrNested.hello': [],
'emptyArrNested.world[0]': null,
'emptyNested.hello': null
};
"use strict";
exports.__esModule = true;
var flatten_js_1 = require("../flatten.js");
var data_js_1 = require("../__fixtures__/data.js");
describe('@corex/flatten', function () {
test('flatten', function () {
expect((0, flatten_js_1.flatten)(data_js_1.sampleInput)).toStrictEqual(data_js_1.sampleResult);
});
});
"use strict";
exports.__esModule = true;
var unflatten_js_1 = require("../unflatten.js");
var data_js_1 = require("../__fixtures__/data.js");
describe('@corex/unflatten', function () {
test('unflatten', function () {
expect((0, unflatten_js_1.unflatten)(data_js_1.sampleInput)).toStrictEqual(data_js_1.sampleResult);
});
});
"use strict";
exports.__esModule = true;
exports.flatten = void 0;
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
var flatten = function (obj) {
var result = {};
var flat = function (curr, propKey) {
if (propKey === void 0) { propKey = ''; }
// If current value is an not an object (number, string etc) => Add value to result with propKey
if (Object(curr) !== curr) {
return (result[propKey] = curr);
}
// If current value is array
if (Array.isArray(curr)) {
// Empty array => Add empty arr to result[propKey]
if (curr.length == 0) {
return (result[propKey] = []);
}
// Iterate over all items
return curr === null || curr === void 0 ? void 0 : curr.map(function (_, index) {
flat(curr[index], "".concat(propKey, "[").concat(index, "]"));
});
}
// Handle objects
// Extract object keys
var objKeys = Object.keys(curr);
// If object keys length is 0 => Empty object
if (propKey && (objKeys === null || objKeys === void 0 ? void 0 : objKeys.length) === 0) {
return (result[propKey] = {});
}
// Iterate over all object keys and build index
return objKeys === null || objKeys === void 0 ? void 0 : objKeys.forEach(function (key) {
flat(curr[key], propKey ? "".concat(propKey, ".").concat(key) : key);
});
};
// Run recursive
flat(obj, '');
return result;
};
exports.flatten = flatten;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
exports.__esModule = true;
__exportStar(require("./flatten.js"), exports);
__exportStar(require("./unflatten.js"), exports);
"use strict";
exports.__esModule = true;
exports.unflatten = void 0;
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
var unflatten = function (data) {
if (Object(data) !== data || Array.isArray(data)) {
return data;
}
var resultholder = {};
var regex = /\.?([^.[\]]+)|\[(\d+)\]/g;
for (var p in data) {
var cur = resultholder;
var prop = '';
var m = void 0;
while ((m = regex.exec(p))) {
// console.log('m[1]', m[1])
// console.log('m[2]', m[2])
// cur = cur[prop] || (cur[prop] = ((m[2] || !isNaN(m[1])) ? [] : {}))
cur = cur[prop] || (cur[prop] = m[2] ? [] : {});
prop = m[2] || m[1];
// remove placeholder
prop = prop.replace(/XPERIOD/g, '.');
}
cur[prop] = data[p];
}
return resultholder[''] || resultholder;
};
exports.unflatten = unflatten;
export const sampleInput = {
1: {
a: 'aaa',
2: {
b: 'bbb',
},
},
here: {
is: 'my',
object: 'with',
stuff: 'in it',
},
empty: {},
emptyNested: {
hello: null,
},
emptyArr: [],
emptyArrNested: {
hello: [],
world: [null],
},
containing: {
many: [
'things',
'inside',
{
nest: {
hello: 'world',
},
},
undefined,
null,
{
nest: {
arr: [1, 2, 4],
},
},
],
},
};
export const sampleResult = {
'1.a': 'aaa',
'1.2.b': 'bbb',
'here.is': 'my',
'here.object': 'with',
'here.stuff': 'in it',
'containing.many[0]': 'things',
'containing.many[1]': 'inside',
'containing.many[2].nest.hello': 'world',
'containing.many[3]': undefined,
'containing.many[4]': null,
'containing.many[5].nest.arr[0]': 1,
'containing.many[5].nest.arr[1]': 2,
'containing.many[5].nest.arr[2]': 4,
empty: {},
emptyArr: [],
'emptyArrNested.hello': [],
'emptyArrNested.world[0]': null,
'emptyNested.hello': null,
};
import { flatten } from '../flatten.js';
import { sampleInput, sampleResult } from '../__fixtures__/data.js';
describe('@corex/flatten', () => {
test('flatten', () => {
expect(flatten(sampleInput)).toStrictEqual(sampleResult);
});
});
import { unflatten } from '../unflatten.js';
import { sampleInput, sampleResult } from '../__fixtures__/data.js';
describe('@corex/unflatten', () => {
test('unflatten', () => {
expect(unflatten(sampleInput)).toStrictEqual(sampleResult);
});
});
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
export const flatten = (obj) => {
const result = {};
const flat = (curr, propKey = '') => {
// If current value is an not an object (number, string etc) => Add value to result with propKey
if (Object(curr) !== curr) {
return (result[propKey] = curr);
}
// If current value is array
if (Array.isArray(curr)) {
// Empty array => Add empty arr to result[propKey]
if (curr.length == 0) {
return (result[propKey] = []);
}
// Iterate over all items
return curr?.map((_, index) => {
flat(curr[index], `${propKey}[${index}]`);
});
}
// Handle objects
// Extract object keys
const objKeys = Object.keys(curr);
// If object keys length is 0 => Empty object
if (propKey && objKeys?.length === 0) {
return (result[propKey] = {});
}
// Iterate over all object keys and build index
return objKeys?.forEach((key) => {
flat(curr[key], propKey ? `${propKey}.${key}` : key);
});
};
// Run recursive
flat(obj, '');
return result;
};
export * from './flatten.js';
export * from './unflatten.js';
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
export const unflatten = (data) => {
if (Object(data) !== data || Array.isArray(data)) {
return data;
}
const resultholder = {};
const regex = /\.?([^.[\]]+)|\[(\d+)\]/g;
for (const p in data) {
let cur = resultholder;
let prop = '';
let m;
while ((m = regex.exec(p))) {
// console.log('m[1]', m[1])
// console.log('m[2]', m[2])
// cur = cur[prop] || (cur[prop] = ((m[2] || !isNaN(m[1])) ? [] : {}))
cur = cur[prop] || (cur[prop] = m[2] ? [] : {});
prop = m[2] || m[1];
// remove placeholder
prop = prop.replace(/XPERIOD/g, '.');
}
cur[prop] = data[p];
}
return resultholder[''] || resultholder;
};
+4
-1
{
"name": "@corex/flatten",
"version": "4.0.19",
"version": "4.0.22",
"sideEffects": false,

@@ -15,2 +15,5 @@ "license": "MIT",

},
"files": [
"dist"
],
"description": "A zero dependency object merger with typescript support and built in common merge utilities.",

@@ -17,0 +20,0 @@ "keywords": [