Print a page section on button-click in Vue

Use a button on a Vue component to start a print dialog and print a specific section. Setup We will use a small library called ‘html2paper’. First install the said library in your Vue project - npm install vue-html-to-paper Add library to your code in main.js - // main.js import Vue from "vue"; // ... import VueHtmlToPaper from "vue-html-to-paper"; // ... const options = { name: "_blank", specs: ["fullscreen=yes", "titlebar=yes", "scrollbars=yes"], styles: [ "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css", "https://unpkg.com/kidlat-css/css/kidlat.css" ] }; Vue.use(VueHtmlToPaper, options); // ... We will use a Vuetify example to demonstrate how this is used in Vue code, because - why not? ...

Use await without async function in Javascript

How can you use await without an async function? Don’t click or proceed further if you are not into practical jokes. Say, you try using await in a “normal” function or directly within a module.. function getWeather(loc) { const weather = await fetchWeather(loc); return weather; } // SyntaxError: await is only valid in async function You receive a compilation error that await can be used only in async function. But, if I have to use await but do not want to create a separate function with its own memory space, what should I do? ...

Generate Fibonacci series using generators

Generate Fibonacci series with generators and feel good about yourself. Previously you have seen how the totally practical example of generating Fibonacci series was breathing fire with memoization, and may have also come across using loops or recursion to solve the same problem. But, there is no fun in all that - this is real-life and we have to “generate” fun (yep, there we go again). If you have forgotten your schooling, here’s the series - any number is the sum of previous two numbers (except the first two, of course). ...

Function object and Function in an object

Previously, we have talked about all things being an object in Javascript. But to what extent can we apply this to our beloved functions. You can see this almost everywhere with the below syntax - const getSum = (x, y) => { return x + y; }; console.log(getSum(1, 2)); // 3 We have also touched upon how function is an object and how that helps in using closures and currying. For now, let’s leave all those complex things behind. Function being an object implies that we can do this - ...

Generators in Javascript

What are generators and how can I use them? How do you write loops in Javascript - Simple for/while? Recursion? In other less exciting ways? How do you throttle and control function execution? Debounce function? Build your own? What if you could produce the right mix of reusable functions, looping and throttling in one go? Enter generators. What are Generators? Generators are functions that lets you can control iteration. You can use generators to suspend execution of a function while saving the context for continuing the execution at a later time. ...

Validating props in Vue components

Validate your props in Vue components. We have seen all about props in Vue before. We can define a simple prop in a component, pass the value from source, and the receiving component happily processes it. <template> <div>{{ contactDetails }}</div> </template> <script> export default { data: () => ({}), props: { contactDetails: { type: String, required: true } } }; </script> But, what if you want to validate the prop? We have certainly seen the warnings when we do not pass the prop of the expected type or when not supplying a mandatory prop. ...

A simple password field in Vuetify

Enable a simple password field in Vuetify. Validating passwords is a repetitive task, but a task nevertheless. But, how do you make sure that the password field is modern and the password complies to pre-defined rules? I don’t quite build super sensitive applications, and tend to use a simple VeeValidate or Vuelidate expression to validate passwords. Here’s the Vue component.. <!-- PasswordChange.vue --> <template> <v-container> <v-form ref="form" v-model="valid"> <v-layout text-center wrap> <v-flex xs12></v-flex> <v-flex xs12 mb-4> <h1 class="display-1 font-weight-bold mb-3">Change thy password</h1> <p class="subheading font-weight-light grey--text"> It is 2020, but password is still a thing. </p> </v-flex> <v-flex offset-md-3 xs6> <v-text-field autocomplete="current-password" :value="userPassword" label="Enter password" hint="Your password passed! Password rules are not meant to be broken!" :append-icon="value ? 'mdi-eye' : 'mdi-eye-off'" @click:append="() => (value = !value)" :type="value ? 'password' : 'text'" :rules="[rules.password]" @input="_=>userPassword=_" ></v-text-field> </v-flex> </v-layout> </v-form> </v-container> </template> We are doing a few basic things in the component - ...

The NPM Problem in Javascript

Is there really a problem with NPM use? Short answer: no. Long answer: it’s complicated. I am nothing but thankful for the tonnes of NPM packages. I would never have built web applications that could support thousands of users all by myself without all those hundreds of packages. There are thousands of developers (if not millions) who do much better than what I could. With NPM to support what I do - I just identify the right solution, search in NPM / GitHub, and most of the times find it. At least I find something in the general area of what I am looking for. ...

Flatten recursive arrays

Flatten recursive arrays using Array.flat(). We have seen how to flatten arrays before. Array.flat() is quite useful to flatten arrays in one statement and output a simpler array. But, what about situations where you do not know the depth of array hierarchy - const nums = [[1, 2], [3, 4], [[5], [6]], [[[7]]]]; Fear not. Array.flat() takes in an argument to flatten to a specific depth or flatten everything. console.log(nums.flat()); // [ 1, 2, 3, 4, [ 5 ], [ 6 ], [ [ 7 ] ] ] console.log(nums.flat(2)); // [1, 2, 3, 4, 5, 6, [7]]; console.log(nums.flat(Infinity)); // [ 1, 2, 3, 4, 5, 6, 7 ]

Filter Falsy Values in Array

Remember to filter your arrays before doing array-wise operation. Filtering falsy values must be the first operation even if you do not have specific filtering requirements. Consider this array - const nums = [1, 3, 5, undefined, 7, 9, null]; If you are in control of the data source, you can very well expect to filter only when necessary. But any kind of array operations, or data from external systems can cause all kinds of unexpected results. We have seen how unexpected values impact array sort before. As you can imagine filtering goes beyond a simple array sort. ...