Ad
Code
Diff
  • const revstr = str => str.split('').reverse().join('')
    • function revstr(str) {
    • i = str.length; newstr = "";
    • while (i > 0) {
    • newstr += str[i - 1]; i--;
    • }
    • return newstr;
    • }
    • const revstr = str => str.split('').reverse().join('')
Closures
Basic Language Features
Fundamentals
ES2015
Babel
Functions
Control Flow

Added some null checks

Code
Diff
  • 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
Objects
Data Types
Strings
Code
Diff
  • 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;
    • }