Categories
TypeScript Answers

How to convert TypeScript enum to object array?

Sometimes, we want to convert TypeScript enum to object array.

In this article, we’ll look at how to convert TypeScript enum to object array.

How to convert TypeScript enum to object array?

To convert TypeScript enum to object array, we can use the Object.values method.

For instance, we write

enum Colors {
  WHITE = 0,
  BLACK = 1,
  BLUE = 3,
}

const colorValueArray = Object.values(Colors);

to create the Colors enum.

Then we call Object.values with Colors to return an array of enum values.

Conclusion

To convert TypeScript enum to object array, we can use the Object.values method.

Categories
TypeScript Answers

How to disable a TypeScript rule for a specific line?

Sometimes, we want to disable a TypeScript rule for a specific line.

In this article, we’ll look at how to disable a TypeScript rule for a specific line.

How to disable a TypeScript rule for a specific line?

To disable a TypeScript rule for a specific line, we can use the @ts-ignore comment.

For instance, we write

if (false) {
  // @ts-ignore: Unreachable code error
  console.log("hello");
}

to add the // @ts-ignore: Unreachable code error comment so that the TypeScript compiler ignores the unreachable code error in the line after the comment.

Conclusion

To disable a TypeScript rule for a specific line, we can use the @ts-ignore comment.

Categories
JavaScript Answers

How to check image width and height before upload with JavaScript?

Sometimes, we want to check image width and height before upload with JavaScript.

In this article, we’ll look at how to check image width and height before upload with JavaScript.

How to check image width and height before upload with JavaScript?

To check image width and height before upload with JavaScript, we can get the dimensions by assigning the image data string to the src property of an img element.

For instance, we write

const reader = new FileReader();

reader.readAsDataURL(fileUpload.files[0]);
reader.onload = (e) => {
  const image = new Image();
  image.src = e.target.result;
  image.onload = (e) => {
    const height = e.target.height;
    const width = e.target.width;
    if (height > 100 || width > 100) {
      alert("Height and Width must not exceed 100px.");
      return false;
    }
    alert("Uploaded image has valid Height and Width.");
    return true;
  };
};

to create a FileReader object in the file input change event handler.

Then we call readAsDataURL with fileUpload.files[0] to read the fileUpload.files[0] selected file into a base64 string.

Then we create an img element with Image and set the base64 image URL string as the src attribute value with

image.src = e.target.result;

And then we get the width and height from e.target with

const height = e.target.height;
const width = e.target.width;

And then we can check the width and height after that.

Conclusion

To check image width and height before upload with JavaScript, we can get the dimensions by assigning the image data string to the src property of an img element.

Categories
JavaScript Answers

How to split string with newline (‘\n’) in Node.js?

Sometimes, we want to split string with newline (‘\n’) in Node.js.

In this article, we’ll look at how to split string with newline (‘\n’) in Node.js.

How to split string with newline (‘\n’) in Node.js?

To split string with newline (‘\n’) in Node.js, we can use the string split method.

For instance, we write

const arr = "a\nb\r\nc".split(/\r?\n/);

to call "a\nb\r\nc".split with the regex /\r?\n/ to split the string by \r\n or \n into separate strings.

Conclusion

To split string with newline (‘\n’) in Node.js, we can use the string split method.

Categories
JavaScript Answers

How to use Lodash to find and return an object from array with JavaScript?

Sometimes, we want to use Lodash to find and return an object from array with JavaScript.

In this article, we’ll look at how to use Lodash to find and return an object from array with JavaScript.

How to use Lodash to find and return an object from array with JavaScript?

To use Lodash to find and return an object from array with JavaScript, we can use the find function.

For instance, we write

const song = _.find(songs, { id });

to call find with the songs array and we find the object in it with the id property set to id and return it.

Conclusion

To use Lodash to find and return an object from array with JavaScript, we can use the find function.