minor: refined scopes, some more graphing, ocd

This commit is contained in:
2024-08-01 00:01:27 +05:30
parent 9bc0cb651d
commit 149965a15b
7 changed files with 52 additions and 32 deletions

View File

@@ -6,7 +6,21 @@ const typedefs = require("../typedefs");
* Directed graph, may or may not be connected.
*
* NOTE: Assumes that nodes and edges are valid.
*/
*
* Example:
* ```javascript
* let nodes = ['a', 'b', 'c', 'd', 'e'];
* let edges = [
* { from: 'a', to: 'b' },
* { from: 'b', to: 'c' },
* { from: 'c', to: 'd' },
* { from: 'd', to: 'a' },
* { from: 'e', to: 'a' }
* ];
* let g = new myGraph(nodes, edges);
* console.log(g.detectCycle()); // true
* ```
*/
class myGraph {
/**
* @param {string[]} nodes Graph nodes IDs
@@ -21,7 +35,15 @@ class myGraph {
* @param {type} node
* @returns {string[]}
*/
getNeighbors(node) {
getDirectHeads(node) {
return this.edges.filter(edge => edge.to == node).map(edge => edge.from);
}
/**
* @param {type} node
* @returns {string[]}
*/
getDirectTails(node) {
return this.edges.filter(edge => edge.from == node).map(edge => edge.to);
}
@@ -56,10 +78,10 @@ class myGraph {
let node = zeroInDegreeQueue.shift();
topologicalOrder.push(node);
for (let neighbor of this.getNeighbors(node)) {
inDegree[neighbor]--;
if (inDegree[neighbor] === 0) {
zeroInDegreeQueue.push(neighbor);
for (let tail of this.getDirectTails(node)) {
inDegree[tail]--;
if (inDegree[tail] === 0) {
zeroInDegreeQueue.push(tail);
}
}
}

View File

@@ -7,10 +7,10 @@ const base62Pattern = /^[A-Za-z0-9]+$/;
* Returns type and ID from a Spotify URI
* @see {@link https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids|Spotify URIs and IDs}
* @param {string} uri Spotify URI - can be of an album, track, playlist, user, episode, etc.
* @returns {typedefs.UriObject}
* @returns {typedefs.URIObject}
* @throws {TypeError} If the input is not a valid Spotify URI
*/
const parseSpotifyUri = (uri) => {
const parseSpotifyURI = (uri) => {
const parts = uri.split(":");
if (parts[0] !== "spotify") {
@@ -56,7 +56,7 @@ const parseSpotifyUri = (uri) => {
/**
* Returns type and ID from a Spotify link
* @param {string} link Spotify URL - can be of an album, track, playlist, user, episode, etc.
* @returns {typedefs.UriObject}
* @returns {typedefs.URIObject}
* @throws {TypeError} If the input is not a valid Spotify link
*/
const parseSpotifyLink = (link) => {
@@ -103,10 +103,10 @@ const parseSpotifyLink = (link) => {
/**
* Builds URI string from a URIObject
* @param {typedefs.UriObject} uriObj
* @param {typedefs.URIObject} uriObj
* @returns {string}
*/
const buildSpotifyUri = (uriObj) => {
const buildSpotifyURI = (uriObj) => {
if (uriObj.is_local) {
const artist = encodeURIComponent(uriObj.artist ?? '');
const album = encodeURIComponent(uriObj.album ?? '');
@@ -119,7 +119,7 @@ const buildSpotifyUri = (uriObj) => {
/**
* Builds link from a URIObject
* @param {typedefs.UriObject} uriObj
* @param {typedefs.URIObject} uriObj
* @returns {string}
*/
const buildSpotifyLink = (uriObj) => {
@@ -134,8 +134,8 @@ const buildSpotifyLink = (uriObj) => {
}
module.exports = {
parseSpotifyUri,
parseSpotifyURI,
parseSpotifyLink,
buildSpotifyUri,
buildSpotifyURI,
buildSpotifyLink
}