Skip to content

Commit

Permalink
Merge pull request #5 from TimofeevAnton1980/master
Browse files Browse the repository at this point in the history
03-objects-arrays-intro-to-testing
  • Loading branch information
jsru-1 authored Nov 10, 2024
2 parents bc1085a + df303b3 commit c9f3a6a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
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;
}, 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));
}

0 comments on commit c9f3a6a

Please sign in to comment.