6
\$\begingroup\$

Background

A VSCode extension that uses Chance.JS to generate random data types.

demo

Concerns

  1. My main concern is that there are 4 levels of function references that feels like a hack. From registerCommand → insertFunc* → randomFunc* → processSelection.
  2. If the code is sensible TypeScript/JavaScript

The Code

export function activate(context: vscode.ExtensionContext) {
    context.subscriptions.push(vscode.commands.registerCommand('randomeverything.int', insertRandomInt));
    context.subscriptions.push(vscode.commands.registerCommand('randomeverything.letters', insertRandomLetters));
    context.subscriptions.push(vscode.commands.registerCommand('randomeverything.lettersAndNumbers', insertRandomLettersAndNumbers));
    context.subscriptions.push(vscode.commands.registerCommand('randomeverything.iPv4Address', insertRandomIPv4Address));
    context.subscriptions.push(vscode.commands.registerCommand('randomeverything.iPV6Address', insertRandomIPV6Address));
    /* More commands... */
}

function insertRandomInt(): void {

    var max:number;
    var min:number;

    Window.showInputBox({prompt: "Please enter [MIN-MAX]", value:"1-100"}).then(
        function(txt){
            if(txt){
                var args = txt.split("-");

                min = Number.parseInt(args[0]);
                max = Number.parseInt(args[1]);

                if(args.length != 2 || isNaN(min) || isNaN(max))
                {
                    Window.showErrorMessage("Invalid format.");
                    return;
                }
                processSelection(randomIntString, [min, max]);
            }
    });

}

function insertRandomLetters(): void {
    processSelection(randomLetters, []);
}

function insertRandomLettersAndNumbers(): void {
    processSelection(randomLettersAndNumbers, []);
}

function insertRandomIPv4Address(): void {
    processSelection(randomIP, ['ipv4']);
}

function insertRandomIPV6Address(): void {
    processSelection(randomIP, ['ipv6']);
}
/* More inserters... */

/**
 * Chance.js Wrappers
 */
function randomIntString(min, max): string {
    var chance = require('chance').Chance();
    var randomVar: Number = chance.integer({
      min: min,
      max: max
    });

    return randomVar.toString();
}

function randomIP(option?:string): string{
    var chance = require('chance').Chance();
    var randomVar:string;

    switch (option) {
        default:
        case 'ipv4':
            randomVar = chance.ip();
            break;
        case 'ipv6':
            randomVar = chance.ipv6();
            break;
    }

    return randomVar;
}

/* More wrappers... */

function processSelection(formatCB, argsCB) {

    let e = Window.activeTextEditor;
    let d = e.document;
    let sel = e.selections;

    e.edit(function (edit) {
        // iterate through the selections
        for (var x = 0; x < sel.length; x++) {
            let txt: string = d.getText(new Range(sel[x].start, sel[x].end));

            if (argsCB.length > 0) {
                txt = formatCB.apply(this, argsCB);
            } else {
                txt = formatCB();
            }

            //insert the txt in the start of the current selection
            edit.insert(sel[x].start, txt);
        }
    });
}
\$\endgroup\$
4
\$\begingroup\$

As for the architecture it seems good to me.

As for the TypeScript I have a few minor suggestions:

Avoid using var, use const or let instead.

function randomIntString(min: number, max: number): string {
    const chance = require('chance').Chance();
    const randomVar: number = chance.integer({
      min: min,
      max: max
    });

    return randomVar.toString();
}

It is also preferable to use let for for-cycle control variables.

for (let x = 0; x < sel.length; x++) {
   // body
}

It would help readability and maintainability if you specified the types of all the arguments of all your functions.

function randomIntString(min: number, max: number): string {
    // body
}

Also prefer using lower case type names for built-in primitive types (e. g. number over Number) when specifying the type of something.

You could specify the IP option type more precisely (asuming you use TypeScript >= 2.0). This will help you prevent typos and also improves readability (the code is more self-documenting):

type IpAddressType = 'ipv4' | 'ipv6';
function randomIP(option?: IpAddressType): string{
    // body stays the same
}

When the property of the object being initialized has the same name as the variable assigned to it you can shorten it like so (see the chance.integer call):

var randomVar: number = chance.integer({
      min,
      max
    });
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.