Skip to content

Instantly share code, notes, and snippets.

@claudioacioli
Created May 18, 2021 22:18
Show Gist options
  • Save claudioacioli/bf81cc749e1566df5db341d5b7417308 to your computer and use it in GitHub Desktop.
Save claudioacioli/bf81cc749e1566df5db341d5b7417308 to your computer and use it in GitHub Desktop.
const Client = function (data) {
let copy = Object.keys(data);
const
reset = function () {
copy = Object.keys(data);
},
nextIndex = function () {
Math.floor(Math.random() * copy.length);
},
removeIndex = function (index) {
copy.splice(index, 1);
if (!copy.length) {
reset();
}
},
get = function () {
const index = nextIndex();
const result = copy[index];
removeIndex(index);
return result;
}
;
return {
get
};
};
const Ads = function (data) {
let copy = data;
let client = null;
const
nextIndex = function () {
const data = copy[client];
return Math.floor(Math.random() * data.length);
},
reset = function () {
copy[client] = data[client];
},
removeIndex = function (index) {
copy[client].splice(index, 1)
if (!copy[client].length) {
reset(client);
}
},
get = function (idClient) {
client = idClient;
const index = nextIndex();
const result = copy[client][index];
removeIndex(index);
return result;
}
;
return {
getFromClient : get
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment