Categories
JavaScript Answers

How to call getElementById based on a partial string with JavaScript?

Sometimes, we want to call getElementById based on a partial string with JavaScript.

In this article, we’ll look at how to call getElementById based on a partial string with JavaScript.

How to call getElementById based on a partial string with JavaScript?

To call getElementById based on a partial string with JavaScript, we call querySelector instead of getElementById.

For instance, we write

const id = document.querySelector('[id^="poll-"]').id;

to call querySelector with '[id^="poll-"]' to match the first element where its ID starts with poll-.

Conclusion

To call getElementById based on a partial string with JavaScript, we call querySelector instead of getElementById.

Categories
TypeScript Answers

How to extend two classes with TypeScript?

Sometimes, we want to extend two classes with TypeScript.

In this article, we’ll look at how to extend two classes with TypeScript.

How to extend two classes with TypeScript?

To extend two classes with TypeScript, we merge all the properties in each class’ prototype into the class that we want to inherit from the two classes.

For instance, we write

class CanEat {
  public eat() {
    alert("eat.");
  }
}

class CanSleep {
  sleep() {
    alert("sleep.");
  }
}

const applyMixins = (derivedCtor: any, baseCtors: any[]) => {
  baseCtors.forEach((baseCtor) => {
    Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
      if (name !== "constructor") {
        derivedCtor.prototype[name] = baseCtor.prototype[name];
      }
    });
  });
};

class Being implements CanEat, CanSleep {
  eat: () => void;
  sleep: () => void;
}
applyMixins(Being, [CanEat, CanSleep]);

to define the applyMixins function.

In it, we loop through each class in baseCtors with forEach.

In the forEach callback, we get the property names of each class’ prototype with Object.getOwnPropertyNames.

And then we loop through each property name string with forEach.

If its name isn’t 'constructor'.

Then we assign baseCtor.prototype[name] to derivedCtor.prototype[name].

Next, we create the Being class that implements the CanEat and CanSleep classes.

And we call applyMixins to incorporate the property properties from the CanEat and CanSleep classes into the Being class’ prototype.

Conclusion

To extend two classes with TypeScript, we merge all the properties in each class’ prototype into the class that we want to inherit from the two classes.

Categories
React Answers

How to import image (.svg, .png) in a React component?

Sometimes, we want to import image (.svg, .png) in a React component.

In this article, we’ll look at how to import image (.svg, .png) in a React component.

How to import image (.svg, .png) in a React component?

To import image (.svg, .png) in a React component, we can import the image directly.

For instance, we write

import React from "react";
import image from "./img1.png";
import "./helloWorld.scss";

const HelloWorld = () => (
  <>
    <img src={image} alt="some image" />
  </>
);
export default HelloWorld;

to add the img element into the HelloWorld component.

We set its src prop to the image that we import with the import statement.

We can import it with import since the Webpack file loader plugin is included with Create React app projects.

Conclusion

To import image (.svg, .png) in a React component, we can import the image directly.

Categories
JavaScript Answers

How to test that a value is greater than or equal to in Jasmine and JavaScript?

Sometimes, we want to test that a value is greater than or equal to in Jasmine and JavaScript.

In this article, we’ll look at how to test that a value is greater than or equal to in Jasmine and JavaScript.

How to test that a value is greater than or equal to in Jasmine and JavaScript?

To test that a value is greater than or equal to in Jasmine and JavaScript, we can use an expression that has the comparison operator.

For instance, we write

describe("percent", function () {
  it("should be a decimal", () => {
    const percent = insights.percent;

    expect(percent >= 0).toBeTruthy();
    expect(percent).toBeLessThan(1);
  });
});

to check that percent >= 0 is truthy to check that percent is bigger than or equal to 0.

Conclusion

To test that a value is greater than or equal to in Jasmine and JavaScript, we can use an expression that has the comparison operator.

Categories
JavaScript Answers

How to stub a class method with Sinon.js and JavaScript?

Sometimes, we want to stub a class method with Sinon.js and JavaScript.

In this article, we’ll look at how to stub a class method with Sinon.js and JavaScript.

How to stub a class method with Sinon.js and JavaScript?

To stub a class method with Sinon.js and JavaScript, we can call the sinon.stub method.

For instance, we write

sinon.stub(YourClass.prototype, "myMethod").callsFake(() => {
  return {};
});

to stub the myMethod instance method in the YourClass class.

We call callsFake with a callback that returns the value we want for the fake method.

Likewise, we stub a static method with

sinon.stub(YourClass, "myStaticMethod").callsFake(() => {
  return {};
});

We call stub to stub the YouClass.myStaticMethod static method.

Conclusion

To stub a class method with Sinon.js and JavaScript, we can call the sinon.stub method.