Merge hooks in QUnit

I need to reuse sets of setup and cleanup logic in multiple QUnit modules without code duplication. These pieces of logic are already encoded as “module hook” objects as per the QUnit API. Some of the hooks might be async. Here is an example – The content of these hooks doesn’t really mean anything, it’s just for demonstration.

const fruitHook = {
  async before () {
    this.banana = { ripeness: await generateRandomInitialRipeness() };
    this.apple = { ripeness: await generateRandomInitialRipeness() };
  },

  beforeEach () {
    this.banana.ripeness += 1;
    this.apple.ripeness += 1;
  }
};

const nutHook = {
  before () {
    this.walnut = { cracked: false };
    this.peanut = { cracked: false };
  }
};

I would like to combine these hooks in different ways as needed by my QUnit modules.

// This module needs the fruitHook
QUnit.module('TestFruits', fruitHook);

// This module needs the nutHook
QUnit.module('TestNuts', nutHook);

// This module needs both
QUnit.module('TestFruitsAndNuts', /* ??? */);

In a real use case, I would have a larger set of hooks that need to be combined into various different subsets. Does QUnit offer a way to combine these hooks?

I can write my own “hooks concatenator” utility:

function concatenateHooks (hooks) {
  const resultingHook = {
    async before () {
      for (const hook of hooks) {
        if (typeof hook.before === 'function') {
          hook.before && await hook.before.call(this);
        }
      }
    },

    async beforeEach () { /* ... */ }

    async afterEach () { /* ... */ }

    async after () { /* ... */ }
  }
}

And use it like so:

QUnit.module('TestFruitsAndNuts', concatenateHooks([fruitHook, nutHook]);

In my implementation I’m forcing all hooks to become async, though I suspect I could make it a bit more elaborate to avoid awaiting if all passed hooks are synchronous.

Is there a feature in QUnit that does this and that I’ve missed?

My javascript code didn’t show alert dialog within a function but when i just call it, it will appear

I made a form for registration and use JavaScript for checking condition, but when I call that function, the alert message didn’t show for all but I call it alone, the dialog appeared.

This is my code for checking the condition of each field.

function validateForm() {
  var firstName = document.getElementById("firstName").value;
  var lastName = document.getElementById("lastName").value;
  var email = document.getElementById("inputEmail4").value;
  var password = document.getElementById("inputPassword4").value;

  if (firstName === "" || lastName === "" || email === "" || password === "") {
    alert("Please fill out all fields");
    return false;
  }

  if ((firstName.length < 2 || firstName.length > 30) && firstName !== "") {
    alert("First Name must be between 2 and 30 characters");
    return false;
  }

  if (lastName.length < 2 || lastName.length > 30) {
    alert("Last Name must be between 2 and 30 characters");
    return false;
  }

  const emailPattern = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
  if (!email.match(emailPattern)) {
    alert("Invalid email address");
    return false;
  }

  if (password.length < 2 || password.length > 30) {
    alert("Password must be between 2 and 30 characters");
    return false;
  }

  alert("Complete!");

}
<form class="row g-3" id="registrationForm" onsubmit="return validateForm();">
  <div class="col-md-6">
    <label for="firstName" class="form-label">First name</label>
    <input type="text" class="form-control" id="firstName" placeholder="First name">
  </div>
  <div class="col-md-6">
    <label for="lastName" class="form-label">Last name</label>
    <input type="text" class="form-control" id="lastName" placeholder="Last Name">
  </div>
  <div class="col-md-12">
    <label for="inputEmail4" class="form-label">Email</label>
    <input type="email" class="form-control" id="inputEmail4" placeholder="[email protected]">
  </div>
  <div class="col-md-12">
    <label for="inputPassword4" class="form-label">Password</label>
    <input type="password" class="form-control" id="inputPassword4">
  </div>
  <div class="d-grid gap-2 d-md-block">
    <button type="submit" class="btn btn-primary">Submit</button>
    <button type="reset" class="btn btn-warning">Reset</button>
  </div>
</form>

Trying to flip a business card I designed from front to back. But it’s not working [closed]

I’ve created a business card, both front and back. In my editor, they appear one on top of the other (front side and back side). Maybe that’s not the correct way to set this up. But the bigger problem is that when I click on the flip icon, nothing happens. Not sure where I’ve done wrong. The codepen is here: [https://codepen.io/WordNerdGuy/pen/VYwzPXe]

I’ve used Ask Copilot for suggestions but not much luck. When I click the flip icon in the lower-right corner, I expected the card to flip over and display the information on the back of the business card. But nothing happens when I click the icon. Admittedly, I am very new to javascript but also, the HTML has gotten complicated (for me) and it’s tough to know where to place what tags.

Why does map over Uint8Array behave strangely?

I am trying to get mimeType from the image buffer or ArrayBuffer. But I am facing a strange issue.

I am using the following code to convert the signature bytes to hex string.

const uint8Array = new Uint8Array([137, 80, 78, 71]);
const signatureLength = 4;
const signature1 = uint8Array.slice(0, signatureLength).map(byte => {
  const signature = byte.toString(16).padStart(2, "0");
  console.log("signature --- ", signature, byte, typeof signature);
  return signature;
});
const signature = signature1.join("").toUpperCase();
console.log("signature 1 --- ", signature1, signature);

As you can see in the console output, 78 is actually converted to ‘4e’, but in the map result it is saved as 0. This behaviour seems very strange. What is going on here?

getComputedStyle() does not index dashed properties in Chrome

getComputedStyle() does not list dashed CSS properties in Chrome

:root {
    --test: #000;
}
let cs = window.getComputedStyle(document.documentElement);
// works
console.log(cs.getPropertyValue('--test'));

// doesn't work
for (let i of cs) {
    if (i.indexOf('--') == 0) {
        console.log(i);
    }
}

https://jsfiddle.net/4kLv716o/

I read the docs (https://googlechrome.github.io/samples/css-custom-properties/index.html) and I tried SO suggestions. I want to fix the above snippet for Chrome.

React JS with TypeScript

Is it correct to create and develop React applications with TypeScript in the functional components way?
Or using TypeScript in React, means using class components?
If I want to use functional components and take advantage of some TypeScript features, like type checking, would this be a correct and normal approach?

Can I avoid awaiting an async function if no return is expected or required [duplicate]

I think I understand async/await but I am confused by a part I have reached in my code in a very common scenario.

I have an async function createOrder() that processes orders. Before that function returns any values, it needs to execute another async function to send email confirmation sendEmail().

I don’t want to wait for the email to be sent before returning the order details back to the customer.

So this is how I have gone about it:

async function createOrder(){

try {
  //Create the order object 
  const order = {
        productid: 1234
        name: "helpful text"
        price: "55.00"
  }

  sendEmail(order); // I dont want to await this

  return {
    order // Client needs this ASAP without waiting for email to be sent
  }
}
catch (e) {
  console.error(e);
}

}

The email function sends the mail:

async function sendEmail(payload){

try {
   await axios.post('/emailer/send', {
       payload
   });

}
catch(e){
  console.error(e);
}
}

My question is that my IDE gives me a warning that I have not used await when calling the sendEmail() function. Is it just a suggestion or is there something fundamentally wrong with my approach? If I am wrong, then what is a better way to go about it?

Nestjs CACHE_MANAGER is a provider + import error

So I am trying to use caching in my nestjs project with redis as a store but I am having a lot of issues with implementation errors.

This is my cache module

import { Module } from '@nestjs/common'
import { CacheModule } from '@nestjs/cache-manager'
import { ConfigModule, ConfigService } from '@nestjs/config'
import * as redisStore from 'cache-manager-redis-store'

@Module({
    imports: [
        CacheModule.registerAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => ({
                isGlobal: true,
                store: redisStore,
                host: configService.get<string>('cache.host') || 'localhost',
                port: configService.get<number>('cache.port') || 6379,
                ttl: 60,
            }),
            inject: [ConfigService],
        }),
    ],
})
export class MyCacheModule {}

I am importing this into my app.module.ts simply as MyCacheModule.

Service

import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Inject, Injectable } from '@nestjs/common'
import { Cache } from 'cache-manager'

@Injectable()
export class ItemService {
    constructor(@Inject(CACHE_MANAGER) private readonly _cache: Cache) {}

    async getItem(id: string) {
        const cachedUser = await this._cache.get(`item_${id}`)

        if (cachedUser) {
            return cachedUser
        }

        const item = {
            id: 50,
            name: 'item',
        } /**This would be a database call */

        await this._cache.set(`item_${id}`, item, 60) // Cache for 60 seconds

        return item
    }
}

This is a simple service where I am trying to use cache. However the problem I keep running into is Please make sure that the argument "CACHE_MANAGER" at index [0] is available in the ItemModule context. I have added MyCacheModule into the ItemModule imports and still recieve the same error.

Does anyone have a simple caching in nestjs w/redis store solution?

Am I using the correct memory allocation in Frida?

I want to patch a text-editor like application in runtime in the following way: when a user opens any file, my predefined file (say /tmp/predefined) should be opened instead of the users’s original file. For this task I use the Frida toolkit with java script API. I intercept all open() system calls, decide if I need to make a substitution, and if so, replace the first open() parameter (the path to the file to be opened) with my predefined one.

The script patcher.js:

function main()
{

Interceptor.attach(Module.getExportByName(null, 'open'), {
    onEnter(args) {
        const originalPath = args[0].readCString();
        if (shouldReplace(originalPath)){
            args[0].writeUtf8String('/tmp/predefined'); // (1)
            //args[0] = Memory.allocUtf8String("/tmp/predefined"); (2)
        }
    },
  });
}

function shouldReplace(path) {
if (!path) return false;
// Do not replace essential system or config files:
if (path.startsWith("/usr/") || path.startsWith("/etc/") || path.startsWith("/lib") || 
    path.startsWith("/var/") || path.startsWith("/proc/") || path.startsWith("/sys/") || 
    path.startsWith("/dev/") || path.startsWith("/run/") || path.startsWith("/home/user/.config/")
||  path.startsWith("/home/user/.cache") || path.startsWith("/home/user/.local") ) {
    return false;
}
// Avoid replacing if it's already the predefined file (prevent infinite loop)
if (path === "/tmp/predefined") {
    return false;
}
// Otherwise, assume it's a user-requested file and should be repalced
return true;
}

main()

As a text-editor I use gnome-text-editor
Run frida as: ./frida -l patcher.js -f /usr/bin/gnome-text-editor /tmp/originalFile

This seems to works: /tmp/predefined is opened instead of /tmp/originalFile. But if I want to allocate new memory for string “/tmp/predefined” and assign a pointer to this new memory to args[0], rather than rewriting the contents of the memory pointed to by args[0] (uncomment (2) line, comment out (1) line), I get errors:

  1. The editor does not open /tmp/predefined and writes Could Not Open File You do not have permissions to open the file
  2. In the terminal: (gnome-text-editor:9036): editor-document-WARNING **: 11:11:33.542: Failed to load file: Error opening file /tmp/originalFile: No such file or directory, however /tmp/originalFile exists.

Since these errors only occur when I try to allocate new memory for a string and change the value of the args[0] pointer, I’m wondering if I’m using the memory allocation in Frida’s javascript API correctly?

How to construct a dexie entity class?

class Friend extends Entity<AppDB> {
  id!: number;
  name!: string;
  age!: number;
  constructor(id = 1, name = '2', age = 3){
    super()
    this.id = id
    this.name = name
    this.age = age
  }
}

Now if I try to create an instance of Friend I get error in super:

let f = new Friend() // Uncaught (in promise) TypeError at super()

How should I call the super function?
Note: I am following examples from here.

How can I create a scrolling animation in Elementor?

enter image description here

I want to create a scrolling effect similar to this website: TradeQuick Inc.. When the user scrolls down, the image should move up and eventually disappear (as if display: none is applied). I’ve tried multiple approaches but haven’t been able to achieve this effect.

My site: Hero Animation.

Can you please help me implement this effect?

I’ve experimented with multiple approaches to achieve this effect, including Elementor Pro’s scrolling effects, custom CSS, and JavaScript. Despite my efforts, none of these methods have worked as expected.

I’ve tested different configurations, adjusted various settings, and even tried troubleshooting potential conflicts, but I still haven’t been able to get the desired result.

Handling large file upload in Deno efficiently

A large file is split into chunks in the browser (with file.slice), and each chunk is sent with fetch() to the server, along with some other info, like file name, chunk index, total number of chunks.

This is how my Deno request handler for a chunk looks like:

// <file name, number of chunks uploaded>
const pending = new Map<string, number>();

const chunkDir = path.join(`chunks_${query.name}`);
let numUploaded = pending.get(chunkDir) ?? 0;

const chunkPath = path.join(chunkDir, String(i));
await Deno.writeFile(chunkPath, bytes, { mode: 0o644 });

numUploaded++;
pending.set(chunkDir, numUploaded);

if (numUploaded !== query.numChunks) {
  return { completed: false };
}

// this is the last chunk, combine all chunks together in finalFile
const finalFile = query.name;
const fh = await Deno.create(finalFile);
for (let i = 0; i < query.numChunks; i++) {
  const bytes = await Deno.readFile(path.join(chunkDir, String(i)));
  awai fh.write(bytes);
}

fh.close();
pending.delete(chunkDir);
await Deno.remove(chunkDir, { recursive: true });
return { completed: true };

so it puts each chunk into a file, and when it has all the chunks it joins them into the final file.

This works fine, but I was wondering if I can build the final file directly, without having to write all the chunks and then join them. This would avoid a lot of extra reads and writes on the disk.
The chunks are not guaranteed to come in order, so I cannot just call fh.write(…) on the finalFile as each chunk is received..

Questions about matching rules of slate `Editor.nodes()` function

I feel very confused about the matching rules of Editor.nodes(). This is my demo about slate in react project, and the UI is simple, only a “toggle bold” button can make text bold. The screenshot is as follows:

ui

My logic of Editor.nodes() to match bold block is located in file srctoolsCustomCommand.js, and the snippet is as follows:

isBoldMarkActive(editor) {
  const matchList = Editor.nodes(editor, {
    match: (node) => {
      const retValue = node.bold === true;
      console.log(
        `isBoldMarkActive, node: ${JSON.stringify(node)}, ` +
          `ret value: ${retValue}`
      );
      return retValue;
    },
    universal: true,
  });
  const matchArray = [...matchList];
  console.log(
    `isBoldMarkActive, matchList: ${JSON.stringify(matchArray)}, ` +
      `len: ${matchArray.length}`
  );
  return !!matchArray[0];
}

My first question is why the return value is false, and nothing matched when I select “this is” and click “Toggle Bold” button? as we know the word “is” was bold originally:

return_false

My second question is why the return value is still false, even if one block is matched when I select “is editable” and click “Toggle Bold” button? the word “is” was bold originally:

still_return_false

Thanks for any light you may be able to shed on my confused brain!