How to make husky await for result of promise in pre-commit hook script?

I have a simple pre-commit hook that asks the user for input and then updates the package version number. I need this pre-commit script to complete before husky finishes.

const { exec } = require('child_process');
const inquirer = require('inquirer');
 
const question = {
  type: 'list',
  name: 'version',
  message: 'Semantic Version?',
  choices: ['Patch', 'Minor', 'Major'],
  filter(val) {
    return val.toLowerCase();
  },
};

const selectVersion = async () => {
  const version = await inquirer.prompt(question);
  exec(`npm version ${version}`);
}

selectVersion();
"scripts": {
  "version:select": "node ./scripts/versionSelect.js"
},

I get prompted to input my answer but the commmit hook finishes too quickly so I am unable to interact with my question prompt as you can see here:

enter image description here

How can i put “you are entering a number” right below the input box when i select input box?

        .Input{
        background-color:grey;
        }
        .Input:focus {
        background-color: white;
        }
    

Enter the first number:

Enter the second number:

Enter the third number:

Enter the fourth number:

Enter the fifth number:

Submit

function get_product_of_inputs() {
    var a = document.getElementById("a").value;
    var b = document.getElementById("b").value;
    var c = document.getElementById("c").value;
    var d = document.getElementById("d").value;
    var e = document.getElementById("e").value;
    var f = a*b*c*d*e;
    console.log(f);
    document.getElementById("Submit").innerHTML= ("Product of all the numbers are "+f);
}

I am literally trying to put “you are entering a number” when someone select the input field. what can i do?
Please help me out. I am a bit confused at this point?

How to use fillText two times in canvas?

<!DOCTYPE html>
<html lang="fi">
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8"/>
<title>Rolling and rotating text</title>
</head>
<script>
var canvas = undefined;
var ctx = undefined;
var angle = -270;
var start = 0;
var i = 0;
 
const SCROLLTEXT = "  B       C       D       A  ";


 
window.onload = function() {
    canvas = document.getElementById("canvas");
    canvas.setAttribute("width",document.documentElement.clientWidth);
    canvas.setAttribute("height",document.documentElement.clientHeight);
 
    ctx = canvas.getContext("2d");
    ctx.font = "48px courier";
 
    rotate_text();
}
 
 
function rotate_text() {
 
 
    ctx.clearRect(0,0,document.documentElement.clientWidth,document.documentElement.clientHeight);
 
    i = 0;
    
    
 
    for (let angle2 = -270 - angle; angle2 < -270 + 360 - angle; angle2+=10) {
 
        letter = SCROLLTEXT.substring(start + i, start + i + 1);
 
        ctx.save();
        ctx.translate(Math.cos((angle2) * (Math.PI / 180)) * 250, Math.sin(( angle2) * (Math.PI / 180)) * 250);
        ctx.translate(canvas.width / 2, canvas.height / 2);
        ctx.rotate((angle2) * (Math.PI / 180));
        ctx.translate(-canvas.width / 2, -canvas.height / 2);
        
        ctx.fillStyle = "#1122FF";
 
        
         
 
        ctx.fillText(letter, canvas.width / 2  , canvas.height / 2);
        
        ctx.restore();  
         
        i++;
        
 
    }
 
 
    if (angle % 10 == 0) {
        start+=1;
        angle=0;
    } 
    angle += 0.5;
     
 
    if (start == SCROLLTEXT.length - 36) {
        start = 0;
    }
     
 
    window.requestAnimationFrame(rotate_text);
 
}
 
 
</script>
<body>
    
<canvas id="canvas"></canvas>

</body>
</html>

**I tried adding alphabet “A” inside this rotating alphabets But it didn’t work for me and I don’t know how to use fillText two times, So I am expecting to add alphabets inside this rotating alphabets **

**I tried adding alphabet “A” inside this rotating alphabets But it didn’t work for me and I don’t know how to use fillText two times, So I am expecting to add alphabets inside this rotating alphabets **

Redirect using navigate() to the same URL updating the state in react

‘/’URL Login handler function

const loginHandler = (e) => {
        e.preventDefault();
        Services.login(email, password)
          .then((Res) => {
            if(Res.data==="success"){
              navigate('/home')
            }
            else {
              navigate('/',{
                state: {
                  msg: Res.data,
                }})
            }
          })
          .catch((error) => {
            console.log(error);
          });
      };

On Res.data other than success I want to redirect to the same /Url by updating the state of a component so that I can display proper error message on the same page and prevent user to navigate to /home Url . How can I do that?

How can I get the number of items in a filtered array in JavaScript

I was trying to divide the number of items in a filtered array but it kept showing me undefined, NAN, or the number of letters in that particular filter when I send it to the console. But what I want to get is the number of items in the filter.

Here is the code I wrote:
for (let i = 0; i < categories.length; i++) {

categories[i].addEventListener('click', (e) => {

    const filterF = e.target.dataset.category;

    dogProducts.forEach((product) => {
        if (product.classList.contains(filterF)) {
            product.style.display = 'block';
            ulTag.style.display = 'none';

            console.log(products);

        } else {
            product.style.display = 'none';
            ulTag.style.display = 'none';
        }

    })
})

}

How to persist an object that is passed by reference in React?

I’m working on this task where I need to do the following:
I have this object passed by reference from 4 components

const InitialStateSubjects = {
"1":{
"name": "math",
"selected": true}
"2":{
"name": "English",
"selected": true} 
}

const Subjects = {
"1":{
"name": "math",
"selected": false}
"2":{
"name": "English",
"selected": true} 
}

const [initialState, setInitialState] = useState(subjectsObj)
const [changedState, setChangedState] = useState(subjectsObj)

//this is the way I'm filtering but it's not working because it's comparing two identical objects

const changedSubjects = Object.keys(changedState)
.filter(key => initialState[key]?.selected !== changedState[key]?.selected
.map(key => console.log(initialState[key]?.name))

As you can see they both need to have the same object passed which is subjectObj
Most of the logic is done with the changedState, and I only set the initial one once in the useEffect, but regardless, both the initialState & changedState gets changed at the same time. I want to use the initial value of Initial state to compare the difference and return the name of the subjects that selected values changed. I have tried doing in this in the console and it works brilliantly but when doing in React I am stuck with this. I have tried spread operator to make a shallow copy of it, and used a library to make a deep clone of it but it still mutates the initial object. If you do think of a different way to compare initial state without creating another state that would also work for me because all I want is to return the ones that changed upon clicking a button from their original initial state. Thanks so much in advance!

Trying to make array with certain length

I’m trying to create an array from two other arrays using a loop, where in both arrays I compare two strings and when I find two strings that are the same I push the value to the array from second compared array, and if I don’t find it I push 0. The problem is that the array has to be exactly the length of the first array.
const array1 = ['21', '22', '23', '24', '25', '26', '27'] const array2 = [{score: 1, date: '21'}, {score: 3, date: '23'} ]
output should look like this: const array 3 = [1, 0, 3, 0, 0, 0, 0] <=== this array must have length of array1, and i have problem to create proper loop, or maybe this is shouldnt be loop? Please help.

React JS inline styles working on only one component instance

I have a custom react component called Subtitle.

import React from "react";
import "./Subtitle.css";
const Subtitle = ({ theme, textGroup }) => {
  return (
    <div>
      <div className="Subtitle__heading">
        <div className="p__heading">
          <p>
            <span style={{ color: theme.lifeJourney }}>{textGroup.text1}</span>{" "}
            {textGroup.text2}
          </p>
        </div>
      </div>
    </div>
  );
};

export default Subtitle;

The component takes in a theme prop. The theme is an object that specifies what colors the component should render.
I pass in the inline styles fairly comfortably and call the component several times on the page inside other components. Problem Is the inline styles only work on the first call. The second time I use the component. It runs but it contains no styling.

I don’t get it. what I am doing wrong here? Please help.

Show Options only when match found

I am trying to make a search box which shows suggestions as soon as user starts typing in the search bar. Using select2 it shows suggestions by default even when user does not type anything in the search box. Since I have large no. of options I don’t want them to display all by default. It should be displayed only when match with the user typing in the search box.

<html>
  <head>
    <title>Using Select2</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- Select2 CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
  </head>
  <body>
    <div class="jumbotron">
      <div class="container bg-danger">
        <div class="col-md-6">
          <label>Multiple Select2</label>
          <select id="multiple" class="js-states form-control" multiple>
            <option>Java</option>
            <option>Javascript</option>
            <option>PHP</option>
            <option>Visual Basic</option>
          </select>
        </div>
      </div>
    </div>
    <!-- jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- Select2 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
    <script>
      $("#multiple").select2({
          placeholder: "Select a programming language",
          allowClear: true
      });
    </script>
  </body>
</html>

Custom IsAny and IsUnknown generic based on type assignability(compatibility) table

I tried to create IsAny generic based on this.

And my IsAny generic seems to work fine.
But when I use it in another generic(IsUnknown) it is broken:

const testIsUnknown2: IsUnknown<any> = true; // problem here, must be false

But when I change IsAny generic to commented one, – it works fine again. So why is it happening? Because I do not see any difference in both IsAny generics, but it seems my generic is not working inside other generics.

// type IsAny<T> = 0 extends (1 & T) ? true : false;

type IsAny<T> = unknown extends T ? (T extends object ? true : false) : false;
const testIsAny1: IsAny<any> = true;
const testIsAny2: IsAny<unknown> = false;
const testIsAny3: IsAny<string> = false;
const testIsAny4: IsAny<object> = false;

// unknown is only assignable to two types: unknown and any
type IsUnknown<T> = unknown extends T ? (IsAny<T> extends true ? false : true) : false;
const testIsUnknown1: IsUnknown<unknown> = true;
const testIsUnknown2: IsUnknown<any> = true; // problem here, must be false
const testIsUnknown3: IsUnknown<object> = false;
const testIsUnknown4: IsUnknown<number> = false;

Material UI portal component vanishes after page refresh in next.js

I use Next.js and Material UI. I need to render an element in another place, because of the Swiper library that I use.

For this purpose, I’m using Material UI’s <Portal> component and here’s my code:

import { useRef } from 'react'
import Portal from '@mui/material/Portal';

//more code here

const container = useRef(null);

//more code here

<div ref={container}></div>

//more code here

<Swiper
    spaceBetween={20}
    slidesPerView={1}
>
    <Portal container={container.current}>
        <SwiperButton></SwiperButton>
    </Portal>
    {
        ads.map(ad => <SwiperSlide key={ad.imageUrl}>
            <div>{ad.title}</div>
        </SwiperSlide>
        )}
</Swiper>

This code works just fine for the first load of my web page. But as soon as I refresh the web page, it vanishes and disappears completely and there is no error whatsoever in the console.

And no matter what I do, it won’t come back. Ctrl+F5 does not work and stopping and destroying and recreating my container does not work.

The only way to make it work is to comment that part of code, load the page and uncomment it again.

Why is it so, and how can I fix this problem?

Delete an element from an array (RecoilJS)

I have a form where I put a float value (1.1, 1.2, 1.9 and so on) and I want to store a bunch of them inside an array on an atom:

import { atom } from 'recoil';

export const valueState = atom({
    key: 'values',
    default: []
});

Whenever I write a value and it’s checked that it’s a double, the value gets added to valueState, however, I want to make it so if that the value I write on the form gets deleted, it also deletes the value from the valueState array. I tried by using pop, however, if I do so the program crashes. How can I do it then?

import { valueState as valueStateAtom } from '../../atoms/Atoms';
import { useSetRecoilState } from 'recoil';
const setValue = useSetRecoilState(valueStateAtom);


// The function that handles the onChange event of the form
const setNewValue = (v) => {
   if (v !== '') {
      const valueNumber = parseFloat(v);
      if (!isNaN(valueNumber)) {
         setPageValueChanged(true);
         pageValue = valueNumber;
         // The value gets added to valueState
         setValue((prev) => prev.concat({ valueNumber, cardID }));
      } else {
          setPageValueChanged(false);
      }
   } else {
       setPageValueChanged(false);
       // Delete v from the atom array here
    }
};