Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

03-objects-arrays-intro-to-testing #5

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion 03-objects-arrays-intro-to-testing/1-create-getter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@
* @returns {function} - function-getter which allow get value from object by set path
*/
export function createGetter(path) {

const fields = path.split('.');
return function (product) {
return fields.reduce((acc, key) => {
if (acc && product.hasOwnProperty.call(acc, key)) {
return acc[key];
}
return undefined;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь и ниже, нет необходимости в return undefined; пишите return;

}, product);
};
}
9 changes: 8 additions & 1 deletion 03-objects-arrays-intro-to-testing/2-invert-object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@
* @returns {object | undefined} - returns new object or undefined if nothing did't pass
*/
export function invertObj(obj) {

if (!obj) {
return undefined;
}
const newObj = {};
Object.entries(obj).reduce((acc, [key, value]) => {
return newObj[value] = key;
}, {});
return newObj;
}
17 changes: 17 additions & 0 deletions 03-objects-arrays-intro-to-testing/3-trim-symbols/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,22 @@
* @returns {string} - the new string without extra symbols according passed size
*/
export function trimSymbols(string, size) {
if (size === undefined) {return string;}
if (size === 0) {return '';}
let result = '';
let count = 1;

for (let i = 0; i < string.length; i++) {
if (string[i] === string[i - 1]) {
count++;
} else {
count = 1;
}

if (count <= size) {
result += string[i];
}
}

return result;
}
2 changes: 1 addition & 1 deletion 03-objects-arrays-intro-to-testing/4-uniq/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* @returns {*[]} - the new array with uniq values
*/
export function uniq(arr) {

return Array.from(new Set(arr));
}
Loading