Categories
Programming

HTML custom input with type hidden and number1 min read

I don’t think it is possible to a create an element like

<input type="hidden | number" />

Solution is to create a custom element like this

<hidden-num value="0"></hidden-num>

MDN – Using Custom ELements (https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)

class InputHiddenNum extends HTMLElement {
    constructor() {
        super();
    }
    connectedCallback() {
        this.style.display = "none";
    }
    get value() {
        return this.intern_value;
    }
    set value(v) {
        this.intern_value = typeof v !== "number" ? parseFloat(v) : v;
        this.setAttribute("value", this.intern_value.toString());
    }
}

customElements.define("hidden-num", InputHiddenNum);

To add a property of value to our <hidden-num> element, we must add the get value()  and set value(val)  methods.

An attribute of value is also added to element if the value property is changed. A number  value is returned if the elem.value  is accessed.

Custom element is registered with

customElements.define(“hidden-num”, InputHiddenNum);

Built in element can also be extended like

<input is=”hidden-num” />

Refer – MDN – Customized built-in Elements (https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#Customized_built-in_elements)

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

Electron: Share sessionStorage between window and webView?1 min read

I wanted to declare global const/var that were specific for an app instance.

Here is a scenario –

  • The app asks for username as soon as it starts and saves it for use unitil the app is closed.
  • If the app was run two times, I didn’t want these instances to have the same username if different usernames were chosen at the start. That is, if I change the username in one instance, didn’t want it to change in the other instance.
  • Also inside the app instance I wanted the saved username to be available in all created webViews and newly created BrowserWindows.

My options

  • localStorage – Naaah, it would be same for all app instances.
  • sessionStorage –  No, it is not shared with webViews and windows created inside the app.
  • global – Yessss, thats it.

How to use it?

Inside the initial script declare this

global.settings = {
    user_name: null
};

(Note- You do not have to use “settings” , you can use whatever you want)

Now in the initial screen, I can do this to change the value

const {
  remote
} = require('electron');

remote.getGlobal('settings').user_name = 'The selected username';

And that’s it.