Categories
JavaScript Answers

How to include a hyphen in a regex character bracket with JavaScript?

Sometimes, we want to include a hyphen in a regex character bracket with JavaScript.

In this article, we’ll look at how to include a hyphen in a regex character bracket with JavaScript.

How to include a hyphen in a regex character bracket with JavaScript?

To include a hyphen in a regex character bracket with JavaScript, we can use \-.

For instance, we write

/^[a-zA-Z0-9.\-_]+$/

to add hyphen into the list of characters to match in the square brackets.

Conclusion

To include a hyphen in a regex character bracket with JavaScript, we can use \-.

Categories
JavaScript Answers

How to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript?

Sometimes, we want to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript.

In this article, we’ll look at how to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript.

How to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript?

To use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript, we can use spies.

For instance, we write

class Klass {
  method(arg) {
    return arg;
  }
}

describe("spy behavior", () => {
  it("should spy on an instance method of a Klass", () => {
    const obj = new Klass();
    spyOn(obj, "method");
    obj.method("foo argument");
    expect(obj.method).toHaveBeenCalledWith("foo argument");
    expect(
      obj.method.calls.mostRecent().args[0] instanceof String
    ).toBeTruthy();
  });
});

to create the Klass class to test.

Then we create a new Klass instance in the test.

We then spy on the method method with spyOn(obj, "method");.

Next, we call the method with obj.method("foo argument");.

Then we check that obj.method is called with 'foo argument' with expect(obj.method).toHaveBeenCalledWith("foo argument");.

And then we check that it’s most recently called with a string with

expect(obj.method.calls.mostRecent().args[0] instanceof String).toBeTruthy();

Conclusion

To use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript, we can use spies.

Categories
JavaScript Answers

How to close all info windows in Google Maps API v3 and JavaScript?

Sometimes, we want to close all info windows in Google Maps API v3 and JavaScript.

In this article, we’ll look at how to close all info windows in Google Maps API v3 and JavaScript.

How to close all info windows in Google Maps API v3 and JavaScript?

To close all info windows in Google Maps API v3 and JavaScript, we can call the close method.

For instance, we write

const latlng = new google.maps.LatLng(-34.397, 150.644);
let infoWindow = null;

//..

google.maps.event.addListener(marker, "click", () => {
  infoWindow?.close();
  infoWindow = new google.maps.InfoWindow();
  //...
});

to call infoWindow?.close() to close the info window when we click on a marker.

Then we create a new info window with

infoWindow = new google.maps.InfoWindow();

Conclusion

To close all info windows in Google Maps API v3 and JavaScript, we can call the close method.

Categories
JavaScript Answers

How to read Excel file using Node.js and JavaScript?

Sometimes, we want to read Excel file using Node.js and JavaScript

In this article, we’ll look at how to read Excel file using Node.js and JavaScript.

How to read Excel file using Node.js and JavaScript?

To read Excel file using Node.js and JavaScript, we use the node-xlsx package.

To install it, we run

npm install node-xlsx --save

Then we use it by writing

const xlsx = require("node-xlsx");

const obj = xlsx.parse(__dirname + "/myFile.xlsx");

to parse a file at the path with xlsx.parse.

We can parse a buffer with

const obj = xlsx.parse(fs.readFileSync(__dirname + "/myFile.xlsx"));

We read the file with readFileSync and then parse the returned buffer.

Conclusion

To read Excel file using Node.js and JavaScript, we use the node-xlsx package.

Categories
JavaScript Answers

How to make synchronous requests in Node.js and JavaScript?

Sometimes, we want to make synchronous requests in Node.js and JavaScript.

In this article, we’ll look at how to make synchronous requests in Node.js and JavaScript.

How to make synchronous requests in Node.js and JavaScript?

To make synchronous requests in Node.js and JavaScript, we can use the retus library.

To install it, we run

npm i retus

Then we use it by writing

const retus = require("retus");

const { body } = retus("https://example.com");

to make a get request to https://example.com synchronously.

body has the response body.

Conclusion

To make synchronous requests in Node.js and JavaScript, we can use the retus library.