Categories
JavaScript Answers

How to cap a number to a segment with JavaScript?

Sometimes, we want to cap a number to a segment with JavaScript.

In this article, we’ll look at how to cap a number to a segment with JavaScript.

How to cap a number to a segment with JavaScript?

To cap a number to a segment with JavaScript, we can use the Math.max and Math.min methods.

For instance, we write

const n = Math.max(min, Math.min(number, max));

to use Math.min(number, max) to get the min of number and max.

And then we call Math.amx with min and the number returned by Math.min(number, max) to cap the number between min and max.

If number is less than max and bigger than min, then it’ll be returned.

Conclusion

To cap a number to a segment with JavaScript, we can use the Math.max and Math.min methods.

Categories
JavaScript Answers

How to check if a key exists with HTML5 LocalStorage and JavaScript?

Sometimes, we want to check if a key exists with HTML5 LocalStorage and JavaScript.

In this article, we’ll look at how to check if a key exists with HTML5 LocalStorage and JavaScript.

How to check if a key exists with HTML5 LocalStorage and JavaScript?

To check if a key exists with HTML5 LocalStorage and JavaScript, we can use the getItem method.

For instance, we write

if (localStorage.getItem("username") === null) {
  //...
}

to check if the entry with key 'username' exists or not with getItem.

If it returns null, then the entry doesn’t exist.

Conclusion

To check if a key exists with HTML5 LocalStorage and JavaScript, we can use the getItem method.

Categories
JavaScript Answers

How to test Chrome extensions with JavaScript?

Sometimes, we want to test Chrome extensions with JavaScript.

In this article, we’ll look at how to test Chrome extensions with JavaScript.

How to test Chrome extensions with JavaScript?

To test Chrome extensions with JavaScript, we can use sinon-chrome.

For instance, we write

const vm = require("vm");
const fs = require("fs");
const chrome = require("sinon-chrome");

chrome.tabs.query.yields([
  { id: 1, title: "Tab 1" },
  { id: 2, title: "Tab 2" },
]);

const context = {
  chrome,
};

const code = fs.readFileSync("src/background.js");
vm.runInNewContext(code, context);

sinon.assert.calledOnce(chrome.browserAction.setBadgeText);
sinon.assert.calledWithMatch(chrome.browserAction.setBadgeText, {
  text: "2",
});

to mock the open tabs with

chrome.tabs.query.yields([
  { id: 1, title: "Tab 1" },
  { id: 2, title: "Tab 2" },
]);

Then we inject the mocked chrome.* API into the environment with

const context = {
  chrome,
};

And then we run the extension with

const code = fs.readFileSync("src/background.js");
vm.runInNewContext(code, context);

Finally, we do the assertions with

sinon.assert.calledOnce(chrome.browserAction.setBadgeText);
sinon.assert.calledWithMatch(chrome.browserAction.setBadgeText, {
  text: "2",
});

Conclusion

To test Chrome extensions with JavaScript, we can use sinon-chrome.

Categories
JavaScript Answers

How to get the caret column (not pixels) position in a textarea, in characters, from the start with JavaScript?

Sometimes, we want to get the caret column (not pixels) position in a textarea, in characters, from the start with JavaScript.

In this articl;e, we’ll look at how to get the caret column (not pixels) position in a textarea, in characters, from the start with JavaScript.

How to get the caret column (not pixels) position in a textarea, in characters, from the start with JavaScript?

To get the caret column (not pixels) position in a textarea, in characters, from the start with JavaScript, we can use the text area’s selectionStart and selectionEnd properties.

For instance, we write

textarea.value.substring(textarea.selectionStart, textarea.selectionEnd)

to call textarea.value.substring with textarea.selectionStart and textarea.selectionEnd to get the highlighted string in the text area.

Conclusion

To get the caret column (not pixels) position in a textarea, in characters, from the start with JavaScript, we can use the text area’s selectionStart and selectionEnd properties.

Categories
JavaScript Answers

How to prevent sticky hover effects for buttons on touch devices with CSS?

Sometimes, we want to prevent sticky hover effects for buttons on touch devices with CSS.

In this article, we’ll look at how to prevent sticky hover effects for buttons on touch devices with CSS.

How to prevent sticky hover effects for buttons on touch devices with CSS?

To prevent sticky hover effects for buttons on touch devices with CSS, we can use the hover media query.

For instance, we write

@media (hover: hover) {
  button:hover {
    background-color: blue;
  }
}

to apply styles for the hover media query.

We set buttons’ background color to blue if we’re hovering on a touch screen by using the media query.

Conclusion

To prevent sticky hover effects for buttons on touch devices with CSS, we can use the hover media query.