• Mar 11, 2021 •C S
0 likes • 0 views
require("dotenv").config(); const mongoose = require("mongoose"); const db = process.env.MONGO_URI; const connectDB = async () => { try { await mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true }); console.log("MongoDB Connected"); } catch (err) { console.error(err.message); } }; module.exports = connectDB;
• Nov 19, 2022 •CodeCatch
0 likes • 3 views
const deepMerge = (a, b, fn) => [...new Set([...Object.keys(a), ...Object.keys(b)])].reduce( (acc, key) => ({ ...acc, [key]: fn(key, a[key], b[key]) }), {} ); deepMerge( { a: true, b: { c: [1, 2, 3] } }, { a: false, b: { d: [1, 2, 3] } }, (key, a, b) => (key === 'a' ? a && b : Object.assign({}, a, b)) ); // { a: false, b: { c: [ 1, 2, 3 ], d: [ 1, 2, 3 ] } }
0 likes • 1 view
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)
• Jan 26, 2023 •AustinLeath
0 likes • 6 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
• Nov 18, 2022 •AustinLeath
const dragAndDropDiv = editor.container; dragAndDropDiv.addEventListener('dragover', function(e) { e.stopPropagation(); e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; }); dragAndDropDiv.addEventListener("drop",function(e){ // Prevent default behavior (Prevent file from being opened) e.stopPropagation(); e.preventDefault(); const files = e.dataTransfer.items; // Array of all files console.assert(files.length >= 1); if (files[0].kind === 'file') { var file = e.dataTransfer.items[0].getAsFile(); const fileSize = file.size; const fileName = file.name; const fileMimeType = file.type; reader = new FileReader(); reader.onloadend = function(){ editor.setValue(reader.result); } reader.readAsText(file); }else{ //Maybe handle if text is dropped console.log(files[0].kind); } });
• Oct 16, 2023 •C S
0 likes • 9 views
class TreeNode { constructor(data, depth) { this.data = data; this.children = []; this.depth = depth; } } function buildNaryTree(hosts) { const nodeMap = {}; // Step 1: Create a map of nodes using fqdn as the key hosts.forEach((host) => { nodeMap[host.fqdn] = new TreeNode(host, 0); // Initialize depth to 0 }); // Step 2: Iterate through the array and add each node to its parent's list of children hosts.forEach((host) => { if (host.displayParent) { if (nodeMap[host.displayParent]) { const parent = nodeMap[host.displayParent]; const node = nodeMap[host.fqdn]; node.depth = parent.depth + 1; // Update the depth parent.children.push(node); } else { console.error(`Parent with fqdn ${host.displayParent} not found for ${host.fqdn}`); } } }); // Find the root nodes (nodes with no parent) const rootNodes = hosts.filter((host) => !host.displayParent).map((host) => nodeMap[host.fqdn]); return rootNodes; } function treeToObjects(node) { const result = []; function inOrder(node) { if (!node) { return; } // Visit the current node and add it to the result result.push(node.data); // Visit children nodes first node.children.forEach((child) => { inOrder(child); }); } inOrder(node); return result; } // Usage example const hosts = [ { fqdn: 'fqdn_1', display_name: 'Host 1', }, { fqdn: 'fqdn_2', display_name: 'Host 2', displayParent: 'fqdn_1', }, { fqdn: 'fqdn_3', display_name: 'Host 3' }, { fqdn: 'fqdn_4', display_name: 'Host 4', displayParent: 'fqdn_3', }, { fqdn: 'fqdn_5', display_name: 'Host 5', displayParent: 'fqdn_1', }, ]; const tree = buildNaryTree(hosts); // Function to convert the tree to an array of objects function convertTreeToArray(nodes) { const result = []; nodes.forEach((node) => { result.push(...treeToObjects(node)); }); return result; } // Convert the tree back to an array of objects const arrayFromTree = convertTreeToArray(tree); console.log(arrayFromTree);