Categories
JavaScript Answers

How to make TinyMCE paste in plain text by default with JavaScript?

Sometimes, we want to make TinyMCE paste in plain text by default with JavaScript.

In this article, we’ll look at how to make TinyMCE paste in plain text by default with JavaScript.

How to make TinyMCE paste in plain text by default with JavaScript?

To make TinyMCE paste in plain text by default with JavaScript, we can set the paste_as_text option to true.

For instance, we write

tinymce.init({
  plugins: "paste",
  paste_as_text: true,
});

to call tinymce.init with an object with the paste_as_text property set to true to make it paste as text by default.

Conclusion

To make TinyMCE paste in plain text by default with JavaScript, we can set the paste_as_text option to true.

Categories
JavaScript Answers

How to convert a string to byte array with JavaScript?

Sometimes, we want to convert a string to byte array with JavaScript.

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

How to convert a string to byte array with JavaScript?

To convert a string to byte array with JavaScript, we can use the TextEncoder constructor.

For instance, we write

const utf8Encode = new TextEncoder();
const byteArr = utf8Encode.encode("abc");

to create a TextEncoder object.

Then we call utf8Encode.encode with a string to return a byte array converted from the string.

Conclusion

To convert a string to byte array with JavaScript, we can use the TextEncoder constructor.

Categories
TypeScript Answers

How to check if an object implements an interface at runtime with TypeScript?

Sometimes, we want to check if an object implements an interface at runtime with TypeScript.

In this article, we’ll look at how to check if an object implements an interface at runtime with TypeScript.

How to check if an object implements an interface at runtime with TypeScript?

To check if an object implements an interface at runtime with TypeScript, we can create our own function.

For instance, we write

interface Test {
  prop: number;
}

function isTest(arg: any): arg is Test {
  return typeof arg?.prop === "number";
}

to define the isTest function that returns the arg is Test type.

This lets the TypeScript compiler know that it’s a type guard function.

Then we check if arg.prop is a number with typeof in it and return the result.

Conclusion

To check if an object implements an interface at runtime with TypeScript, we can create our own function.

Categories
JavaScript Answers

How to use async/await inside a React functional component with JavaScript?

Sometimes, we want to use async/await inside a React functional component with JavaScript.

In this article, we’ll look at how to use async/await inside a React functional component with JavaScript.

How to use async/await inside a React functional component with JavaScript?

To use async/await inside a React functional component with JavaScript, we can define an async function inside the component.

For instance, we write

function Dashboard() {
  const [token, setToken] = useState("");

  const getToken = async () => {
    const headers = {
      Authorization: authProps.idToken,
    };
    const response = await axios.post(
      "https://foo.bar/get-token",
      {
        //...
      },
      { headers }
    );
    const data = await response.json();
    setToken(data.access_token);
  };

  useEffect(() => {
    if (!token) {
      getToken();
    }
  }, []);

  return <>...</>;
}

to define the getToken async function in the Dashboard component.

In it, we call axios.post to make a post request.

It returns a promise and we get the resolve value of the promise with await.

Then we call getToken in the useEffect callback if token is falsy.

Conclusion

To use async/await inside a React functional component with JavaScript, we can define an async function inside the component.

Categories
JavaScript Answers

How to sort an ES6 Set with JavaScript?

Sometimes, we want to sort an ES6 Set with JavaScript.

In this article, we’ll look at how to sort an ES6 Set with JavaScript.

How to sort an ES6 Set with JavaScript?

To sort an ES6 Set with JavaScript, we can convert it to an array and then sort it.

For instance, we write

const sorted = Array.from(new Set(["b", "a", "c"])).sort();

to convert the set to an array with the Array.from method.

Then we call the sort method on it to return an array with the sorted entries.

Conclusion

To sort an ES6 Set with JavaScript, we can convert it to an array and then sort it.