Objects
Data Types
Strings
export const transform = (source: Record<string, any>): Record<string, any> => { // Array of keys const keys = Object.keys(source); // Array of splitted keys const splitted_keys: string[][] = []; // Splitting keys keys.forEach((item, i) => { // If name isn't underscore only if (item !== '_') { splitted_keys.push(item.split('_')); // If name is underscore only } else { splitted_keys.push(['_']); } }); // Resulting object const result = Object.create(null); // Creating objects splitted_keys.forEach((item, index) => { // Merging objects Object.assign(result, recursiveCreateObject( // New object Object.create(null), // Array of keys item, // Final value source[keys[index]] ) ); }); return result; } /** * @describe Function, that recursively creates objects * @param obj Object, that will be nested * @param keys Keys that will be used in nesting * @param final_value Final value, that will be used, when keys array will be empty * @returns Nested object */ const recursiveCreateObject = (obj: any, keys: string[], final_value: any): any => { // If no keys in array if (keys.length === 0) { return obj[keys[0]] = final_value; } // Shift first value in array const shifted_symbol: string = keys.shift() as string; // Creating new nested object obj = {[shifted_symbol]: recursiveCreateObject(Object.create(null), keys, final_value)}; // Returning nested object return obj; }
- export const transform = (source: Record<string, any>): Record<string, any> => {
const target = Object.create(null);// TODO: handle invalid propertyObject.entries(source).forEach(([key, value]) => {key.split("_").slice(0, -1).reduce((node: Record<string, any>, element: string) => {return node[element] ??= {};}, target)[key.slice(key.lastIndexOf("_") + 1)] = value;});return target;- // Array of keys
- const keys = Object.keys(source);
- // Array of splitted keys
- const splitted_keys: string[][] = [];
- // Splitting keys
- keys.forEach((item, i) => {
- // If name isn't underscore only
- if (item !== '_') {
- splitted_keys.push(item.split('_'));
- // If name is underscore only
- } else {
- splitted_keys.push(['_']);
- }
- });
- // Resulting object
- const result = Object.create(null);
- // Creating objects
- splitted_keys.forEach((item, index) => {
- // Merging objects
- Object.assign(result,
- recursiveCreateObject(
- // New object
- Object.create(null),
- // Array of keys
- item,
- // Final value
- source[keys[index]]
- )
- );
- });
- return result;
- }
- /**
- * @describe Function, that recursively creates objects
- * @param obj Object, that will be nested
- * @param keys Keys that will be used in nesting
- * @param final_value Final value, that will be used, when keys array will be empty
- * @returns Nested object
- */
- const recursiveCreateObject = (obj: any, keys: string[], final_value: any): any => {
- // If no keys in array
- if (keys.length === 0) {
- return obj[keys[0]] = final_value;
- }
- // Shift first value in array
- const shifted_symbol: string = keys.shift() as string;
- // Creating new nested object
- obj = {[shifted_symbol]: recursiveCreateObject(Object.create(null), keys, final_value)};
- // Returning nested object
- return obj;
- }
// See https://www.chaijs.com for how to use Chai. import { assert } from "chai"; import { transform } from "./solution"; describe("object transformer function", () => { it("should handle single property", () => { const before = { a_b_c : 'value' } const after = { a : { b: { c: 'value' } } } assert.deepEqual(transform(before), after); }); it("should handle multiple property", () => { const before = { a_b_c : 'value', d:'value_d' } const after = { a : { b: { c: 'value' } }, d:'value_d' } assert.deepEqual(transform(before), after); }) it("should handle invalid property", () => { const before = { _: 'value_d' }; const after = { _:'value_d'}; assert.deepEqual(transform(before), after); }) it("should handle empty object", () => { const before = Object.create(null); const after = Object.create(null); assert.deepEqual(transform(before), after); }) });
- // See https://www.chaijs.com for how to use Chai.
- import { assert } from "chai";
- import { transform } from "./solution";
- describe("object transformer function", () => {
- it("should handle single property", () => {
- const before = { a_b_c : 'value' }
- const after = {
- a : {
- b: {
- c: 'value'
- }
- }
- }
- assert.deepEqual(transform(before), after);
- });
- it("should handle multiple property", () => {
- const before = { a_b_c : 'value', d:'value_d' }
- const after = {
- a : {
- b: {
- c: 'value'
- }
- },
- d:'value_d'
- }
- assert.deepEqual(transform(before), after);
- })
- it("should handle invalid property", () => {
const before = { _: 'value' };- const before = { _: 'value_d' };
- const after = { _:'value_d'};
- assert.deepEqual(transform(before), after);
- })
- it("should handle empty object", () => {
- const before = Object.create(null);
- const after = Object.create(null);
- assert.deepEqual(transform(before), after);
- })
- });