Categories
Programming

Creating a simple babel plugin1 min read

What will it do?

It will change this

let content = include_str(‘path to text file’);

to this

let content = ‘content of text file’;

It works just like the rust-lang include_str!() macro.

Code

  • While looping through the AST, our code checks for a function call (CallExpression ) named include_str
  • If one is found, we derive the required file path from the first argument to the call and the path of the file that is being transpiled
  • Contents of the file are read into a string with fs.readFIleSync
  • The CallExpression  is replaced with the string which is converted into a stringLiteral

index.js

const fs = require('fs');
const Path = require('path');

module.exports = function MyPlugin(ref) {
    var t = ref.types;
    return {
        visitor: {
            CallExpression: {
                enter: function (path) {
                    if (t.isCallExpression(path.node) && path.node.callee.name === 'include_str') {
                        let file_name = this.file.hub.file.opts.filename;
                        let fpath = Path.join(Path.dirname(file_name), path.node.arguments[0].value);
                        let content = fs.readFileSync(fpath, 'UTF-8');
                        path.replaceWith(t.stringLiteral(content));
                    }
                }
            }
        }
    };
};

 

Disclaimer

Not tested

Similar libs to check out

 

 

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

RUST: non-exhaustive patterns: `&_` not covered pattern `&_` not covered21 sec read

Tried something like this

let a = "hello";

match a{
    "hello"=>{println!("hello")}
}

Error

non-exhaustive patterns: `&_` not covered pattern `&_` not covered

What I thought was wrong

I thought it meant you cannot match references

What the real error was

Just had to add all possibilities in match statement

let a = "hello";

match a {
    "hello" => println!("helloooooo"),
    _ => println!("blahh blahhh"),
}