Posts

Creating a Secure Password Generator Using TypeScript

Password generation is something you’re hopefully using a password manager for these days. However, you might not be aware that modern browsers support some great crypto features. In this quick little tutorial, we will use the Crypto API to create a strong password generator. The code is remarkably simple, and you can adapt this to generate unique values for games and other purposes besides passwords. /\*\* \* Generates a random password of the specified length. \* \* @param length The length of the password to generate. \* @returns A Promise that resolves to a string containing the generated password. \* @throws An error if the length argument is less than 1. \*/ const generatePassword = async (length: number): Promise => { if (length < 1) { throw new Error('Length must be greater than 0'); } // Create a new Uint8Array with the specified length. const buffer = new Uint8Array(length); // Get the browser's crypto object for generating random numbers. const crypto = window.crypto || (window as any).msCrypto; // For compatibility with IE11. // Generate random values and store them in the buffer. const array = await crypto.getRandomValues(buffer); // Initialize an empty string to hold the generated password. let password = ''; // Define the characters that can be used in the password. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; // Iterate over the array of random values and add characters to the password. for (let i = 0; i < length; i++) { // Use the modulus operator to get a random index in the characters string // and add the corresponding character to the password. password += characters.charAt(array[i] % characters.length); } // Return the generated password. return password; }; As you can see, the getRandomValues method does all of the heavy lifting here for generating our password. We also define what characters are allowed in our password, allowing us to remove ambiguous characters if we wish.

It's Done When It's Done

There are more methodologies than you can shake a stick at. All promise to streamline your workflow and deliver quality software, many of which leverage the same old approach to work: estimation and timelines. I am not saying that timelines need to be replaced entirely. Because they are a necessary evil. However, in my experience, most companies putting deadlines on features and projects use arbitrary figures, not for a valid reason.

Samsung Galaxy S23 Ultra review

Samsung is making yearly phone upgrades obsolete. I used my beloved Galaxy Note 10+ Plus until its dying breath. Even when the charging port failed and could only be charged wireless, I persisted until one day; it refused to charge. I have been using the Samsung Galaxy S21 Ultra for the last couple of years. It’s no Note, but it’s one of the best phones I have ever owned. Even now, the hardware specifications of the S21 hold up against newer devices.

ChatGPT Stopping Part Way Through a Response? Here Is How You Fix It

The ChatGPT tool is possibly part of your everyday workflow. I’ve been using it for research purposes and as a writing assistant, where it thrives at. However, one of the issues with ChatGPT is that it has a limitation on its response length. You write in a prompt, and ChatGPT starts generating its response. All of a sudden, it stops. I hope OpenAI one day increases the response limit because, besides the fact, there is a way to fix it, it’s still frustrating.

Happy AI Valentine's Day

Valentine’s Day is upon us and this year that heartfelt card you get from your significant other this year might be written using AI. Specifically, ChatGPT which has taken the internet by storm. In fact, I asked ChatGPT to write a really heartfelt card message and this is what it produced. My dearest [Wife’s Name], On this Valentine’s Day, I want to take a moment to express how grateful I am to have you in my life. You are my rock, my support, and my best friend. You have brought so much joy and happiness into my life and I am forever grateful for your love.

A real threat has emerged to challenge Google and it might be Microsoft

The term “googling” is synonymous with searching for things online. For years, Google has enjoyed a monopoly on search, advertising, and other facets of internet life. As Yahoo! fades into the ether and Microsoft’s Bing exists but isn’t used much (who says “binging” or “bing it”?), we are starting to see competition heat up in the search space are decades of Google dominance. The threat to Google is ironically coming from Microsoft.

Why Don't You Hear Much About GraphQL These Days?

You might have noticed that you don’t hear about GraphQL as much as you used to. Some people might have you believe that developers have lost interest in this technology, but that couldn’t be further from the truth. This blog post will explore why GraphQL isn’t as talked about as it used to be and why it’s still a relevant and valuable technology for developers. Maturity and stability GraphQL has come a long way since its introduction a few years ago. As more and more companies adopt GraphQL, the technology has become more mature and stable. The growing number of real-world applications built with GraphQL has resulted in fewer bugs and compatibility issues, making GraphQL a reliable and predictable technology for developers. With its growing popularity, GraphQL has become a well-established technology widely accepted and used in the industry, reducing the need for developers to talk about and evangelize GraphQL.

Sentiment Analysis Using TypeScript Without Dependencies

Sentiment analysis is usually a task that requires a specialised dataset and machine-learning techniques to implement properly. However, I thought it might be a nice exercise to try and implement sentiment analysis with TypeScript without training models. // Define a type for the sentiment result type Sentiment = 'positive' | 'neutral' | 'negative'; // Class to perform sentiment analysis on a given text class SentimentAnalysis { // Arrays of positive and negative words to use in the analysis private positiveWords = ["love", "like", "great", "good", "happy", "awesome"]; private negativeWords = ["hate", "dislike", "bad", "angry", "sad", "terrible"]; // Method to perform sentiment analysis on a given text public getSentiment(text: string): { sentiment: Sentiment, positiveWords: number, negativeWords: number, neutralWords: number } { // Convert the text to lowercase to make the analysis case-insensitive const lowerText = text.toLowerCase(); // Sum up the number of times each positive word appears in the text let positiveScore = this.positiveWords.reduce((acc, word) => { // Use a regular expression to match the word in the text return acc + (lowerText.match(new RegExp(word, 'g')) || []).length; }, 0); // Sum up the number of times each negative word appears in the text let negativeScore = this.negativeWords.reduce((acc, word) => { // Use a regular expression to match the word in the text return acc + (lowerText.match(new RegExp(word, 'g')) || []).length; }, 0); // Calculate the number of neutral words by subtracting the positive and negative words from the total number of words let neutralScore = lowerText.split(' ').length - positiveScore - negativeScore; // Compare the number of positive and negative words and return the sentiment result if (positiveScore > negativeScore) { return { sentiment: "positive", positiveWords: positiveScore, negativeWords: negativeScore, neutralWords: neutralScore }; } else if (positiveScore < negativeScore) { return { sentiment: "negative", positiveWords: positiveScore, negativeWords: negativeScore, neutralWords: neutralScore }; } else { return { sentiment: "neutral", positiveWords: positiveScore, negativeWords: negativeScore, neutralWords: neutralScore }; } } } // Create an instance of the SentimentAnalysis class const sentimentAnalysis = new SentimentAnalysis(); // Analyze some sample text const result = sentimentAnalysis.getSentiment("I love this code and think it is great!"); // Log the result console.log(result); This code uses a class called SentimentAnalysis to perform sentiment analysis on a given text. The class has two arrays of positive and negative words and a method called getSentiment that performs the analysis.

Handling Errors with the Fetch API

The Fetch API is a modern and efficient way to retrieve resources from a server. It is an interface that provides a unified way to fetch resources from different sources. Fetch makes sending HTTP requests, including GET and POST, easy and handles responses asynchronously. However, handling errors properly is important when working with the Fetch API. Errors can occur for various reasons, such as a network error, a server-side error, or an invalid URL. Failing to handle these errors can result in unexpected behaviour and break your application.

The PlayStation VR 2 (PSVR 2) Headset Needs PC Modders to Survive

I tend to get excited about virtual reality and have wanted to see it succeed for over a decade. While VR has undoubtedly grown, it’s not mainstream due to the barrier to entry. Most notably, requiring beefy PC setups or locked down (like the original PSVR headset). Adding to my growing collection of VR headsets, I preordered the PSVR 2 headset for the PlayStation 5. There has been work done on untethered VR headsets, failed beginnings with Samsung creating phone VR headsets before Meta struck gold with the Meta Quest. Not only does it support using it by itself, but it can also be connected to a computer. Then you have the heavy, expensive hitters like the HTC Vive Pro 2 for powerful computer VR experiences.