Hello, world.
This is a blog about application development web, desktop & mobile across a range of programming languages incl. JavaScript, C# and more.
These are interesting times for the web. Tag along as I get amazed on what the web can do, how AI is taking over the world, and sympathize my spectacular failures and stupidity.
Explore
- 📝 Posts — In-depth articles on web development, JavaScript, TypeScript, Vue.js, ASP.NET, and more
- 🗂️ Categories — Browse topics: JavaScript, Vue.js, TypeScript, ASP.NET, and more
- 🚀 Apps — Simple, powerful tools built for real-world use
Stay in touch!: 🐤Twitter | 🛜RSS | 🚀GitHub
A function can have its own methods - strange but true. Why? Because everything in Javascript is an object and object can have methods.
Consider -
function getSum(x, y) { return x + y; } console.log(getSum(1, 2)); //3 console.log(getSum.call(this, 1, 2)); //3 The first call to getSum is as expected. The second call uses a method called call and passes this as the argument along with the two numbers. This is equivalent to the first call.
Or, use call’s friend apply -
...
Chainable interfaces make your code readable and, arguably, your life more enjoyable.
Chainable interfaces are those series of methods that you see chained to an object. You can use them for objects where “relevant”.
Commonly used functions It is common to see methods chained to one another. The chaining works from left to right, with results from the left-side of the function passed to the right in sequence.
const message = "hello world"; const newMsg = message .replace("h", "y") .toUpperCase() .substr(0, 6); console.log("message: ", newMsg); // YELLO Custom Objects One of the ways of demonstrating chainable interfaces for our own objects is by defining a function, and defining props and prototypes for the function.
...
Do not set properties to null or empty, always use delete.
Consider -
const earth = { name: "earth", position: 3, aweIndex: 42 }; console.log(earth); // { name: 'earth', position: 3, aweIndex: 42 } If you set any property to null -
earth["aweIndex"] = undefined; console.log(earth); // { name: 'earth', position: 3, aweIndex: undefined } You can expect similar results if you set the prop to null.
If you really want to delete the object, do a delete prop.
delete earth["aweIndex"]; console.log(earth); // { name: 'earth', position: 3 } And before you ask - yes, this is covered when we were discussing (almost) everything about object and props in Javascript.
...
Check if a given object has a specified property. You can either use non-enumerable props or hasOwnProperty.
Consider the below code -
Object.prototype.awesomeness = 42; const earth = {}; console.log(earth["awesomeness"]); // 42 We have defined a prototype that gets attached to any object. Therefore the prop defined by prototype is attached to the object as well.
What if you want to check all props in earth?
for (prop in earth) { console.log(prop, ":", earth[prop]); // 42 } Even when the object is empty, the awesomeness prop rears it beautiful head.
...
Use MySQL load data function to load data from files within within AdonisJS services.
You can perform batch data and file operations efficiently using database utilities. The logic will likely be quicker, lighter on resources, and overall, more suited for batch jobs.
Loading data from files is not as sought-after as in the good-ol’ days, but is quite common in enterprise applications. One or more of the following operations may be required to load data from files -
...
Use Adonis Scheduler to create batch tasks as well as schedule execution of said tasks.
Adonis Scheduler improves your efficiency in writing batch jobs. I am talking about bulk operations that may or may not be suitable for your general purpose service and controllers.
Although scheduler’s purpose seems to be, well, scheduling stuff we can reuse Tasks enabled by scheduler to run bulk operations.
Install Scheduler First, install scheduler -
yarn add adonis-scheduler Let AdonisJS know that you have added scheduler -
...
I have this annoying behaviour to force as many things as I can through the business layer. This can be a boon and bane at the same time.
What do I mean by business layer? Let’s say I have an application running on AdonisJS. For every operation (CRUD) -
I try to go through controllers/services Almost all of them through APIs when called from front-end Almost all of the operations through Lucid with exceptions through Database statements Use Tasks or equivalent for batches Everyone does this, why is it a point of discussion? Going through business layer is good. You can control what gets read or updated when routing through the server layers.
...
Use Array.some() and Array.every() to check whether an array has elements that satisfy the given criteria.
Consider the below array -
const nums = [3, 5, 6, 1, 2]; You can check whether the elements satisfy a specified criteria with below code -
console.log(nums.some((val, index) => val > 3)); // true console.log(nums.some((val, index) => val > 10)); // false You can do the same for objects in the array.
const fruits = [(apple = { color: "red" }), (orange = { color: "orange" })]; console.log(fruits.some((val, index) => val.color == "red")); // true console.log(fruits.some((val, index) => val.color == "blue")); // false In a similar vein, you can check whether every element in the array satisfies a given criteria.
...
Often my server DB functions return the data in JSON format with all fields and values thrown in.
const alphaNum = { a: 1, b: 2, c: 3, d: 4, e: 5 }; When we use charts (or even tables), this needs to be converted to an array of arrays. Most of the iterative functions work well with arrays and not objects.
We would want to convert the object into [['a', 1], ['b', 2]...].
Use Object.keys We can convert JSON to array by using a map function on Object.keys.
...
Initializing objects can be a pain. You may end up with multiple checks if you are trying to set a specific prop in the object path to a pre-defined value.
For e.g. set color of a Kawasaki bike to ‘blue’. To do this we have to traverse through the object path from the root through color. This may be made more difficult if you may or may not get a partial array from an external function or a third party system.
...