Giter Site home page Giter Site logo

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

/*

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"]);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.