Categories
JavaScript Answers

How to add the equivalent of Ruby Array.any? in JavaScript?

Sometimes, we want to add the equivalent of Ruby Array.any? in JavaScript.

In this article, we’ll look at how to add the equivalent of Ruby Array.any? in JavaScript.

How to add the equivalent of Ruby Array.any? in JavaScript?

To add the equivalent of Ruby Array.any? in JavaScript, we can use the array some method.

For instance, we write

const isBiggerThan10 = (element, index, array) => {
  return element > 10;
};

const hassBiggerThan10 = [2, 5, 8, 1, 4].some(isBiggerThan10);

to call [2, 5, 8, 1, 4].some with the isBiggerThan10 function to check if there’re any elements in [2, 5, 8, 1, 4] that’s bigger than 10 with isBiggerThan10.

It returns false since there’re no elements in [2, 5, 8, 1, 4] bigger than 10.

If there’re any elements bigger than 10, it’ll return true.

Conclusion

To add the equivalent of Ruby Array.any? in JavaScript, we can use the array some method.

Categories
JavaScript Answers

How to use module.exports as a constructor with Node.js?

Sometimes, we want to use module.exports as a constructor with Node.js.

In this article, we’ll look at how to use module.exports as a constructor with Node.js.

How to use module.exports as a constructor with Node.js?

To use module.exports as a constructor with Node.js, we export the constructor as a default export.

For instance, we write

class Square {
  constructor(width) {
    this.width = width;
  }
  area() {
    return this.width ** 2;
  }
}

export default Square;

to export the Square class as a default export with export default.

Then we import it by writing

import Square from "./square";

in another module in the same folder.

Conclusion

To use module.exports as a constructor with Node.js, we export the constructor as a default export.

Categories
JavaScript Answers

How to mock console when it is used by a third-party-library with Jest and JavaScript?

Sometimes, we want to mock console when it is used by a third-party-library with Jest and JavaScript.

In this article, we’ll look at how to mock console when it is used by a third-party-library with Jest and JavaScript.

How to mock console when it is used by a third-party-library with Jest and JavaScript?

To mock console when it is used by a third-party-library with Jest and JavaScript, we call the jest.spyOn method.

For instance, we write

const consoleWarnMock = jest.spyOn(console, 'warn').mockImplementation();

to mock the console.warn method with the spyOn method in our test.

And then we call mockImplementation to return a mocked version of console.warn.

When we’re done with the test, then we call

consoleWarnMock.mockRestore();

to restore the original console.warn method.

Conclusion

To mock console when it is used by a third-party-library with Jest and JavaScript, we call the jest.spyOn method.

Categories
JavaScript Answers

How to check if an element is a child of a parent with JavaScript?

Sometimes, we want to check if an element is a child of a parent with JavaScript.

In this article, we’ll look at how to check if an element is a child of a parent with JavaScript.

How to check if an element is a child of a parent with JavaScript?

To check if an element is a child of a parent with JavaScript, we can use the parent element’s contains method.

For instance, we write

const contains = (parent, child) => {
  return parent !== child && parent.contains(child);
};

to create the contains function that checks if parent isn’t child and that the parent element contains the child element with parent.contains.

Conclusion

To check if an element is a child of a parent with JavaScript, we can use the parent element’s contains method.

Categories
JavaScript Answers

How to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript?

Sometimes, we want to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript.

In this article, we’ll look at how to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript.

How to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript?

To fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript, we set the location of the text when we call fillText.

For instance, we write

const c = document.getElementById("c").getContext("2d");
c.font = "11px Courier";
console.log(c);
const txt = "line 1\nline 2\nthird line..";
const x = 30;
const y = 30;
const lineHeight = 15;
const lines = txt.split("\n");

for (const line of lines) {
  c.fillText(line, x, y + i * lineHeight);
}

to loop through the lines with a for-of loop.

In it, we call the context’s fillText method with the line to render.

Then y coordinates of the text is set to y + i * lineHeight which positions the text below the previous line.

Conclusion

To fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript, we set the location of the text when we call fillText.