How to implement readLinesSync[path[, options] in Javascript?

In a more perfect world, node.js would include this in its fs functions.

I am familiar with the node.js filesystem documentatation. I’m also familiar with the readLines method of fs. Sadly, I find no readLinesSync function in the above node.js filesystem documentation — that’s very close to what I’m looking for.

I need a Javascript function that reads and answers specified lines from
potentially large files. Since the files I’m reading might have thousands of
lines of text, I do not want read the entire file at once.

I’m implementing a server-side REST API using node.js running on a robust AWS
EC2 instance running Rocky Linux. I have full control over the platform.

I’m lost in the complexity of async/await, promises, and so on.

Here is the definition of the function I want to implement:

function readLinesSync(path, options) {
  const lines = doWhateverItTakes()
  return lines
}

The arguments to the above readLinesSync are as follows:

  • @path: The full absolute path of a possibly large text file. A typical value is '/home/tms/example_documents/example_1.txt'

  • @options: A literal object containing at least the following bindings:

    • @startLineIndex: The zero-origin index of a line in the text file. A typical value is 42

    • @stopLineIndex: The zero-origin index of a line in the text file. A typical value is 142

When readLinesSync is called with the above typical values, I expect it to answer an array containing 100 lines of text read from the specified file, starting at line 42.

Importantly, this function must not return until the necessary file I/O is complete. Any complexity involving async/await or promises must be somehow encapsulated elsewhere.

Ideally, it will somehow catch and report any errors thrown along the way.

The project uses import ... rather than require ....

I’m familiar with a stackoverflow related question. Sadly, the answer to that question only emits lines to the console. This is unresponsive to the most challenging aspect of my question.

In the most general case, I seek a pattern for encapsulating async behavior
behind a synchronous wrapper.

How to modify javascript to have the table start sorted on a certain column on page load

I have looked at other questions like this but I am so unused to javascript that I cannot understand how to integrate their solutions into my code. I need someone to look at mine and tell me what to do. Thank you.

This javascript came from a pre-built table made to be more accessible for assistive technology. I would love for the table to start sorted A-Z on the first column (Products). Here is the code:

https://codepen.io/The-Alberta-Library/pen/zYVyroJ

Thanks again.

The javascript:

/*
 *   This content is licensed according to the W3C Software License at
 *   https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
 *
 *   File:   sortable-table.js
 *
 *   Desc:   Adds sorting to a HTML data table that implements ARIA Authoring Practices
 */

'use strict';

class SortableTable {
  constructor(tableNode) {
    this.tableNode = tableNode;

    this.columnHeaders = tableNode.querySelectorAll('thead th');

    this.sortColumns = [];

    for (var i = 0; i < this.columnHeaders.length; i++) {
      var ch = this.columnHeaders[i];
      var buttonNode = ch.querySelector('button');
      if (buttonNode) {
        this.sortColumns.push(i);
        buttonNode.setAttribute('data-column-index', i);
        buttonNode.addEventListener('click', this.handleClick.bind(this));
      }
    }

    this.optionCheckbox = document.querySelector(
      'input[type="checkbox"][value="show-unsorted-icon"]'
    );

    if (this.optionCheckbox) {
      this.optionCheckbox.addEventListener(
        'change',
        this.handleOptionChange.bind(this)
      );
      if (this.optionCheckbox.checked) {
        this.tableNode.classList.add('show-unsorted-icon');
      }
    }
  }

  setColumnHeaderSort(columnIndex) {
    if (typeof columnIndex === 'string') {
      columnIndex = parseInt(columnIndex);
    }

    for (var i = 0; i < this.columnHeaders.length; i++) {
      var ch = this.columnHeaders[i];
      var buttonNode = ch.querySelector('button');
      if (i === columnIndex) {
        var value = ch.getAttribute('aria-sort');
        if (value === 'descending') {
          ch.setAttribute('aria-sort', 'ascending');
          this.sortColumn(
            columnIndex,
            'ascending',
            ch.classList.contains('num')
          );
        } else {
          ch.setAttribute('aria-sort', 'descending');
          this.sortColumn(
            columnIndex,
            'descending',
            ch.classList.contains('num')
          );
        }
      } else {
        if (ch.hasAttribute('aria-sort') && buttonNode) {
          ch.removeAttribute('aria-sort');
        }
      }
    }
  }

  sortColumn(columnIndex, sortValue, isNumber) {
    function compareValues(a, b) {
      if (sortValue === 'ascending') {
        if (a.value === b.value) {
          return 0;
        } else {
          if (isNumber) {
            return a.value - b.value;
          } else {
            return a.value < b.value ? -1 : 1;
          }
        }
      } else {
        if (a.value === b.value) {
          return 0;
        } else {
          if (isNumber) {
            return b.value - a.value;
          } else {
            return a.value > b.value ? -1 : 1;
          }
        }
      }
    }

    if (typeof isNumber !== 'boolean') {
      isNumber = false;
    }

    var tbodyNode = this.tableNode.querySelector('tbody');
    var rowNodes = [];
    var dataCells = [];

    var rowNode = tbodyNode.firstElementChild;

    var index = 0;
    while (rowNode) {
      rowNodes.push(rowNode);
      var rowCells = rowNode.querySelectorAll('th, td');
      var dataCell = rowCells[columnIndex];

      var data = {};
      data.index = index;
      data.value = dataCell.textContent.toLowerCase().trim();
      if (isNumber) {
        data.value = parseFloat(data.value);
      }
      dataCells.push(data);
      rowNode = rowNode.nextElementSibling;
      index += 1;
    }

    dataCells.sort(compareValues);

    // remove rows
    while (tbodyNode.firstChild) {
      tbodyNode.removeChild(tbodyNode.lastChild);
    }

    // add sorted rows
    for (var i = 0; i < dataCells.length; i += 1) {
      tbodyNode.appendChild(rowNodes[dataCells[i].index]);
    }
  }

  /* EVENT HANDLERS */

  handleClick(event) {
    var tgt = event.currentTarget;
    this.setColumnHeaderSort(tgt.getAttribute('data-column-index'));
  }

  handleOptionChange(event) {
    var tgt = event.currentTarget;

    if (tgt.checked) {
      this.tableNode.classList.add('show-unsorted-icon');
    } else {
      this.tableNode.classList.remove('show-unsorted-icon');
    }
  }
}

// Initialize sortable table buttons
window.addEventListener('load', function () {
  var sortableTables = document.querySelectorAll('table.sortable');
  for (var i = 0; i < sortableTables.length; i++) {
    new SortableTable(sortableTables[i]);
  }
});

I have looked at a lot of other threads on how to start tables sorted on a certain column at page load, but none of them make sense to me because I know nothing at all about javascript and cannot understand how to integrate their solutions, especially since none of them are working on code that is optimized for assistive technology.

How could I better convert a child object of arrays to one array of objects when flattening the parent?

I’m receiving data and scratching my head to find a cleaner way to modify the data in a singular one level array of objects:

data:

{
  dayMon: [
    {
      id: 1,
      name: 'whatever'
    }
  ],
  dayTues: [],
  dayWed: [],
  nextWeek: [
    {
      dayTues: [{
        id: 3,
        name: 'whatever'
      }],
      dayMon: [],
      dayThurs: []
    }
  ]
}

and my goal is:

[
  {
    id: 1,
    name: 'whatever'
  },
  {
    id: 3,
    name: 'whatever'
  }
]

so I created a function:

export function buildDays(data: {[s: string]: unknown} | ArrayLike<unknown>) {
  const flatDays = Object?.values(data)?.flat() || []
  if (!flatDays || flatDays?.length === 0) return null
  
  const nextWeek: unknown[][] = []
  if (data.hasOwnProperty('nextWeek')) {
    data?.nextWeek.map((day: any) => nextWeek.push(Object?.values(day)?.flat()))
  }

  const cleaned = [...flatDays, ...nextWeek?.flat()].filter(Boolean)
  return cleaned
}

but my issue falls under nextWeek an at current implementation ...reuseBlock?.flat() throws a TypeScript warning of:

Unsafe usage of optional chaining. If it short-circuits with ‘undefined’ the evaluation will throw TypeError.

Reading

In Vanilla JavaScript is there a better way to build the data into one array of objects?

Parse embedded XML with JavaScript

I’m failing to parse a XML document embedded in a script tag with Vanilla JavaScript:

document.addEventListener('DOMContentLoaded', function() {
  const xmlStr = document.getElementById('xml').innerText;
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xmlStr,'text/xml');
  const barText = xmlDoc.getElementById('bar').textContent;
  alert(barText)
});
<body>

  <h1>Parsing &lt;script type="text/xml"&gt with JavaScript</h1>

  <script id="xml" type="text/xml">
<?xml version="1.0" encoding="utf-8"?>
<foo>
  <bar id="bar">Hello world!</bar>
</foo>
  </script>

</body>

What’s the problem here?

How to configure code indentation in VSCode?

I would like to clarify a question about VSCode indentation.

My employee is using the Prettier extension for code formatting.

A function that was supposed to look like this:

function evaluateCandidate(age, salary, experience, educationLevel) {
  if (age < 18) {
      return "Age below the minimum required.";
  } else if (age > 65) {
      return "Age above the allowed limit.";
  }
  
  if (salary < 3000) {
      return "Salary below the minimum acceptable.";
  } else if (salary > 10000) {
      return "Salary above the maximum acceptable.";
  }
  
  if (experience < 2) {
      return "Insufficient experience.";
  }
  
  if (educationLevel !== "Bachelor's Degree" && educationLevel !== "Master's Degree" && educationLevel !== "PhD") {
      return "Education level not met.";
  }
  
  return "Candidate approved!";
}

Is being formatted like this:

function evaluateCandidate(
    age,
    salary,
    experience,
    educationLevel,
  ) {
  if (
    age < 18
  ) {
    return "Age below the minimum required.";
  } else if (
    age > 65
  ) {
    return "Age above the allowed limit.";
  }

  if (
    salary < 3000
  ) {
    return "Salary below the minimum acceptable.";
  } else if (
    salary > 10000
  ) {
    return "Salary above the maximum acceptable.";
  }

  if (
    experience < 2
  ) {
    return "Insufficient experience.";
  }

  if (
    educationLevel !== "Bachelor's Degree" &&
    educationLevel !== "Master's Degree" &&
    educationLevel !== "PhD"
  ) {
    return "Education level not met.";
  }

  return "Candidate approved!";
}

For a simple function like this, it might not make much difference. But in a large project, it makes a significant impact because other employees waste time adjusting the indentation and trying to understand what this ****** is doing.

I would like to know if there is a way for other employees to configure something to remove or override Prettier’s indentation.

If there is no way, I will need to dismiss him.

I have already tried configuring Prettier to undo the default settings it applies, but it doesn’t work.

How to download .nitro-files from a website?

I want to download all the .nitro-files from a website, using DevTools console.

This is the code I allready have

(async function downloadNitroFilesFromNetwork() {
    // Überprüft ob die DevTools geöffnet sind & ob der Netzwerk-Tab ausgewählt ist
    if (!window.performance || !window.performance.getEntriesByType) {
        console.error("DevTools müssen geöffnet und der Netzwerk-Tab ausgewählt sein");
        return;
    }

    const nitroFiles = [];

    const entries = performance.getEntriesByType('resource');

    // Filtert Dateien mit .nitro-Endung heraus
    entries.forEach(entry => {
        if (entry.name.endsWith('.nitro')) {
            nitroFiles.push(entry.name);
        }
    });

    // Lädt die .nitro-Dateien einzeln herunter
    for (const url of nitroFiles) {
        const response = await fetch(url);
        
        if (!response.ok) {
                console.error(`Failed to download ${url}: ${response.statusText}`);
                continue;
        }
        
        const blob = await response.blob();
        const link = document.createElement('a');
        const fileName = url.split('/').pop();
        link.href = URL.createObjectURL(blob);
        link.download = fileName;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }

    console.log(`Downloaded ${nitroFiles.length} .nitro files`);
})();

The website I want to download the files from, has CORS enabaled. I seams like the script dont get any Information because the output is: Downloaded 0 .nitro files

I tried CORS-Blocker-Extension and a Proxy-Server allready

Selecting correct types from an union based on another property in mapping function

I am converting some old project to Typescript recently and got stuck with some nested object types… Basically I have two types created based on backend response object and they have some common properties eg. index but also some unique ones eg. sharkEnabled:

type TSharkItem = {
  index: string;
  sharkEnabled: boolean;
};

type TGrapeItem = {
  index: string;
  grapeEnabled: boolean;
};

// Union type of data for later use
type TData = TGrapeItem[] | TSharkItem[];

Now I have mapping function mapDataToState which will need to know which type of TData to use and create proper state out of it. To do that, there was created some kind of “connector/mapping” between fields:

// Connector/mapping
const columnMap = {
  fruit: {
    enabledColumn: "grapeEnabled",
  },
  fish: {
    enabledColumn: "sharkEnabled",
  },
} as const;
type ColumnMap = typeof columnMap;
type AvailableColumnName = ColumnMap["fish"] | ColumnMap["fruit"];


// Return type of mapDataToState fn
type State = { isEnabled: boolean }[];


const mapDataToState = (data: TData, columnNames: AvailableColumnName): State => {
  return data.reduce((memo, row) => {
    return [
      ...memo,
      {
        isEnabled: row[columnNames.enabledColumn], // ERROR: Property 'grapeEnabled' does not exist on type 'TSharkItem | TGrapeItem'.
      },
    ];
  }, [] as State);
};

I understood the error from TS, that for sure grapeEnabled only exist in TGrapeItem type, not TSharkItem – but unfortunately I’m quite fresh in TS world and I’m not sure if that’s even possible to achieve.

Any hints much appreciated 🙂

Link to TS playground:

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBKA2BXAtmAsgQwA4wLwwG8AoGGAMwCdEBLKALkJNJgFMwMAjeFgEwGEQSVAwBEAcwrYWAUXZdeIgDRMAvstJlqEABYNizVnO79BKMKJ0YKAa1mdjS1cpUwMEOOGgBuIkSgBPLBYYASF0bDwYAKCQMg8wzCwfaOCAQQA3DGp4exZQswA5DGRg-HzURIBtEU0dEQBdGAAfENMK7GrKGigGnz9A4IAVAGVtK2sASSgWZEj9GGowHhYADwZoCkWxH1JLGzt5HgYOEEEWDDAfFT6UmEGAcUkgqZm5pkXltZgNrZ2YCSkB2Mx1O3AuVxuAzuABEMFAMJEHk8WC9kJVGi0RmMbKj0ZCgjBhvDpnMFhAgbwQWcLjAVHiiKBILBkNhYfDBiAiXDSjAABQ8OEYBiDNkYRTxQrFFgQBgZLI5eTlMBFEoASgYXJJuAAfIxSBQWFBEBQwDABfCAHQGniIYAsXm8krIEDiiggADuqrwuvm+sNxtNlSYBgtoadLuDzF9BjJFKOMDd7sqCElJQgFrYuRMYXq6hjakj9T+ahg6Nc7k1LFVEKIQA

Best approach for a javascript Lightbox on an image grid created using json

I’m working on my portfolio, and I want to create a Lightbox that activates when a user clicks on an image.

The images are added to the dom via a javascript that fetches a json file with the paths to the images :

const imagesData = "images.json"

const display = document.getElementById("grid-container")

const getData = async () => {
    const response = await fetch(imagesData)
    if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`)
    }
    const data = await response.json()
    return data
}

const displayImages =async () => {
    const content = await getData()

    let dataDisplay = content.map((object) => {
        return `
        <div class="grid-item">
            <img src="${object.path}" alt="${object.name}" class="grid-image">
        </div>
        `
    }).join("")

    display.innerHTML = dataDisplay
}
displayImages()
<!DOCTYPE html>
<html lang="fr">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        
        <title>Grille et lightbox</title>
        <meta name="author" content="Benoît Aguesse" />
        <meta name="description" content="description" />
        
        <link href="styles.css" rel="stylesheet" />
        <script src="grille.js" defer></script>
        <script src="lightbox.js" defer></script>
    </head>
    <body>
        <h1>Photo gallery</h1>
        <p>Click on an image</p>
        <div id="grid-container"></div>
        <!-- <div class="lightbox show">
            <button class="lightbox-close">Fermer</button>
            <button class="lightbox-next">Suivant</button>
            <button class="lightbox-previous">Précédent</button>
            <div class="lightbox-container">
                <img src="images/Hourtin 1.jpeg" alt="" class="lightbox-image">
            </div>
            <span class="lightbox-info">
                <p>Titre</p>
                <p>Description</p>
                <p>Métadonnées</p>
            </span>
        </div> -->
    </body>
</html>

*The commented html bit is a draft to setup the Lightbox content and layout

I tried following different tutorials on implementing a Lightbox, but none really worked… I’m relatively new to javascript but this an opportunity to learn ! the functionalities I’m looking for are :

  1. opening the Lightbox showing a larger (better resolution) version of the clicked image, the title and some metadata that are stored in the json file used to create the image grid
  2. having a “close”, “previous” and “next” button to cycle trough the images while being in the Lightbox (ideally it also recognizes arrow keystrokes and swipe gestures, if possible)

What are the general principles or best practices to guide me on the approach to have ? Should I build the Lightbox in html and uses javascript to activate it and modify it to cycle trough the images, or use javascript to create html on the fly ?

I tried wrapping the images in a <a> tag and using javascript to prevent the default behavior and use the link to display the image in the Lightbox but it did not prevent the default behavior, the image still opened as a file :

class Lightbox {

    static init() {
        const links = document.querySelectorAll('a[href$=".jpeg"]')
        .forEach(link => link.addEventListener('click', e => {
            e.preventDefault()
            new Lightbox(e.currentTarget.getAttribute('href'))
        }))
    }

    constructor(url) {
        const element = this.buildDOM(url)
        document.body.appendChild(element)
    }

    buildDOM(url) {
        const dom = document.createElement('div')
        dom.classList.add('lightbox')
        dom.innerHTML = `
            <button class="lightbox-close">Fermer</button>
            <button class="lightbox-next">Suivant</button>
            <button class="lightbox-previous">Précédent</button>
            <div class="lightbox-container">
                <img src="${url}" alt="" class="lightbox-image">
            </div>
        `
        return dom
    }

}

Index.js not rendering in browser

Project: Fullstack dockerized project. Backend with Express and frontend with Vite and React. Front and back-end in same project.

Backend code:

require("dotenv").config();

const express = require("express");
const mysql = require("mysql");
const bodyParser = require("body-parser");
const path = require("path")


const cors = require("cors");

const app = express();

const PORT = process.env.PORT || 3001;

app.use(cors({origin : 'https://qquestlabs.nl/'}));
app.use(bodyParser.json());

const db = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});
app.use(express.static(path.join(__dirname, 'frontend', 'dist')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'frontend', 'dist', 'index.html'));
});

db.connect((err) => {
  if (err) {
    throw err;
  }
  console.log("MySQL Connected...");
});

app.post("/api/submit", (req, res) => {
  const { name, email, phoneNumber, dateOfEntry } = req.body;
  const query =
    "INSERT INTO DeelnemerGegevens (Naam, Email, Telefoonnummer, Invuldatum) VALUES ( ?, ?, ?, CURDATE())";
  db.query(query, [name, email, phoneNumber, dateOfEntry], (err, result) => {
    if (err) {
      return res.status(500).send(err);
    }
    res.status(200).send("Data inserted");
  });
});

app.get('api/health', (req, res) => {
  res.send('ok')
})

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Front-end vite config:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";


export default defineConfig({
  plugins: [react()],
  // build: {
  //   minify: false,
  // },
  base: "./",
  preview: {
    port: 5173,
    strictPort: true,
  },
  server: {
    port: 5173,
    strictPort: true,
    host: true,
  },
});

Dockerfile:

FROM node:20.11-alpine AS frontend

WORKDIR /app/frontend

# Copy package.json and package-lock.json to install dependencies for the frontend
COPY frontend/package*.json ./

# Use a cache for npm modules to speed up builds
RUN --mount=type=cache,target=/root/.npm 
    npm ci

# Copy the rest of the frontend application code
COPY frontend .

# Build the frontend (this outputs the built static files to a 'dist' or similar directory)
RUN npm run build

# Stage 2: Run the backend and serve the frontend using Node.js
FROM node:20.11-alpine

WORKDIR /app

# Copy package.json and package-lock.json for backend dependencies
COPY package*.json ./

# Use a cache for npm modules to speed up builds
RUN --mount=type=cache,target=/root/.npm 
    npm ci

# Copy the backend server code
COPY . .

# COPY ./.env ./

# Copy the built frontend files from the first stage into the correct directory in the backend
COPY --from=frontend /app/frontend/dist ./frontend/dist

# Expose the backend port (adjust this if your server uses a different port)
EXPOSE 3001

# Run the server using server.js
CMD ["npm", "run" , "start"]

Nginx config for this project:

location /project-guus/ {
    proxy_pass http://project-guus-prod/;
    add_header Access-Control-Allow-Origin *;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Connection $connection_upgrade;
}

Issue:

When i run the docker container in localhost everything works well. When i push the project in my DigitalOcean droplet and proxy it with Nginx container then the index.js that is being created seems not to load properly and i dont see any content. The index.css in the same file render properly.

What i have tried:

I have changed the content of the gerenated index.js to a single console.log and this shows properly. I see further not errors or issues in the network tab

Google Multi Line Chart not showing Line Series

I have a problem in Drawing and filtering the charts on the same div. My code works without an error checked it on the console, but my charts looks like this in the browser

enter image description here

Here is my data

[["SalesDiv","date","volume"],
["National","2024-04-30T18:30:00.000Z",100],
["National","2024-05-30T18:30:00.000Z",403],
["National","2024-06-30T18:30:00.000Z",678],
["National","2024-07-30T18:30:00.000Z",700],

["New City","2024-04-30T18:30:00.000Z",90],
["New City","2024-05-31T18:30:00.000Z",200],
["New City","2024-06-30T18:30:00.000Z",500],
["New City","2024-07-31T18:30:00.000Z",300],

["old City","2024-04-30T18:30:00.000Z",10],
["old City","2024-05-31T18:30:00.000Z",203],
["old City","2024-06-30T18:30:00.000Z",178],
["old City","2024-07-31T18:30:00.000Z",400]]

My Script code is

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript"> 
google.charts.load('current',{'packages':['corechart']});
google.charts.setOnLoadCallback(drawchart);

//Getting Data from AppScript function
function drawchart(){  
  google.script.run.withSuccessHandler(displaychart).funct_data();
}

let filter_selection =[];
//Displaying charts in the Div
function displaychart(c_data1){
    var arrMain = new Array();

        for(i=0; i<c_data1.length;i++){
          var arr = new Array(c_data1[i].date,c_data1[i].volume,c_data1[i].SalesDiv);
          arrMain.push(arr);
        }

        var datatable = new google.visualization.DataTable();
        datatable.addColumn({type: 'date', label: 'x'});
        datatable.addColumn({type: 'number', label: 'y'});
        datatable.addColumn({type:'string',role:'annotation'}); 
        datatable.addRows(arrMain);  
        console.log(datatable);             
      
        var chart = new google.visualization.LineChart(document.getElementById('line_cont'));
        chart.draw(datatable);
      }
// Function to filter the data on Dropdown select & Button click
     function filtering(){                
        google.script.run.withSuccessHandler(filter_data).draw_linechart();             
      }       

      function filter_data(f_sales_div_data){
        let selection = document.getElementById("sales_div").value;
    let filter_sales_div = f_sales_div_data;        

        //Filter for regions
        if(selection==""){
          filter_selection  = filter_sales_div.slice(0);
        }else{          
          filter_selection = filter_sales_div.filter(f_sales_div_data => f_sales_div_data.salesDiv === selection)       ;          
        }     
      }
</script>
<select id="sales_div">
    <option value="National">National</option>
    <option value="New City">New City</option>
    <option value="old city">old city</option>
     </select>
     <button id="srch_btn" onclick="filtering()"> Search </button> 
     <div id="line_cont" class="d_c"></div>

I am trying to acheive to draw a chart on the page load with values “National” in the Data, on the dropdown select and button click filter chart based on the Dropdown selection.

How to run a function inside a node server after a interval?

I have a node server. Inside the server there is a function for clean up which I want to run very 100ms or so. How should I achieve this outcome?I have thought of making it a cronjob using the

npm-cron 

package to run this function as a background job. Though configuring a cronjob for sub-second intervals seems not possible.

Chart.js: Aligning multiple side-by-side bar charts with inconsistent label widths

I’m using Chart.js (4.4) to create multiple bar charts side by side, each representing a different country. My current implementation uses separate <canvas> elements for each chart. However, I’m encountering an issue where the leftmost chart is smaller than the others due to its y-axis labels taking up more space, and the labels are getting cut off since they are quite long. How can I make the labels show fully and make sure that all of the bars are aligned?

My current attempt

Here is my current implementation:

COUNTRIES.map((country, index) => {
  return (
    <BarChart
      showLabel={index === 0}
      data={{
        labels: labels,   // Prizes and awards
        datasets: [
          {
            label: country,
            data: data[index],
          },
        ]

      }}
      width={100}
      height={300} />
  )
})

with the only notable chart options being indexAxis: 'y'

Why jest.useFakeTimers() is not working for a setTimeout inside a Promise inside a HTTP Request?

I have a test usinging jest thats try to validate an result of a http request to an endpoint (nextjs) which results in a timeout depending on the input parameters like: “/timeout/300”

this is the test
https://github.com/Streeterxs/timeout-server/blob/main/src/app/api/timeout/%5Bms%5D/__tests__/%5Bms%5D.spec.ts#L18

// why this is not working?
it.skip("Should successfully timeout for 300ms", async () => {

  const ms = '300';
  const params: Record<string, string | string[]> = {ms};

  const testApiHandlerPromise = testApiHandler({
    appHandler: timeoutRouteHandler,
    params,
    test: async ({ fetch }) => {

      const responsePromise = fetch({ method: "GET" });

      // https://stackoverflow.com/questions/51126786/jest-fake-timers-with-promises
      jest.runAllTimers();
      const response = await responsePromise;

      const json = await response?.json();

      console.log({json})
      expect(response.status).toBe(200);
      expect(json).toStrictEqual({
        error: null,
        success: `Timeouted ${ms}ms`
      });
      expect(setTimeout).toHaveBeenCalledTimes(1);
    },
  });

  // https://stackoverflow.com/questions/51126786/jest-fake-timers-with-promises
  // dont know if this is needed, but since this promise runs inside the package scope I tried it out
  // probably occurs because of the callback code of test function
  // but I tried to modify the code and it stuck right on the setTimeout function
  jest.runAllTimers();

  await testApiHandlerPromise;
});

yes, Im using a package that encapsulates the test Im trying todo. It justs executes the global fetch function in a way that nextJs understands it (at least it was what I undestood)

Already tried the differents solutions that deals with setTimeout + promises out there but none worked (I think it is because the promise is encapsulated in a http request dont know for sure but it would be cool if anyone answers that too)

¿Qué lenguaje me recomendarían para un programa o pagina que sea local para un contador de archivos de excel de cotizaciones? [closed]

Estoy empezando en esto de la programación pero me piden que haga un programa para llevar mejor el control de archivos de cotizaciones que son en excel quiero hacer un programa para computadora donde se suba el archivo en excel y se guarde en la misma en un bd que lleve nombre, id y fecha para poder filtrar y poder buscar de mejor manera y no permitir que se repitan las cotizaciones de nuevo. Pero quiero saber que me recomiendan para una mejor ejecución.

espero me puedan ayudar y dar su mejor consejo. estaba pensando usar electron.js pero no tengo experiencia en el