How to create a custom React hook for locale-specific date formatting

In this post, I will show you how to create a custom React hook that provides locale-specific date formatting functions. This hook can be useful if you want to display dates and times in different languages and formats depending on the user’s preferences. What is a custom React hook? A custom React hook is a function that starts with the word use and may call other hooks. Custom hooks let you reuse stateful logic between components without duplicating code or introducing complex patterns. You can learn more about custom hooks from the official React documentation (1) or this tutorial (2). ...

How to capitalize each word of a string in TypeScript

In the world of web development and application design, small details can make a big difference. One such detail is the proper capitalization of text, especially when dealing with user-generated content or dynamic data. Ensuring that text appears neat and consistent can significantly enhance the user experience. In this blog post, we’ll introduce you to a handy utility function called capitalizeEachWord that can help you achieve just that. What is capitalizeEachWord? The capitalizeEachWord function is a TypeScript utility that capitalizes the first letter of each word in a given string while keeping the rest of the letters in lowercase. This function also allows you to specify a word separator, which can be useful when dealing with text containing multiple words separated by a character or whitespace. ...

How to scrape all links from a website

Simply copy-paste the following one-liner to Chrome Dev Tools. To limit the results, set the reg exp placeholder to contain some sensible value, or replace the function call with return value true. Array.prototype.slice.call( document.getElementsByTagName('a'), 0 ) .map(i => i.href) .filter(i => i.match('reg exp placeholder')) .forEach(i => console.log(i)) Here is an explanation for what it does: Selects all the elements on the document. Converts the HTMLCollection object returned by getElementsByTagName into an Array by using the Array.prototype.slice method. The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. Here, it selects all the items starting from index 0 to the end of the array by passing 0 as the first argument to slice() method. Maps the href attribute of each of the selected elements into an array using the Array.prototype.map method. The resulting array will contain a list of all the href attributes of the elements. Filters the array obtained in the previous step by selecting only those elements that match a regular expression pattern. The pattern is a placeholder and should be replaced with an actual regular expression to match the desired elements. This is achieved using the Array.prototype.filter method. Loops through the resulting filtered array using the forEach() method and logs each element to the console using console.log() method. This will output the list of href attributes of the elements that match the given regular expression pattern to the console.

How to bump version number on Git push for projects using npm

Note: This works only from command line as user feedback is needed from keyboard to know which part of the version number to bump. It should not break pushing from a GUI tool (e.g. TortoiseGit), but you will not be prompted to update the version number. The positive thing is that the version change is done neatly as a separate commit. Install husky as dev-dependency See instructions. Add the following script into new file .husky/pre-push ...

How to collect all npm licenses from multiple subdirectories with Powershell

Install license-report (https://www.npmjs.com/package/license-report) npm install -g license-report run the script Get-ChildItem -Directory | foreach { $_ >> ./licenses.csv ; license-report --output=csv --only=prod --package=./$_/package.json >> ./licenses.csv } You can use the same idea to run other stuff in subdirectories. Just replace the command. Here is an example of git pull. Get-ChildItem -Directory -Force -Recurse *.git | ForEach-Object { cd $_.Parent.FullName; Write-Host $_.Parent.FullName; git pull }