Need to install a specific version of Yarn? Simply run the command below in your terminal. Replace 1.19.1 with the yarn version you want to install.
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.19.1
Oh, just a software engineer's little blog.
Need to install a specific version of Yarn? Simply run the command below in your terminal. Replace 1.19.1 with the yarn version you want to install.
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.19.1
So let’s say I have a React component written in TypeScript with a private member variable that I would like to unit test. The component is very complicated and I need to make sure the private variable has the correct value, and it looks something like this:
export class HelloWorld extends React.Component<Props, State> { private message = "foobar"; public render() { return <div>Hello World</div>; } }
If I mount the component with Enzyme, I can find the component, but I cannot see the private variables inside it. One thing I can do is to create an interface of that component just for testing purposes. In the interface, I can reveal the private variables, like this:
interface HelloWorldInstance extends React.Component<Props, State> { message: string; }
With this interface created, I can mount the component, get the instance of it, and do a type assertion. Now I can test that private variable without TypeScript complaining:
describe("HelloWorld", () => { it("has correct message", () => { const wrapper = mount(<HelloWorld />); const helloWorld = wrapper.find(HelloWorld); const helloWorldInstance = helloWorld.instance() as HelloWorldInstance; expect(helloWorldInstance.message).toBe("foobar"); }); });
I recently made a very simple website with Next.js that would display the 5 newest YouTube videos from a particular channel using the YouTube Data API. I quickly learned three things after deploying the code to Netlify and Now:
At this point, I decided to look online to see how to run an Express server with Next.js so I can keep the private API key hidden in the backend and so I can always display the 5 latest videos without needing to redeploy my code. Here are the steps I took starting from the beginning.
Run these 2 lines to initialize the project and install Next.js, React, and Express:
npm init npm install --save next react react-dom express
Now edit the package.json file to make the scripts section look like this:
"scripts": { "dev": "node server.js", "build": "next build", "start": "next start", "deploy": "NODE_ENV=production next build && NODE_ENV=production node ./server.js" },
Tip: Use “nodemon server.js” when you are devving so every time you make a change to the server.js file, it will automatically restart your server. Run npm install -g nodemon to install the utility.
Create a folder called “pages” in the root directory with a file named index.js inside the folder. Add the following code into pages/index.js:
function Home() { return (<div>Hello next.js on express!</div>); } export default Home;
This small block of code simply displays a short message when someone visits the website.
In the root directory, create a server.js file and copy/paste the following code. Credit to tilomitra’s blog post: Building Server-rendered React Apps with NextJS.
const express = require('express'); const next = require('next'); const dev = process.env.NODE_ENV !== 'production'; const port = process.env.PORT || 3000; const app = next({ dev }); const handle = app.getRequestHandler(); app.prepare() .then(() => { const server = express(); server.get('/test', (req, res) => { res.send('Hello from express!', 200); }); server.get('*', (req, res) => { return handle(req, res); }); server.listen(port, (err) => { if (err) throw err; console.log(`> Ready on http://localhost:${port}`); }); }) .catch((ex) => { console.error(ex.stack); process.exit(1); });
The ~30 lines of code here basically creates an express server where you can have custom endpoints for you to do API data fetching. For example, you can create a /api/videos endpoint for fetching and returning JSON data by adding this block:
server.get('/api/videos', async (req, res) => { const apiResponse = await fetch('https://example.com/api'); const apiData = await apiResponse.json(); res.json({ totalVideos: apiData.totalResults, videos: apiData.items, }); });
In the /pages/index.js file, you can do a fetch on the /api/videos endpoint and render the data however you’d like.
Now that the code is done, I decided to deploy to AWS Elastic Beanstalk because I have used AWS for other projects before. It was fairly easy but I will outline the steps.
First, go to the Elastic Beanstalk page and click on “Create New Application” at the top right corner. Fill out the application name and a description.
You should now be on the newly created application page and it’ll tell you to create a new environment.
Click on “Create one now” and select “Web server environment“.
On the next page, you can name the environment anything you’d like, pick a nice domain, select Node.js as the preconfigured platform, and then upload a zip file of the directory with all of the code created from the beginning of this blog post.
Click on “Create environment” to start the process!
After a few minutes, you should see your completed environment.
Almost done. Click on Configuration on the left side and then click Modify in the Software box.
In the Node command input box, enter: npm run deploy. This makes the environment run the deploy script as defined in the scripts section in the package.json file.
Make sure to hit the Apply button, and that’s all there is to it! The code should be deployed and reachable through the elastic beanstalk domain (i.e. <domain>.us-east-1.elasticbeanstalk.com).
Too lazy to create the files and copy/pasting the code? Download the files from the repo I made here: https://github.com/doobix/nextjs-express-demo
Hope this was helpful, let me know your thoughts in the comments section!
I’ve published my first Android app! 🎉 This is yet another Barbell Weight Calculator, pretty much a copy of my web app version. The main differences are: 1. it is an installable Android app from the Google Play Store, and 2. it can calculate percentages of your one rep max!
Since this app was created using React Native, the code can be built for Android devices, as well as iOS devices. I would love to put this app in the Apple App Store, however, Apple charges a yearly developer fee of $99! Google, on the other hand, charges only a one-time developer fee of $25. So in the mean time, I’ll only be supporting the Android version of the app.
If you have an Android phone, check out the Play Store link below! Otherwise, feel free to take a look at the code on GitHub.
Google Play Store: https://play.google.com/store/apps/details?id=com.seewes.barbell
GitHub: https://github.com/doobix/barbell-native
Screenshots:
Hi! Haven’t updated this blog in a while. Just wanted to say that I made a little web app using React to make calculating barbell weights easier.
For example, if we’re trying to lift 210 pounds, which plates do we need to get? By using the web app, it quickly tells us that each side of the barbell needs to have: 45lbs, 25lbs, 10lbs, and a 2.5lbs.
Check out the web app here: http://barbell.seewes.com
GitHub repo here: https://github.com/doobix/barbell
Recently, Valve had released their Counter-Strike: Global Offensive game state integration for developers to use. This allows the game client to send all game data, such as the player’s health, guns, ammo, frags, etc. to a HTTP POST endpoint server.
After seeing the PGL Arena Effects in the DreamHack Cluj-Napoca CS:GO tournament a couple of months ago, I decided to make full use of my Philips Hue lights in my room by recreating the C4 bomb lighting effects.
Since I’ve been coding in Python a lot recently, I stuck with this language for this project. First, I had to create the HTTP server to receive the POST game state data, so I used the Flask microframework. I launched the Flask server and joined a game in CS:GO, and immediately I was seeing tons of JSON being received in the terminal window. All I really needed was the C4 bomb information, which I was able to find in data['round']['bomb']. By checking that key, I noticed there were 3 values if the key existed: planted, exploded, and defused. If the bomb isn’t planted, there won’t be a bomb key in the round object at all. So after getting the bomb status, I made the server write the result to a file conveniently named bomb_status.
Now I had to do the fun part — making my Philips Hue lights turn on and change colors. I went step by step with the Philips Hue API Getting Started guide, and it was very easy to follow and understand the RESTful interface. Once I understood how to control the Hue lights, I wrote a 2nd Python script for reading the bomb_status file. In this 2nd script, I have a while loop that runs indefinitely, with a 250ms wait time in between each loop. In each loop iteration, it gets the bomb status and goes through a series of if-statements to determine what to do. Here’s the run down of it:
And that’s basically it.
My Python code is now on GitHub:
https://github.com/doobix/csgo-c4-hue
I have created a Node.js version of the same code, also on GitHub:
https://github.com/doobix/csgo-c4-hue-node
A video of the script in action is uploaded to YouTube:
https://www.youtube.com/watch?v=QBdI54MHB-k
Reddit thread:
https://redd.it/3wjpgg
After graduating from Hack Reactor last month, I’ve been looking for companies to apply to. One thing that I found myself doing a lot of was visiting a company’s website, and then going to Glassdoor to see how employees rate that company. So I thought, wouldn’t it be nice to have a 1-click button that would display the Glassdoor ratings on demand? It would save a lot of time opening a new tab, going to Glassdoor.com, and then searching for the company.
Seeing that no such extension existed in the Chrome Web Store, I decided to create one myself. I had modified existing Chrome extensions before, but this was my first time creating a brand new extension from scratch.
Introducing… seeglassdoor!
The most difficult part was to figure out how to grab a URL from a current tab.
To grab a URL of a tab that you are viewing, you need the “tabs” permission in your manifest.json file:
1 2 3 |
"permissions": [ "tabs" ] |
Then in your javascript, you will be able to use this code:
1 2 3 4 |
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) { var url = tabs[0].url; console.log(url); }); |
Overall, this project was not a very difficult one, and was a good learning experience for my first Chrome extension. Feel free to take a look at my extension below.
Download seeglassdoor from the Chrome Web Store: https://chrome.google.com/webstore/detail/seeglassdoor/cgganckkpjppenjelhbimliplciledbb
GitHub seeglassdoor Project Repo: https://github.com/doobix/seeglassdoor
My friend asked me a simple JavaScript question last night… or so I thought.
The premise is that we have a javascript object like this:
1 2 3 4 5 |
var object = { 1: { items: ['a','b','c'] } } |
Now the question is, how do you access that items array?
From what I know:
So, I told my friend that we can access that items array by using object.1.items or object['1'].items .
However, object['1'].items worked, while object.1.items threw an error! To my surprise, object[1].items also worked!
Now let’s say the javascript object is like this:
1 2 3 4 5 |
var object = { one: { items: ['a','b','c'] } } |
In this second case, I tested it in the Chrome browser console and confirmed that it is how I originally thought it would work: object['one'].items and object.one.items both work (and object[one].items does not work).
I think I will just use the object['X'] format since it works in both cases: when the key is a number and when it is a word.
I still don’t know the reason for the quirkiness when the key is an integer, so feel free to leave a comment and let me know!
Pigeonhole sort is an interesting sorting algorithm that makes use of an array filled with zeros and its indices. It can sort values in linear time and is relatively simple to implement. However, the tradeoff for its sorting speed is the space that it requires. Also, since it makes use of array indices, only positive integers can be sorted using this algorithm.
So the basic idea of pigeonhole sort is:
Let’s say for this example, we want to sort this inputArray:
1 2 |
// inputArray: [5, 2, 7, 5, 1] |
The first thing the algorithm needs to know is what the maximum value of the inputArray is. We need to create a pigeonholeArray that has indices that go up to the max value. If we don’t know the maximum, we’ll have to loop through the inputArray one time to figure it out. In this case, our biggest value is 7, thus we need to create a pigeonholeArray that can hold 8 elements (which will have indices 0 to 7).
1 2 |
// pigeonholeArray: [0, 0, 0, 0, 0, 0, 0, 0] |
Now we need to loop through the unsorted inputArray (again), and as we do that, we will add 1 to the pigeonholeArray’s value at the pigeonholeArray’s index corresponding with the inputArray’s value. So as we iterate our input array, our pigeonhole array will look like this:
1 2 3 4 5 6 |
// Progress of pigeonholeArray [0, 0, 0, 0, 0, 1, 0, 0] // when the inputArray value is at 5 [0, 0, 1, 0, 0, 1, 0, 0] // when the inputArray value is at 2 [0, 0, 1, 0, 0, 1, 0, 1] // when the inputArray value is at 7 [0, 0, 1, 0, 0, 2, 0, 1] // when the inputArray value is at 5 [0, 1, 1, 0, 0, 2, 0, 1] // when the inputArray value is at 1 |
As you can see, after looping through the entire inputArray, our pigeonholeArray will contain all of inputArray’s values. We will just have to make one last iteration through the pigeonholeArray. For each pigeonholeArray’s value that is greater than zero, we’ll add the pigeonholeArray’s index to the inputArray. We can replace the old values in the inputArray so that no new array needs to be created.
Check out the completed algorithm below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
function pigeonholeSort(inputArray) { // Find the max value in inputArray var max = inputArray[0]; for (var i = 1; i < inputArray.length; i++) { if (max < inputArray[i]) max = inputArray[i]; } // Create pigeonholeArray filled with zeros var pigeonholeArray = []; for (var i = 0; i <= max; i++) { pigeonholeArray.push(0); } // Iterate through inputArray and add 1's to pigeonholeArray for (var i = 0; i < inputArray.length; i++) { pigeonholeArray[inputArray[i]]++; } // Iterate through the pigeonholeArray and replace values in inputArray var inputArrayIndex = 0; for (var i = 0; i < pigeonholeArray.length; i++) { // Add to inputArray until the current pigeonholeArray index is 0 while (pigeonholeArray[i]) { inputArray[inputArrayIndex] = i; pigeonholeArray[i]--; inputArrayIndex++; } } // return sorted inputArray return inputArray; } |
JavaScript has this system called an event loop. I’m not sure if my idea of it is exactly correct, but I will try to explain it from what I can recall from the lecture.
So basically, the event loop is something that is continuously running in the background of your website only if there is nothing in the call stack. It does 3 things:
The call stack contains functions that need to be invoked. If the functions have any callbacks, those will get added into the event table to be invoked when it is time to do so.
Example: Let’s say the current time is 2:00:00pm, and the call stack contains this function that logs “Hello World” after 1 second.
1 |
setTimeout(console.log("Hello World"), 1000); |
That will get invoked, and the callback function console.log("Hello World") would be added to the event table with the time set to 2:00:01pm.
The event table stores more functions with the specific time that they should be ran. If the time in the table is greater or equal to the current time, then the function will be moved to the message queue.
Example: console.log("Hello World") would only be added to the message queue when the clock has turned to 2:00:01pm.
Lastly, the message queue contains functions that are next in line to be ran. These functions will be popped off the queue and be added to the call stack.
Example: console.log("Hello World") will be moved into the call stack. This completes a full circle, and the event loop will repeat agian.