Categories
TypeScript Answers

How to use Jasmine’s spyOn upon a private method with TypeScript?

Sometimes, we want to use Jasmine’s spyOn upon a private method with TypeScript.

In this article, we’ll look at how to use Jasmine’s spyOn upon a private method with TypeScript.

How to use Jasmine’s spyOn upon a private method with TypeScript?

To use Jasmine’s spyOn upon a private method with TypeScript, we can put the any type as the type parameter for spyOn.

For instance, in our test callback, we write

spyOn<any>(fakePerson, "sayHello");
expect(fakePerson["sayHello"]).toHaveBeenCalled();

to call spyOn with the <any> type parameter to spy on the fakePerson.sayHello method.

Then we call expect with fakePerson["sayHello"] to check that fakePerson.sayHello has been called with toHaveBeenCalled.

Conclusion

To use Jasmine’s spyOn upon a private method with TypeScript, we can put the any type as the type parameter for spyOn.

Categories
JavaScript Answers

How to read line by line of a text area HTML tag with JavaScript?

Sometimes, we want to read line by line of a text area HTML tag with JavaScript.

In this article, we’ll look at how to read line by line of a text area HTML tag with JavaScript.

How to read line by line of a text area HTML tag with JavaScript?

To read line by line of a text area HTML tag with JavaScript, we can use the string split method.

For instance, we write

const textArea = document.getElementById("my-text-area");
const arrayOfLines = textArea.value.split("\n");

to select the text area with getElementById.

Then we get the value of the text area with value.

And then we call split with '\n' to split the text area value into an array of strings by the new line character.

Conclusion

To read line by line of a text area HTML tag with JavaScript, we can use the string split method.

Categories
JavaScript Answers

How to prevent text or element selection with cursor drag with JavaScript?

Sometimes, we want to prevent text or element selection with cursor drag with JavaScript.

In this article, we’ll look at how to prevent text or element selection with cursor drag with JavaScript.

How to prevent text or element selection with cursor drag with JavaScript?

To prevent text or element selection with cursor drag with JavaScript, we can use the window.getSelection().removeAllRanges(); method in the selectstart event handler.

For instance, we write

document.onselectstart = () => {
  window.getSelection().removeAllRanges();
};

to set document.onselectstart to a function that calls window.getSelection().removeAllRanges(); to remove all selection in the document when we start making selections.

This will prevent anything from being selected on the page.

Conclusion

To prevent text or element selection with cursor drag with JavaScript, we can use the window.getSelection().removeAllRanges(); method in the selectstart event handler.

Categories
JavaScript Answers

How to count the number of lines of a string in JavaScript?

Sometimes, we want to count the number of lines of a string in JavaScript.

In this article, we’ll look at how to count the number of lines of a string in JavaScript.

How to count the number of lines of a string in JavaScript?

To count the number of lines of a string in JavaScript, we can use the string split method.

For instance, we write

const lines = str.split(/\r\n|\r|\n/);

to call str.split with a regex that matches all line breaks in the str string to split the string by the new line characters.

It returns an array of split substrings so we can use the length property of the lines array to get the number of lines in str.

Conclusion

To count the number of lines of a string in JavaScript, we can use the string split method.

Categories
JavaScript Answers

How to use import fs from ‘fs’ with JavaScript?

Sometimes, we want to use import fs from ‘fs’ with JavaScript.

In this article, we’ll look at how to use import fs from ‘fs’ with JavaScript.

How to use import fs from ‘fs’ with JavaScript?

To use import fs from ‘fs’ with JavaScript, we’ve to run our Node.js app with Node.js 10 or later.

And the --experimental-modules flag has to be enabled.

Once the requirements are met, then we write

import { readFileSync } from "fs";

to import the readFileSync function from the fs ES module.

Conclusion

To use import fs from ‘fs’ with JavaScript, we’ve to run our Node.js app with Node.js 10 or later.

And the --experimental-modules flag has to be enabled.