Categories
JavaScript Answers

How to detect shift+enter and generate a new line in text area with JavaScript?

Sometimes, we want to detect shift+enter and generate a new line in text area with JavaScript.

In this article, we’ll look at how to detect shift+enter and generate a new line in text area with JavaScript.

How to detect shift+enter and generate a new line in text area with JavaScript?

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object.

For instance, we write

textArea.onKeyPress = (e) => {
  if (e.keyCode === 13 && e.shiftKey) {
    e.preventDefault();
    let start = e.target.selectionStart,
      end = e.target.selectionEnd;
    e.target.vaiue =
      prevState.message.substring(0, start) +
      "\n" +
      prevState.message.substring(end);
  } else if (e.keyCode === 13) {
    e.preventDefault();
  }
};

to check keyCode and shiftKey to see if shift+enter is pressed.

If it is, we call preventDefault to prevent the default behavior.

We then get the cursor’s start and end indexes with selectionStart and selectionEnd.

Then we put the new line in between the start and end with

prevState.message.substring(0, start) + "\n" + prevState.message.substring(end);

Conclusion

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object.

Categories
JavaScript Answers

How to use lodash to compare jagged arrays with JavaScript?

Sometimes, we want to use lodash to compare jagged arrays with JavaScript.

In this article, we’ll look at how to use lodash to compare jagged arrays with JavaScript.

How to use lodash to compare jagged arrays with JavaScript?

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

For instance, we write

const array1 = [
  ["a", "b"],
  ["b", "c"],
];
const array2 = [
  ["b", "c"],
  ["a", "b"],
];
_.isEqual(array1.sort(), array2.sort());

to call sort to sort both arrays.

And then we call Lodash’s isEqual function to compare the contents of the sorted arrays.

Conclusion

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

Categories
TypeScript Answers

How to mock dependency in Jest with TypeScript?

Sometimes, we want to mock dependency in Jest with TypeScript.

In this article, we’ll look at how to mock dependency in Jest with TypeScript.

How to mock dependency in Jest with TypeScript?

To mock dependency in Jest with TypeScript, we set the type of the mocked dependency to jest.Mock<typeof dep.default>.

For instance, we write

import * as dep from "../dependency";
jest.mock("../dependency");

const mockedDependency = <jest.Mock<typeof dep.default>>dep.default;

it("should do what I need", () => {
  mockedDependency.mockReturnValueOnce("return");
});

to get the cast the type of dep.default to jest.Mock<typeof dep.default>.

Then we can call mockReturnValueOnce without TypeScript compiler errors.

Conclusion

To mock dependency in Jest with TypeScript, we set the type of the mocked dependency to jest.Mock<typeof dep.default>.

Categories
JavaScript Answers

How to convert a string to JSON object with JavaScript?

Sometimes, we want to convert a string to JSON object with JavaScript.

In this article, we’ll look at how to convert a string to JSON object with JavaScript.

How to convert a string to JSON object with JavaScript?

To convert a string to JSON object with JavaScript, we can use the JSON.parse method.

For instance, we write

const obj = JSON.parse(string);

to call JSON.parse with the JSON string to convert the JSON string to an object.

Conclusion

To convert a string to JSON object with JavaScript, we can use the JSON.parse method.

Categories
React Answers

How to add images in public folder with React?

Sometimes, we want to add images in public folder with React.

In this article, we’ll look at how to add images in public folder with React.

How to add images in public folder with React?

To add images in public folder with React, we get the path to the public folder with process.env.PUBLIC_URL and concatenate the relative image path to it.

For instance, we write

<img src={process.env.PUBLIC_URL + "/img/logo.png"} />;

to add the img element with the src attribute of the image set to process.env.PUBLIC_URL + "/img/logo.png" to load the image from the /img/logo.png file in the public folder.

We use process.env.PUBLIC_URL to get the path to the public folder.

Conclusion

To add images in public folder with React, we get the path to the public folder with process.env.PUBLIC_URL and concatenate the relative image path to it.