Categories
JavaScript Answers

How to remove line breaks from start and end of string with JavaScript?

Sometimes, we want to remove line breaks from start and end of string with JavaScript.

In this article, we’ll look at how to remove line breaks from start and end of string with JavaScript.

How to remove line breaks from start and end of string with JavaScript?

To remove line breaks from start and end of string with JavaScript, we can use the string replace method.

For instance, we write

const newwStr = str.replace(/^\s+|\s+$/g, "");

to call replace to remove line breaks from the start and end of the string by replacing them with empty strings.

^\s+ matches the line break at the start of the string.

\s+$ matches the line break at the end of the string.

Conclusion

To remove line breaks from start and end of string with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to run Node.js app with ES6 features enabled?

Sometimes, we want to run Node.js app with ES6 features enabled.

In this article, we’ll look at how to run Node.js app with ES6 features enabled.

How to run Node.js app with ES6 features enabled?

To run Node.js app with ES6 features enabled, we install a few packages.

We enable ES6 features by running

npm install --save-dev babel
npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0
npm install --save-dev nodemon

to install Babel and nodemon.

Then we create a .babelrc file in the project root and add

{
  "presets": ["es2015", "stage-0"]
}

to let us run our project with ES6 features.

Then in package.json, we write

{
  //...
  "scripts": {
    "watch": "babel -w src/ -d dist/",
    "build": "babel src/ -d dist/",
    "serve": "babel -w src/ -d dist/ | nodemon --watch dist",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
  //...
}

to run babel with nodemon to let us run our Node.js app with Babel.

Conclusion

To run Node.js app with ES6 features enabled, we install a few packages.

Categories
JavaScript Answers

How to sort mixed alpha and numeric array with JavaScript?

Sometimes, we want to sort mixed alpha and numeric array with JavaScript.

In this article, we’ll look at how to sort mixed alpha and numeric array with JavaScript.

How to sort mixed alpha and numeric array with JavaScript?

To sort mixed alpha and numeric array with JavaScript, we can use the array sort and string localeCompare methods.

For instance, we write

const sortAlphaNum = (a, b) => a.localeCompare(b, "en", { numeric: true });
console.log(
  [
    "A1",
    "A10",
    "A11",
    "A12",
    "A2",
    "A3",
    "A4",
    "B10",
    "B2",
    "F1",
    "F12",
    "F3",
  ].sort(sortAlphaNum)
);

to define the sortAlphaNum function that calls the localeCompare method to compare strings a and b.

We set numeric to true to let us compare numeric strings.

Then we call sort with sortAlphaNum to return a new array with the items sorted lexically.

Conclusion

To sort mixed alpha and numeric array with JavaScript, we can use the array sort and string localeCompare methods.

Categories
JavaScript Answers

How to get the row count of a HTML table with JavaScript?

Sometimes, we want to get the row count of a HTML table with JavaScript.

In this article, we’ll look at how to get the row count of a HTML table with JavaScript.

How to get the row count of a HTML table with JavaScript?

To get the row count of a HTML table with JavaScript, we can use the rows property.

For instance, we write

<table id="tableId">
  <thead>
    <tr>
      <th>Header</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Row 1</td>
    </tr>
    <tr>
      <td>Row 2</td>
    </tr>
    <tr>
      <td>Row 3</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Footer</td>
    </tr>
  </tfoot>
</table>

to add a table.

Then we write

const table = document.getElementById("tableId");
const totalRowCount = table.rows.length;
const tbodyRowCount = table.tBodies[0].rows.length;

to select the table with getElementById.

Then we use table.rows.length to get a node list of all tr elements in the table.

We use tBodies[0] to get the first tbody element in the table.

And we use rows to get all tr elements in the tbody element.

We get the count of each with length.

Conclusion

To get the row count of a HTML table with JavaScript, we can use the rows property.

Categories
JavaScript Answers

How to wait until an element is visible with Puppeteer and JavaScript?

Sometimes, we want to wait until an element is visible with Puppeteer and JavaScript.

In this article, we’ll look at how to wait until an element is visible with Puppeteer and JavaScript.

How to wait until an element is visible with Puppeteer and JavaScript?

To wait until an element is visible with Puppeteer and JavaScript, we can use the waitForFunction method.

For instance, we write

await page.waitForFunction(
  "document.querySelector('.btnNext') && document.querySelector('.btnNext').clientHeight !== 0"
);
await page.waitForFunction(
  "document.querySelector('.btnNext') && document.querySelector('.btnNext').style.visibility !== 'hidden'"
);

const btnNext = await page.$(".btnNext");
await btnNext.click();

to call waitForFunction with a string with expressions that checks if the element has a non-zero height and its visibility isn’t hidden.

After that, we get the element with $ and call click to click it.

Conclusion

To wait until an element is visible with Puppeteer and JavaScript, we can use the waitForFunction method.