Categories
JavaScript Answers

How to fix ESLint not working in VS Code?

Sometimes, we want to fix ESLint not working in VS Code.

In this article, we’ll look at how to fix ESLint not working in VS Code.

How to fix ESLint not working in VS Code?

To fix ESLint not working in VS Code, we add some options to settings.json.

For instance, we add

{
  //...
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "html",
    "typescriptreact"
  ]
  //...
}

in settings.json to enable validation with ESLint in VS Code.

We add this option to make ESLint validate JavaScript, JSX, HTML, and TypeScript React files.

Conclusion

To fix ESLint not working in VS Code, we add some options to settings.json.

Categories
JavaScript Answers

How to get the day of week and the month of the year with JavaScript?

Sometimes, we want to get the day of week and the month of the year with JavaScript.

In this article, we’ll look at how to get the day of week and the month of the year with JavaScript.

How to get the day of week and the month of the year with JavaScript?

To get the day of week and the month of the year with JavaScript, we can use the date’s toLocaleTimeString method.

For instance, we write

const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false,
};
const prnDt = new Date().toLocaleTimeString("en-us", options);

to call toLocaleDateString on the Date object with the current date time with the locale string and options.

In options, we specify how to format different parts of the dates.

And it returns a date string with the formatted date and time.

'numeric' means the full number, '2-digit' formats the number as a 2 digit number.

'long' returns the full human readable date or time part.

Conclusion

To get the day of week and the month of the year with JavaScript, we can use the date’s toLocaleTimeString method.

Categories
JavaScript Answers

How to get yesterday’s date with moment.js and JavaScript?

Sometimes, we want to get yesterday’s date with moment.js and JavaScript.

In this article, we’ll look at how to get yesterday’s date with moment.js and JavaScript.

How to get yesterday’s date with moment.js and JavaScript?

To get yesterday’s date with moment.js and JavaScript, we call the subtract method.

For instance, we write

const yesterday = moment().subtract(1, "days");

to call moment to create a moment object with the current date.

And then we call subtract with 1 and 'days' to return a moment object that subtracts the current date by 1 day.

Conclusion

To get yesterday’s date with moment.js and JavaScript, we call the subtract method.

Categories
JavaScript Answers

How to clean up sinon stubs with JavaScript?

Sometimes, we want to clean up sinon stubs with JavaScript.

In this article, we’ll look at how to clean up sinon stubs with JavaScript.

How to clean up sinon stubs with JavaScript?

To clean up sinon stubs with JavaScript, we can call the sinon.restore method.

For instance, we write

const sinon = require("sinon");

it("should do something", () => {
  sinon.stub(some, "method");
});

afterEach(() => {
  sinon.restore();
});

to call sinon.stub to stub the some.method method in our test.

And then we call sinon.restore in the afterEach hook to clean up sinon stubs after each test.

Conclusion

To clean up sinon stubs with JavaScript, we can call the sinon.restore method.

Categories
JavaScript Answers

How to remove undefined fields from an object with JavaScript?

Sometimes, we want to remove undefined fields from an object with JavaScript

In this article, we’ll look at how to remove undefined fields from an object with JavaScript.

How to remove undefined fields from an object with JavaScript?

To remove undefined fields from an object with JavaScript, we can use the Object.keys method to get the keys of the object.

Then we call forEach with a callback that removes the undefined properties.

For instance, we write

Object.keys(obj).forEach((key) => {
  if (obj[key] === undefined) {
    delete obj[key];
  }
});

to get the obj object’s keys with Object.keys.

Then we call forEach with a callback to check if each object property is undefined with

obj[key] === undefined

And if it’s true, we use delete to remove the property.

Conclusion

To remove undefined fields from an object with JavaScript, we can use the Object.keys method to get the keys of the object.

Then we call forEach with a callback that removes the undefined properties.