Categories
JavaScript Answers

How to generate random SHA1 hash to use as ID in Node.js and JavaScript?

Sometimes, we want to generate random SHA1 hash to use as ID in Node.js and JavaScript.

In this article, we’ll look at how to generate random SHA1 hash to use as ID in Node.js and JavaScript.

How to generate random SHA1 hash to use as ID in Node.js and JavaScript?

To generate random SHA1 hash to use as ID in Node.js and JavaScript, we can use the cryto.randomBytes method.

For instance, we write

const id = crypto.randomBytes(20).toString("hex");

to call crypto.randomBytes to create a 20 bytes buffer.

Then we call toString with 'hex' on the bytes buffer to convert it to a hex string.

Conclusion

To generate random SHA1 hash to use as ID in Node.js and JavaScript, we can use the cryto.randomBytes method.

Categories
JavaScript Answers

How to add JavaScript callback that runs when an iframe is finished loading?

Sometimes, we want to add JavaScript callback that runs when an iframe is finished loading.

In this article, we’ll look at how to add JavaScript callback that runs when an iframe is finished loading.

How to add JavaScript callback that runs when an iframe is finished loading?

To add JavaScript callback that runs when an iframe is finished loading, we can listen for the readystatechange event.

To do this, we write

const iframe = document.getElementById("myIframe");

iframe.onreadystatechange = () => {
  if (iframe.readyState === "complete") {
    // ...
  }
};

to select the iframe with getElementById.

Then we set iframe.onreadystatechange to a function that runs when the readyState property changes.

If readyState for the iframe is 'complete', then the iframe‘s content is done loading.

Conclusion

To add JavaScript callback that runs when an iframe is finished loading, we can listen for the readystatechange event.

Categories
JavaScript Answers

How to do fuzzy search with JavaScript?

Sometimes, we want to do fuzzy search with JavaScript.

In this article, we’ll look at how to do fuzzy search with JavaScript.

How to do fuzzy search with JavaScript?

To do fuzzy search with JavaScript, we use the fuzzaldrin library.

To install it, we run

npm install fuzzaldrin

Then we use it by writing

const fuzzaldrin = require("fuzzaldrin");
const matches = fuzzaldrin.filter(["international", "splint", "tinder"], "int");

to call fuzzaldrin.filter with an array of possible matches and the search string we’re searching with.

Then filter returns an array of fuzzy search matches for searching for matches for 'int' in ["international", "splint", "tinder"].

Conclusion

To do fuzzy search with JavaScript, we use the fuzzaldrin library.

Categories
JavaScript Answers

How to detect text area onchange event with JavaScript?

Sometimes, we want to detect text area onchange event with JavaScript.

In this article, we’ll look at how to detect text area onchange event with JavaScript.

How to detect text area onchange event with JavaScript?

To detect text area onchange event with JavaScript, we listen for the input event.

For instance, we write

const area = container.querySelector("textarea");
area.addEventListener(
  "input",
  () => {
    // ...
  },
  false
);

to select the textarea element with querySelector.

Then we call addEventListener on the element with 'input' and a callback that runs when we change the textarea input value.

We call addEventListener to listen for the input event on the textarea.

Conclusion

To detect text area onchange event with JavaScript, we listen for the input event.

Categories
React Answers

How to pass React component as props?

Sometimes, we want to pass React component as props.

In this article, we’ll look at how to pass React component as props.

How to pass React component as props?

To pass React component as props, we can put the components between the parent component’s tags and then then access them in the parent component with the children prop.

For instance, we write

const Label = (props) => <span>{props.children}</span>;
const Tab = (props) => <div>{props.children}</div>;
const Page = () => (
  <Tab>
    <Label>Foo</Label>
  </Tab>
);

to add the Label and Tab components that take the children prop.

Then we define the Page component that renders the Label component in the Tab component.

As a result, a div with a span inside it with the Foo text should be rendered since the children component are replaced by the component between the opening and closing tags.

Conclusion

To pass React component as props, we can put the components between the parent component’s tags and then then access them in the parent component with the children prop.