Loading...
More JavaScript Posts
class Graph {constructor(directed = true) {this.directed = directed;this.nodes = [];this.edges = new Map();}addNode(key, value = key) {this.nodes.push({ key, value });}addEdge(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}removeNode(key) {this.nodes = this.nodes.filter(n => n.key !== key);[...this.edges.values()].forEach(({ a, b }) => {if (a === key || b === key) this.edges.delete(JSON.stringify([a, b]));});}removeEdge(a, b) {this.edges.delete(JSON.stringify([a, b]));if (!this.directed) this.edges.delete(JSON.stringify([b, a]));}findNode(key) {return this.nodes.find(x => x.key === key);}hasEdge(a, b) {return this.edges.has(JSON.stringify([a, b]));}setEdgeWeight(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}getEdgeWeight(a, b) {return this.edges.get(JSON.stringify([a, b])).weight;}adjacent(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc.push(b);return acc;}, []);}indegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (b === key) acc++;return acc;}, 0);}outdegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc++;return acc;}, 0);}}const g = new Graph();g.addNode('a');g.addNode('b');g.addNode('c');g.addNode('d');g.addEdge('a', 'c');g.addEdge('b', 'c');g.addEdge('c', 'b');g.addEdge('d', 'a');g.nodes.map(x => x.value); // ['a', 'b', 'c', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'c => b', 'd => a']g.adjacent('c'); // ['b']g.indegree('c'); // 2g.outdegree('c'); // 1g.hasEdge('d', 'a'); // trueg.hasEdge('a', 'd'); // falseg.removeEdge('c', 'b');[...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'd => a']g.removeNode('c');g.nodes.map(x => x.value); // ['a', 'b', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['d => a']g.setEdgeWeight('d', 'a', 5);g.getEdgeWeight('d', 'a'); // 5
const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);// ExamplesgetSubsets([1, 2]); // [[], [1], [2], [1, 2]]getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
// Get the text that the user has selectedconst getSelectedText = () => window.getSelection().toString();getSelectedText();
const mongoose = require('mongoose')const UserSchema = new mongoose.Schema({username: {type: String,required: true},email: {type: String,unique: true,required: true},password: {type: String,required: true},date: {type: Date,default: Date.now}})module.exports = User = mongoose.model('user', UserSchema)
function copyString(text){async function navigatorClipboardCopy(text){if(document.location.protocol != "https:"){return false;}return new Promise((resolve, reject)=>{navigator.clipboard.writeText(text).then(()=>{resolve(true);}, ()=>{resolve(false);});});}function domCopy(text){if(!(document.execCommand)){console.warn("They finally deprecated document.execCommand!");}const input = document.createElement('textarea');input.value = text;document.body.appendChild(input);input.select();const success = document.execCommand('copy');document.body.removeChild(input);return success;}function promptCopy(){prompt("Copy failed. Might have ... somewhere. Check console.", text);console.log(text);}function done(){alert("Copied to clipboard");}navigatorClipboardCopy(text).catch(()=>{return false;}).then((success)=>{if(success){done();}else{if(domCopy(text)){done();}else{promptCopy();}}});}
if (process.env.NODE_ENV === "production") {app.use(express.static("client/build"));app.get("*", (req, res) => {res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));});}