Categories
Programming

Electron open file explorer with file selected43 sec read

After creating a file and writing it to the disk, its nice to show the user the location of the newly created file in the system file explorer.

At the time of writing this there is no option in electron for opening the system file explorer with a file selected. So, like every programmer does I put the title of this post into the google search bar and found this

https://discuss.atom.io/t/opening-the-oss-file-explorer/24348

So this is what I did

function open_file_exp(fpath) {
  var command = '';
  switch (process.platform) {
    case 'darwin':
      command = 'open -R ' + fpath;
      break;
    case 'win32':
      if (process.env.SystemRoot) {
        command = path.join(process.env.SystemRoot, 'explorer.exe');
      } else {
        command = 'explorer.exe';
      }
      command += ' /select,' + fpath;
      break;
    default:
      fpath = path.dirname(fpath)
      command = 'xdg-open ' + fpath;
  }
  console.log(command);
  child_process.exec(command, function(stdout) {
    //Do something if you really need to
  });
}

 

Categories
Programming

GST(Indian) check digit validation algorithm2 min read

GST number validation is done through 2 steps

  1. Check if there is a character in position of a character and a number in position of a number.
  2. Validate the check digit that is the last character in the GST no. with  Luhn mod N algorithm (https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm)

An implementation in javascript

let GST = {
  /*
      Luhn mod N algorithm    
      https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm
  */
  chars: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  pattern: /[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9A-Za-z]{1}[Zz1-9A-Ja-j]{1}[0-9a-zA-Z]{1}/,

  generate_check_charcter: function(input) {
    factor = 2;
    sum = 0;
    n = this.number_of_valid_input_chars();

    for (i = input.length - 1; i >= 0; i--) {
      code_point = this.code_point_from_char(input[i]);
      addend = factor * code_point;

      factor = (factor == 2) ? 1 : 2;

      addend = (addend / n) + (addend % n);
      sum += Math.floor(addend);
    }

    remainder = sum % n;
    check_code_point = (n - remainder) % n;

    return this.char_from_code_point(check_code_point);
  },

  validate_check_character: function(input) {
    factor = 1;
    sum = 0;
    n = this.number_of_valid_input_chars();

    for (i = input.length - 1; i >= 0; i--) {
      code_point = this.code_point_from_char(input[i]);
      addend = factor * code_point;

      factor = (factor == 2) ? 1 : 2;

      addend = parseInt(addend / n) + (addend % n);
      sum += addend;
    }

    remainder = sum % n;

    return (remainder == 0);
  },

  number_of_valid_input_chars: function() {
    return this.chars.length;
  },

  code_point_from_char: function(input) {
    let str = this.chars;
    for (let i = 0, q = str.length; i < q; i++) {
      if (input == str[i]) {
        return i;
      }
    }
  },

  char_from_code_point: function(input) {
    let str = this.chars;
    for (let i = 0, q = str.length; i < q; i++) {
      if (input == i) {
        return str[i];
      }
    }
  },

  /*
      Function called to check if a GSTno is valid.
      Full GST.no is passed to the func
  */
  is_valid_num: function(gst_no) {
    //Check for pattern
    var patt = new RegExp(this.pattern);
    if (!patt.test(gst_no)) {
      console.log('pattern wrong');
      return false;
    }

    //Validate the check digit
    return this.validate_check_character(gst_no);
  }

};

USAGE:

GST.is_valid_num(gst_no);

true  is returned if the GST.no is valid. false  if not.

Comment below if you have any questions.

Categories
Programming

Creating a Javascript load more plugin part 2 – Searching2 min read

In the first part Creating a Javascript load more plugin part 1 , we have created a javascript plugin which loads data from an API page. Let’s build upon on it.

Today we are going to add search option to our plugin, children ? .

Objectives ?

  • Add search bar to the page.
  • On input on search bar, load in relevant content from the API.
  • The API should take in a search parameter and provide relevant results.

As of now we are sending two parameters to the API – start and count. We would add one more q for sending the search query.

For learning how add searching option at the server end, please refer this tutorial from Codecourse – Basic PHP & MySQL Searching.

It uses PHP and MYSQL but you get the idea ? .

Demo page – Javascript load more plugin – search added.

We have to add one more line to our previous HTML code to include the search bar.

<input type="search" autocomplete="off" class="finder" />

We can add this anywhere inside our main container. Only condition is that it should have a class of finder, so that our plugin can identify it.

For styling HTML forms and form elements view this tutorial – Styling HTML Forms .

Updating the Plugin

Now let’s upgrade our plugin. First we would find the search bar.

//Search bar
this.finder = this.elem.getElementsByClassName('finder').length ? this.elem.getElementsByClassName('finder')[0] : false;

If you are not able to follow, please go through part one of this series.

Now, inside our lmore.load method we would append the search term to the GET query string.

//create query string with
//start and count variables
var qs = this.source + '?lmore_start=' + this.start + '&lmore_count=' + this.count;

//Send search query if search bar is available
qs += this.finder ? '&q=' + this.finder.value.trim() : '';
this.http.open('GET', qs);
this.http.send();

Let’s add and event listener to the search bar so that, data can be loaded while searching.

We could load data on any event, but I prefer the on-input event because it gives an instant search effect.

I don’t like search bars that have a button which has to be clicked to search. We are in 2016, common guys ( who make me click a button to search ? )  ?.

We could even pass the event, as input for the plugin.

if (this.finder) {
  this.finder.addEventListener('input', function() {
    this.load();
  }.bind(this));
}

You could also use this plugin to give search suggestions like google. Maybe we could use the plugin two times, one for suggestions and one for showing search results.

Comment below if you want to see this plugin give you search suggestions.