internship's Issues
grouping todos by users func
/*
- // 1. grouping todos by users:
- https://github.com/gagan-bansal/json-groupby - lib implementing grouping functionality
- book : Бэнкс Алекс, Порселло Ева React и Redux: функциональная веб-разработка «O’Reilly», chapter 3
- https://24ways.org/2019/five-interesting-ways-to-use-array-reduce/
*/
var selectGrpUserId = function (todos_by_userid) {
return todos_by_userid.reduce( function (res, cur) {
const { userId, id, title, completed } = cur;
// initialaze if undefined:
if (!res[userId])
res[userId] = [];
res[userId].push({id, title, completed, userId}); // push all + UserId to check for correctness
//res[userId].push(id); // or push only id
return res;
}, {})
};
const url = "https://jsonplaceholder.typicode.com/todos";
// some experiments on my own with chaining (thenable):
const selectTodosByUserId = async (id) =>
await fetch(url)
.then((response) => response.json())
.then(selectGrpUserId)
.then(function (res) { return res[id] });
selectTodosByUserId(2).then((res) => {
console.log(res);
});
(possible) solution for wrapper func for any selector (promise in fact) func with memoize
/*
- Optional: 3. Write a helper function which
-
- acts like a wrapper
-
- memoizes the result of any selector function:
- resources used:
- https://stackoverflow.com/questions/48449748/how-to-write-a-javascript-promise-decorator
- "JavaScript Patterns" by Stoyan Stefanov (c) O’Reilly - for memorization func
*/
const Wrapper_With_Memoization_and_Timing = (promiseFn, ...args) => {
const start = Date.now()
return promiseFn(...args).then(result => {
// console.log(Promise in \"%s(%d)\" finished in ${(Date.now() - start) / 1000} seconds
, promiseFn.name, ...args);
// deal with cache key:
// minor modification: using memoiz array with unique indices composed of promiseFn.name and its args:
var cachekey = [promiseFn.name, ...args];
console.log("cache key: ", cachekey);
// deal with cache value:
if (!Wrapper_With_Memoization_and_Timing.cache[cachekey]) {
console.log("NOT FOUND in cache, memorizing the result.");
Wrapper_With_Memoization_and_Timing.cache[cachekey] = result;
}
else {
console.log("FOUND in cache, return memorized result.");
}
//console.log(Wrapper_With_Memoization_and_Timing.cache);
return Wrapper_With_Memoization_and_Timing.cache[cachekey];
}, error => {
console.log(Promise errored in ${(Date.now() - start) / 1000} seconds
)
return Promise.reject(error)
})
};
Wrapper_With_Memoization_and_Timing.cache = {};
setTimeout(() =>
{
console.log("Hello world with memo!", Wrapper_With_Memoization_and_Timing.cache["selectById,2"]);
//for (let i in Wrapper_With_Memoization_and_Timing.cache)
// console.log("Hello world in sync!", Wrapper_With_Memoization_and_Timing.cache[i], i, typeof i);
}
, 5000);
// some experiments on my own...
var mf = function ( ) {
// run some tests:
Wrapper_With_Memoization_and_Timing(selectById, 1);
Wrapper_With_Memoization_and_Timing(selectById, 1);
Wrapper_With_Memoization_and_Timing(selectById, 2);
Wrapper_With_Memoization_and_Timing(selectById, 2);
Wrapper_With_Memoization_and_Timing(selectAll);
Wrapper_With_Memoization_and_Timing(selectAll);
}( );
//console.log("this is a last line of the script.");
/* sync call not implemented yet (MK)
- still yet to write it properly...
for (let i in Wrapper_With_Memoization_and_Timing.cache)
console.log("Hello world in sync!", Wrapper_With_Memoization_and_Timing.cache[i]);
*/
// console.log("Hello world in sync!", Wrapper_With_Memoization_and_Timing.cache["selectById, 1"]);
overall solution for task #2 frontend internship test assignment
An overall solution for task №2 test assignment here is a link to typescript pg. it comes with types, class and memoization
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.