Loading...
More JavaScript Posts
// Orconst randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;console.log(randomColor);
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]]
const unzipWith = (arr, fn) =>arr.reduce((acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),Array.from({length: Math.max(...arr.map(x => x.length))}).map(x => [])).map(val => fn(...val));unzipWith([[1, 10, 100],[2, 20, 200],],(...args) => args.reduce((acc, v) => acc + v, 0));// [3, 30, 300]
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;
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 keyhosts.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 childrenhosts.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 depthparent.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 resultresult.push(node.data);// Visit children nodes firstnode.children.forEach((child) => {inOrder(child);});}inOrder(node);return result;}// Usage exampleconst 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 objectsfunction convertTreeToArray(nodes) {const result = [];nodes.forEach((node) => {result.push(...treeToObjects(node));});return result;}// Convert the tree back to an array of objectsconst arrayFromTree = convertTreeToArray(tree);console.log(arrayFromTree);
import Head from 'next/head'import Image from 'next/image'import styles from '../styles/Home.module.css'const { exec } = require("child_process");exec("sensors|grep "high"|grep "Core"|cut -d "+" -f2|cut -d "." -f1|sort -nr|sed -n 1p", (error, stdout, stderr) => {if (error) {console.log(`error: ${error.message}`);return;}if (stderr) {console.log(`stderr: ${stderr}`);return;}console.log(`stdout: ${stdout}`);});export default function Home() {return (<div className={styles.container}><Head><title>Create Next App</title><meta name="description" content="Generated by create next app" /><link rel="icon" href="/favicon.ico" /></Head><main className={styles.main}><h1 className={styles.title}>Welcome to <a href="https://nextjs.org">Next.js!</a></h1><p className={styles.description}>Get started by editing{' '}<code className={styles.code}>pages/index.js</code></p><div className={styles.grid}><a href="https://nextjs.org/docs" className={styles.card}><h2>Documentation →</h2><p>Find in-depth information about Next.js features and API.</p></a><a href="https://nextjs.org/learn" className={styles.card}><h2>Learn →</h2><p>Learn about Next.js in an interactive course with quizzes!</p></a><ahref="https://github.com/vercel/next.js/tree/canary/examples"className={styles.card}><h2>Examples →</h2><p>Discover and deploy boilerplate example Next.js projects.</p></a><ahref="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"className={styles.card}><h2>Deploy →</h2><p>Instantly deploy your Next.js site to a public URL with Vercel.</p></a></div></main><footer className={styles.footer}><ahref="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"target="_blank"rel="noopener noreferrer">Powered by{' '}<span className={styles.logo}><Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} /></span></a></footer></div>)}