Closures
Basic Language Features
Fundamentals
ES2015
Babel
Functions
Control Flow
Added some null checks
const unknown = 'Unknown error' const catalog = { en: { ERROR_USER_NOT_FOUND: 'Error: User not found', ERROR_500: 'Internal Server Error', }, ru: { ERROR_USER_NOT_FOUND: 'Ошибка: Пользователь не найден', } } const catalogProvider = locale => error => catalog[locale] && catalog[locale][error] || unknown
- const unknown = 'Unknown error'
- const catalog = {
- en: {
- ERROR_USER_NOT_FOUND: 'Error: User not found',
- ERROR_500: 'Internal Server Error',
- },
- ru: {
- ERROR_USER_NOT_FOUND: 'Ошибка: Пользователь не найден',
- }
- }
const catalogProvider = locale => error => catalog[locale][error] || unknown- const catalogProvider = locale => error => catalog[locale] && catalog[locale][error] || unknown
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; const Test = require("@codewars/test-compat"); // Uncomment the following line to disable truncating failure messages for deep equals, do: // chai.config.truncateThreshold = 0; // Since Node 12, we no longer include assertions from our deprecated custom test framework by default. // Uncomment the following to use the old assertions: describe("Solution with EN locale", function() { const cp = catalogProvider('en'); it("should test for something", function() { Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Error: User not found'); Test.assertEquals( cp('ERROR_500'), 'Internal Server Error'); // Test.assertEquals(1 + 1, 2); // assert.strictEqual(1 + 1, 2); }); }); describe("Solution with RU locale", function() { const cp = catalogProvider('ru'); it("should test for something", function() { Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Ошибка: Пользователь не найден'); Test.assertEquals( cp('ERROR_500'), 'Unknown error'); }); }); describe("Solution with EO locale", function() { const cp = catalogProvider('eo'); it("should test for something", function() { Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Unknown error'); Test.assertEquals( cp('ERROR_500'), 'Unknown error'); }); });
- // Since Node 10, we're using Mocha.
- // You can use `chai` for assertions.
- const chai = require("chai");
- const assert = chai.assert;
- const Test = require("@codewars/test-compat");
- // Uncomment the following line to disable truncating failure messages for deep equals, do:
- // chai.config.truncateThreshold = 0;
- // Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
- // Uncomment the following to use the old assertions:
- describe("Solution with EN locale", function() {
- const cp = catalogProvider('en');
- it("should test for something", function() {
- Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Error: User not found');
- Test.assertEquals( cp('ERROR_500'), 'Internal Server Error');
- // Test.assertEquals(1 + 1, 2);
- // assert.strictEqual(1 + 1, 2);
- });
- });
- describe("Solution with RU locale", function() {
- const cp = catalogProvider('ru');
- it("should test for something", function() {
- Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Ошибка: Пользователь не найден');
- Test.assertEquals( cp('ERROR_500'), 'Unknown error');
// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);- });
- });
- describe("Solution with EO locale", function() {
- const cp = catalogProvider('eo');
- it("should test for something", function() {
- Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Unknown error');
- Test.assertEquals( cp('ERROR_500'), 'Unknown error');
- });
- });
Objects
Data Types
Strings
transform = (source) => { const target = {}; Object.entries(source).forEach(([key, value]) => { key.split("_").slice(0, -1).reduce((node, element) => { return node[element] = {}; }, target)[key.slice(key.lastIndexOf("_") + 1)] = value; }); return target; }
export const transform = (source: Record<string, any>): Record<string, any> => {const target = Object.create(null);// TODO: handle invalid property- transform = (source) => {
- const target = {};
- Object.entries(source).forEach(([key, value]) => {
key.split("_").slice(0, -1).reduce((node: Record<string, any>, element: string) => {return node[element] ??= {};- key.split("_").slice(0, -1).reduce((node, element) => {
- return node[element] = {};
- }, target)[key.slice(key.lastIndexOf("_") + 1)] = value;
- });
- return target;
- }
const chai = require('chai'); const {assert} = chai; 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";- const chai = require('chai');
- const {assert} = chai;
- 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 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);
- })
- });