From f604d1c0ed7132bbf008007e5ed96546444042b5 Mon Sep 17 00:00:00 2001 From: Vladymyr Shevchuk Date: Tue, 19 May 2020 21:24:27 +0300 Subject: [PATCH 1/3] Added module #2 "objects-arrays-intro-to-testing"; --- .../1-sort-strings/solution/index.js | 21 +++++++ .../2-pick/solution/index.js | 17 ++++++ .../3-omit/solution/index.js | 17 ++++++ .../1-create-getter/README.md | 22 +++++++ .../1-create-getter/index.js | 8 +++ .../1-create-getter/index.spec.js | 59 +++++++++++++++++++ .../2-invert-object/README.md | 12 ++++ .../2-invert-object/index.js | 8 +++ .../2-invert-object/index.spec.js | 25 ++++++++ .../3-trim-symbols/README.md | 18 ++++++ .../3-trim-symbols/index.js | 9 +++ .../3-trim-symbols/index.spec.js | 21 +++++++ .../4-uniq/README.md | 11 ++++ .../4-uniq/index.js | 8 +++ .../4-uniq/index.spec.js | 16 +++++ 02-objects-arrays-intro-to-testing/README.md | 30 ++++++++++ 02-objects-arrays-intro-to-testing/index.html | 5 ++ .../module-plan.md | 15 +++++ 18 files changed, 322 insertions(+) create mode 100755 01-javascript-data-types/1-sort-strings/solution/index.js create mode 100755 01-javascript-data-types/2-pick/solution/index.js create mode 100755 01-javascript-data-types/3-omit/solution/index.js create mode 100755 02-objects-arrays-intro-to-testing/1-create-getter/README.md create mode 100755 02-objects-arrays-intro-to-testing/1-create-getter/index.js create mode 100755 02-objects-arrays-intro-to-testing/1-create-getter/index.spec.js create mode 100755 02-objects-arrays-intro-to-testing/2-invert-object/README.md create mode 100755 02-objects-arrays-intro-to-testing/2-invert-object/index.js create mode 100755 02-objects-arrays-intro-to-testing/2-invert-object/index.spec.js create mode 100755 02-objects-arrays-intro-to-testing/3-trim-symbols/README.md create mode 100755 02-objects-arrays-intro-to-testing/3-trim-symbols/index.js create mode 100755 02-objects-arrays-intro-to-testing/3-trim-symbols/index.spec.js create mode 100755 02-objects-arrays-intro-to-testing/4-uniq/README.md create mode 100755 02-objects-arrays-intro-to-testing/4-uniq/index.js create mode 100755 02-objects-arrays-intro-to-testing/4-uniq/index.spec.js create mode 100755 02-objects-arrays-intro-to-testing/README.md create mode 100755 02-objects-arrays-intro-to-testing/index.html create mode 100755 02-objects-arrays-intro-to-testing/module-plan.md diff --git a/01-javascript-data-types/1-sort-strings/solution/index.js b/01-javascript-data-types/1-sort-strings/solution/index.js new file mode 100755 index 0000000..5dacfc3 --- /dev/null +++ b/01-javascript-data-types/1-sort-strings/solution/index.js @@ -0,0 +1,21 @@ +/** + * sortStrings - sorts array of string by two criteria "asc" or "desc" + * @param {string[]} arr - the array of strings + * @param {string} [param="asc"] param - the sorting type "asc" or "desc" + * @returns {string[]} + */ +export function sortStrings(arr, param = 'asc') { + switch (param) { + case 'asc': + return makeSorting(arr, 1); + case 'desc': + return makeSorting(arr, -1); + default: + return makeSorting(arr, 1); + } + + function makeSorting(array, direction) { + return [...array].sort((a, b) => + direction * a.localeCompare(b, 'default', {caseFirst: 'upper'})); + } +} diff --git a/01-javascript-data-types/2-pick/solution/index.js b/01-javascript-data-types/2-pick/solution/index.js new file mode 100755 index 0000000..b6c21c6 --- /dev/null +++ b/01-javascript-data-types/2-pick/solution/index.js @@ -0,0 +1,17 @@ +/** + * pick - Creates an object composed of the picked object properties: + * @param {object} obj - the source object + * @param {...string} fields - the properties paths to pick + * @returns {object} - returns the new object + */ +export const pick = (obj, ...fields) => { + const result = {}; + + for (let [key, value] of Object.entries(obj)) { + if (fields.includes(key)) { + result[key] = value; + } + } + + return result; +}; diff --git a/01-javascript-data-types/3-omit/solution/index.js b/01-javascript-data-types/3-omit/solution/index.js new file mode 100755 index 0000000..49e90ae --- /dev/null +++ b/01-javascript-data-types/3-omit/solution/index.js @@ -0,0 +1,17 @@ +/** + * omit - creates an object composed of enumerable property fields + * @param {object} obj - the source object + * @param {...string} fields - the properties paths to omit + * @returns {object} - returns the new object + */ +export const omit = (obj, ...fields) => { + const result = {}; + + for (let [key] of Object.entries(obj)) { + if (!fields.includes(key)) { + result[key] = obj[key]; + } + } + + return result; +}; diff --git a/02-objects-arrays-intro-to-testing/1-create-getter/README.md b/02-objects-arrays-intro-to-testing/1-create-getter/README.md new file mode 100755 index 0000000..0620d94 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/1-create-getter/README.md @@ -0,0 +1,22 @@ +# createGetter + +Необходимо реализовать функцию "createGetter". Функция должна принимать строку вида +"prop-1.prop-2.prop-n", где "prop-1, ..., prop-n" - это свойства объекта разделенные точкой. +Возвращать "createGetter" должна новую функцию, которая по заданному пути +найдет значение в переданном ей объекте и вернет его. + +```javascript +function createGetter(field) { + /* ... */ +} + +const product = { + category: { + title: "Goods" + } +} + +const getter = createGetter('category.title'); + +console.log(getter(product)); // Goods +``` diff --git a/02-objects-arrays-intro-to-testing/1-create-getter/index.js b/02-objects-arrays-intro-to-testing/1-create-getter/index.js new file mode 100755 index 0000000..c86ac40 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/1-create-getter/index.js @@ -0,0 +1,8 @@ +/** + * createGetter - creates function getter which allows select value from object + * @param {string} path - the strings path separated by dot + * @returns {function} - function-getter which allow get value from object by set path + */ +export function createGetter(path) { + +} diff --git a/02-objects-arrays-intro-to-testing/1-create-getter/index.spec.js b/02-objects-arrays-intro-to-testing/1-create-getter/index.spec.js new file mode 100755 index 0000000..39c6d83 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/1-create-getter/index.spec.js @@ -0,0 +1,59 @@ +import { createGetter } from './index.js'; + +describe('objects-arrays-intro-to-testing/create-getter', () => { + it('should return existed properties', () => { + const obj = { + category: { + title: 'Goods', + foo: undefined + } + }; + const getter = createGetter('category.title'); + + expect(getter(obj)).toEqual('Goods'); + }); + + it('should return "undefined" for properties which does not exist', () => { + const obj = { + category: { + title: 'Goods', + foo: undefined + } + }; + const getter1 = createGetter('category.foo'); + const getter2 = createGetter('category.bar'); + + expect(getter1(obj)).toBeUndefined(); + expect(getter2(obj)).toBeUndefined(); + }); + + it('gets obj.property', () => { + const getter = createGetter('property'); + + expect(getter({property: 1})).toEqual(1); + }); + + it('returns undefined for no property', () => { + const getter = createGetter('property'); + + expect(getter({})).toBeUndefined(); + }); + + it('gets obj.nested.property', () => { + const getter = createGetter('nested.property'); + + expect(getter({nested: {property: 1}})).toEqual(1); + }); + + it('returns undefined for no nested.property', () => { + const getter = createGetter('nested.property'); + + expect(getter({})).toBeUndefined(); + }); + + it('gets more.nested.property', () => { + const getter = createGetter('more.nested.property'); + + expect(getter({more: {nested: {property: 1}}})).toEqual(1); + }); +}); diff --git a/02-objects-arrays-intro-to-testing/2-invert-object/README.md b/02-objects-arrays-intro-to-testing/2-invert-object/README.md new file mode 100755 index 0000000..7c6a476 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/2-invert-object/README.md @@ -0,0 +1,12 @@ +# invertObj + +Необходимо реализовать функцию, которая меняет местами ключи и значения в объекте. +Функция принимает объект, свойства которого могут быть только примитивными значениями, +а возвращает новый объект, где ключи и свойства заменены между собой местами. + +**Пример:** +```javascript +const obj = { key: 'value' }; + +console.log(invertObj(obj)); // { value: 'key'} +``` diff --git a/02-objects-arrays-intro-to-testing/2-invert-object/index.js b/02-objects-arrays-intro-to-testing/2-invert-object/index.js new file mode 100755 index 0000000..d6f6b7c --- /dev/null +++ b/02-objects-arrays-intro-to-testing/2-invert-object/index.js @@ -0,0 +1,8 @@ +/** + * invertObj - should swap object keys and values + * @param {object} obj - the initial object + * @returns {object | undefined} - returns new object or undefined if nothing did't pass + */ +export function invertObj(obj) { + +} diff --git a/02-objects-arrays-intro-to-testing/2-invert-object/index.spec.js b/02-objects-arrays-intro-to-testing/2-invert-object/index.spec.js new file mode 100755 index 0000000..f5587d8 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/2-invert-object/index.spec.js @@ -0,0 +1,25 @@ +import { invertObj } from './index.js'; + +describe('objects-arrays-intro-to-testing/invert-object', () => { + it('should swap keys and values and return new object', () => { + const obj = { + key1: 'value1', + key2: 'value2' + }; + + const expected = { + value1: 'key1', + value2: 'key2', + }; + + expect(invertObj(obj)).toEqual(expected); + }); + + it('should return empty object if was passed object without values', () => { + expect(invertObj({})).toEqual({}); + }); + + it('should return "undefined" if object wasn\'t passed', () => { + expect(invertObj()).toBeUndefined(); + }); +}); diff --git a/02-objects-arrays-intro-to-testing/3-trim-symbols/README.md b/02-objects-arrays-intro-to-testing/3-trim-symbols/README.md new file mode 100755 index 0000000..f574e0b --- /dev/null +++ b/02-objects-arrays-intro-to-testing/3-trim-symbols/README.md @@ -0,0 +1,18 @@ +# trimSymbols + +Необходимо реализовать функцию "trimSymbols". Функция принимает 2 аргумента: + 1. строку произвольной длинны + 2. число разрешенных одинаковых символов которые расположены в строке подряд + +Функция должна вернуть строку символов, удалив из нее все последовательные одинаковые +символы которые превышают заданное число. + +**Пример:** + +```javascript +trimSymbols('xxx', 3); // 'xxx' - ничего не удалили т.к разрешено 3 символа подряд +trimSymbols('xxx', 2); // 'xx' - удалили один символ +trimSymbols('xxx', 1); // 'x' + +trimSymbols('xxxaaaaa', 2); // 'xxaa' +trimSymbols('xxxaaaaab', 3); // 'xxxaaab' diff --git a/02-objects-arrays-intro-to-testing/3-trim-symbols/index.js b/02-objects-arrays-intro-to-testing/3-trim-symbols/index.js new file mode 100755 index 0000000..bdafd72 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/3-trim-symbols/index.js @@ -0,0 +1,9 @@ +/** + * trimSymbols - removes consecutive identical symbols if they quantity bigger that size + * @param {string} string - the initial string + * @param {number} size - the allowed size of consecutive identical symbols + * @returns {string} - the new string without extra symbols according passed size + */ +export function trimSymbols(string, size) { + +} diff --git a/02-objects-arrays-intro-to-testing/3-trim-symbols/index.spec.js b/02-objects-arrays-intro-to-testing/3-trim-symbols/index.spec.js new file mode 100755 index 0000000..7cc6f16 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/3-trim-symbols/index.spec.js @@ -0,0 +1,21 @@ +import { trimSymbols } from './index.js'; + +describe('objects-arrays-intro-to-testing/trim-symbols', () => { + it('should remove an identical consecutive characters that exceed the specified size', () => { + expect(trimSymbols('eedaaad', 2)).toEqual('eedaad'); + expect(trimSymbols('xxx', 1)).toEqual('x'); + expect(trimSymbols('xxxaaaaab', 1)).toEqual('xab'); + }); + + it('should return empty string if it was passed to function like an argument', () => { + expect(trimSymbols('', 2)).toEqual(''); + }); + + it('should return empty string if "size" equal 0', () => { + expect(trimSymbols('accbbdd', 0)).toEqual(''); + }); + + it('should return the same string if "size" parameter wasn\'t specified', () => { + expect(trimSymbols('aaa')).toEqual('aaa'); + }); +}); diff --git a/02-objects-arrays-intro-to-testing/4-uniq/README.md b/02-objects-arrays-intro-to-testing/4-uniq/README.md new file mode 100755 index 0000000..ea65c44 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/4-uniq/README.md @@ -0,0 +1,11 @@ +# uniq + +Необходимо реализовать функцию "uniq". +Функция принимает один аргумент - массив примитивных значений и возвращает +новый массив уникальных значений + +**Пример:** +```javascript +uniq([1, 2, 2, 3, 1, 4]); // [1, 2, 3, 4] +uniq(['a', 'a', 'b', 'c', 'c']); // ['a', 'b', 'c'] +``` diff --git a/02-objects-arrays-intro-to-testing/4-uniq/index.js b/02-objects-arrays-intro-to-testing/4-uniq/index.js new file mode 100755 index 0000000..b81423b --- /dev/null +++ b/02-objects-arrays-intro-to-testing/4-uniq/index.js @@ -0,0 +1,8 @@ +/** + * uniq - returns array of uniq values: + * @param {*[]} arr - the array of primitive values + * @returns {*[]} - the new array with uniq values + */ +export function uniq(arr) { + +} diff --git a/02-objects-arrays-intro-to-testing/4-uniq/index.spec.js b/02-objects-arrays-intro-to-testing/4-uniq/index.spec.js new file mode 100755 index 0000000..8e7ab5c --- /dev/null +++ b/02-objects-arrays-intro-to-testing/4-uniq/index.spec.js @@ -0,0 +1,16 @@ +import { uniq } from './index.js'; + +describe('objects-arrays-intro-to-testing/uniq', () => { + it('should return array with uniq values', () => { + expect(uniq([1, 1])).toEqual([1]); + expect(uniq([1, 'a', 'a', 2, 2])).toEqual([1, 'a', 2]); + }); + + it('should return empty array if was passed array without values', () => { + expect(uniq([])).toEqual([]); + }); + + it('should return empty array if arguments wasn\'t passed', () => { + expect(uniq()).toEqual([]); + }); +}); diff --git a/02-objects-arrays-intro-to-testing/README.md b/02-objects-arrays-intro-to-testing/README.md new file mode 100755 index 0000000..399922c --- /dev/null +++ b/02-objects-arrays-intro-to-testing/README.md @@ -0,0 +1,30 @@ +# Объекты и массивы, основы тестирования + +## Материалы из учебника: + +### [Типы данных](https://learn.javascript.ru/data-types) +- [Map и Set](https://learn.javascript.ru/map-set) +- [Object.keys, values, entries](https://learn.javascript.ru/keys-values-entries) +- Задача: [Сумма свойств объекта](https://learn.javascript.ru/task/sum-salaries) +- Задача: [Подсчёт количества свойств объекта](https://learn.javascript.ru/task/count-properties) +- [Деструктурирующее присваивание](https://learn.javascript.ru/destructuring-assignment) +- Задача: [Деструктурирующее присваивание](https://learn.javascript.ru/task/destruct-user) +- Задача: [Максимальная зарплата](https://learn.javascript.ru/task/max-salary) + +### [Объекты: основы](https://learn.javascript.ru/object-basics) +- [Сборка мусора](https://learn.javascript.ru/garbage-collection) +- [Тип данных Symbol](https://learn.javascript.ru/symbol) +- [Методы объекта, "this"](https://learn.javascript.ru/object-methods) +- Задача: [Создайте калькулятор](https://learn.javascript.ru/task/calculator) +- [Преобразование объектов в примитивы](https://learn.javascript.ru/object-toprimitive) +- [Конструкторы, создание объектов через "new"](https://learn.javascript.ru/constructor-new) + +### [Основы JavaScript](https://learn.javascript.ru/first-steps) +- [Функции-стрелки, основы](https://learn.javascript.ru/arrow-functions-basics) + +### [Качество кода](https://learn.javascript.ru/code-quality) +- [Автоматическое тестирование c использованием фреймворка Mocha](https://learn.javascript.ru/testing-mocha) + +## Дополнительные материалы: + +* [Jest документация](https://jestjs.io/) diff --git a/02-objects-arrays-intro-to-testing/index.html b/02-objects-arrays-intro-to-testing/index.html new file mode 100755 index 0000000..848c4b5 --- /dev/null +++ b/02-objects-arrays-intro-to-testing/index.html @@ -0,0 +1,5 @@ + + + diff --git a/02-objects-arrays-intro-to-testing/module-plan.md b/02-objects-arrays-intro-to-testing/module-plan.md new file mode 100755 index 0000000..d93fc0d --- /dev/null +++ b/02-objects-arrays-intro-to-testing/module-plan.md @@ -0,0 +1,15 @@ +# Plan for Module #9 + +## Проверка ДЗ + +* sortStrings +* pick +* omit + +## Новая тема - Объекты и массивы, основы тестирования + +* тесты - введение +* JS модули +* объекты, массивы + +## Разбор нового ДЗ From 5df743728b6b87ac3364485eb3b84a475ecac607 Mon Sep 17 00:00:00 2001 From: Boris Catsvill Date: Wed, 20 May 2020 21:05:36 +0300 Subject: [PATCH 2/3] [Boris] Removed "test" word from readme.md --- readme.md | 1 - 1 file changed, 1 deletion(-) diff --git a/readme.md b/readme.md index 0215766..9723a91 100755 --- a/readme.md +++ b/readme.md @@ -22,4 +22,3 @@ "01-javascript-data-types" - это имя директории модуля "1-sort-strings" - имя директории задачи -test From a8e71b5696114bc4ac9235af95fd6e9af34d1787 Mon Sep 17 00:00:00 2001 From: Boris Date: Wed, 20 May 2020 23:00:01 +0300 Subject: [PATCH 3/3] Update readme.md --- readme.md | 1 - 1 file changed, 1 deletion(-) diff --git a/readme.md b/readme.md index 9723a91..635d207 100755 --- a/readme.md +++ b/readme.md @@ -21,4 +21,3 @@ "01-javascript-data-types" - это имя директории модуля "1-sort-strings" - имя директории задачи -