Objects
Data Types
Strings
Write a function that processes the properties of a given object, by the following rules:
- Each property that has an underscore (
_
) in its name will be converted to a nested object - Properties that have only underscores or no properties, will remain the same
- The last nested property will hold the original value
Example:
const beforeTransformation = {
a_b_c: "value"
}
const afterTransformation = {
a: {
b: {
c: "value"
}
}
}
export const transform = (source: Record<string, any>): Record<string, any> => { const target = Object.create(null); // TODO: handle invalid property Object.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; }
export function transform(source: any) {const target = {};Object.entries(source).forEach(function ([k, v]) {k.split("_").slice(0, -1).reduce(function (node: {[key: string]: any}, element: string) {- export const transform = (source: Record<string, any>): Record<string, any> => {
- const target = Object.create(null);
- // TODO: handle invalid property
- Object.entries(source).forEach(([key, value]) => {
- key.split("_").slice(0, -1).reduce((node: Record<string, any>, element: string) => {
- return node[element] ??= {};
}, target)[k.slice(k.lastIndexOf("_") + 1)] = v;- }, target)[key.slice(key.lastIndexOf("_") + 1)] = value;
- });
- return target;
- }
// 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 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";
// TODO Add your tests heredescribe("example", function() {const before = { a_b_c : 'value' }const before_2 = { a_b_c : 'value', d:'value_d' }const after = {a : {b: {c: 'value'}}}const after_2 = {a : {b: {c: 'value'- describe("object transformer function", () => {
- it("should handle single property", () => {
- const before = { a_b_c : 'value' }
- const after = {
- a : {
- b: {
- c: 'value'
- }
- }
- }
},d:'value_d'}it("test", function() {- assert.deepEqual(transform(before), after);
assert.deepEqual(transform(before_2), after_2);- });
- 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 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);
- })
- });
Holy JS :)
const chai = require("chai"); const assert = chai.assert; describe('Fixed Tests',()=>{ it('',()=>assert.strictEqual(larger_than_5(10),true)); it('',()=>assert.strictEqual(larger_than_5(29),true)); it('',()=>assert.strictEqual(larger_than_5(53),true)); it('',()=>assert.strictEqual(larger_than_5(34),true)); it('',()=>assert.strictEqual(larger_than_5(4),false)); it('',()=>assert.strictEqual(larger_than_5(3),false)); it('',()=>assert.strictEqual(larger_than_5(2),false)); it('',()=>assert.strictEqual(larger_than_5(1),false)); it('',()=>assert.strictEqual(larger_than_5(0),false)); }); describe('Random Tests',()=>{ for(let i = 0;i<= 1000; i++){ let r = Math.floor(Math.random()*(-50)) it('',()=>assert.strictEqual(larger_than_5(r),r>5)); } });
Test.describe('Fixed Tests')test.assert_equals(larger_than_5(10), True)test.assert_equals(larger_than_5(39), True)test.assert_equals(larger_than_5(29), True)test.assert_equals(larger_than_5(53), True)test.assert_equals(larger_than_5(34), True)test.assert_equals(larger_than_5(4), False)test.assert_equals(larger_than_5(3), False)test.assert_equals(larger_than_5(2), False)test.assert_equals(larger_than_5(1), False)test.assert_equals(larger_than_5(0), False)- const chai = require("chai");
- const assert = chai.assert;
from random import randintTest.describe('Random Tests')for i in range(1000):r = randint(-45,55)test.assert_equals(larger_than_5(r), r > 5)- describe('Fixed Tests',()=>{
- it('',()=>assert.strictEqual(larger_than_5(10),true));
- it('',()=>assert.strictEqual(larger_than_5(29),true));
- it('',()=>assert.strictEqual(larger_than_5(53),true));
- it('',()=>assert.strictEqual(larger_than_5(34),true));
- it('',()=>assert.strictEqual(larger_than_5(4),false));
- it('',()=>assert.strictEqual(larger_than_5(3),false));
- it('',()=>assert.strictEqual(larger_than_5(2),false));
- it('',()=>assert.strictEqual(larger_than_5(1),false));
- it('',()=>assert.strictEqual(larger_than_5(0),false));
- });
- describe('Random Tests',()=>{
- for(let i = 0;i<= 1000; i++){
- let r = Math.floor(Math.random()*(-50))
- it('',()=>assert.strictEqual(larger_than_5(r),r>5));
- }
- });