This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function uses dijkstra algorithm to find the shortest way in graph. | |
* So for this function to work properly, graph must be directed and acyclic with positive weights only | |
*/ | |
export default function findShortestWayIn(graph) { | |
const minimumCosts = {}; | |
let queue = [{}]; | |
const visitedPointsFromSpecificParent = {}; // to detect a cycle in a graph | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function generator() { | |
const valuesToReturn = generator.prototype.valuesToReturn; | |
return { | |
value: null, | |
done: null, | |
next() { | |
const returnThis = valuesToReturn.pop(); | |
return { | |
value: returnThis, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findMetrics(values) { | |
return Math.hypot(...values); | |
} | |
export function updateWithMetrics(usersVotes) { | |
if (obj(usersVotes).doesntExist()) return null; | |
for (let i = 0, len = usersVotes.length; i < len; i++) { | |
const currentUser = usersVotes[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function findAmountOfSmallestUnitLengthClosedIntervals(sortedPoints) { | |
if(sortedPoints.length <= 1) return 1; | |
let amount = 0; | |
for (let i = 0, len = sortedPoints.length - 1; i < len;) { | |
let unitClosed = false; | |
for (let z = i + 1; z < sortedPoints.length; z++) { | |
if (sortedPoints[z] - sortedPoints[i] < 1) { | |
unitClosed = true; | |
if (z === sortedPoints.length - 1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class AsyncCollector { | |
gotData = []; | |
constructor(timeout, ...funcs) { | |
return new Promise((resolve, reject) => { | |
const promisesOfFuncs = funcs.map(item => item()); | |
Promise.all(promisesOfFuncs) | |
.then(values => this.gotData = values) | |
.then(() => { | |
setTimeout(() => { | |
resolve(this.gotData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function* generateIDS(fromID) { | |
while (true) { | |
yield fromID++; | |
} | |
} | |
// Example of usage | |
// const generator = generateIDS(0); | |
// const MichaelID = generator.next().value; | |
// const DimaID = generator.next().value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class RandomNameGenerator { | |
constructor(amountOfNames, lengthOfNames) { | |
this.amountOfNames = amountOfNames; | |
this.lengthOfNames = lengthOfNames; | |
this.names = new Array(); | |
}; | |
makeName(stringLength) { | |
let result = ''; | |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
const charactersLength = characters.length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function* idGeneration(start = 0) { | |
while(true) { | |
const newID = yield start++; | |
if (typeof newID !== 'undefined') { | |
start = newID; | |
yield start++; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function compose(...fns) { | |
return async x => { | |
let res = x; | |
for (const fn of fns) { | |
res = await fn(res); | |
} | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let range = { | |
start: 0, | |
finish: 10 | |
} | |
range = new Proxy(range, { | |
has(target, property) { | |
if (property >= range.start && range.finish >= property) return true; | |
else return false; | |
} |