Skip to content

Instantly share code, notes, and snippets.

View isurfer21's full-sized avatar

Abhishek Kumar isurfer21

View GitHub Profile
@isurfer21
isurfer21 / handlebars-cli.js
Last active August 12, 2023 18:50
A CLI app which compiles the template with handlebars, renders it with the data, and prints the output html to the console.
// Import the required modules
const fs = require('fs');
const path = require('path');
const handlebars = require('handlebars');
// Get the template and data file names from the command line arguments
const templateFile = process.argv[2];
const dataFile = process.argv[3];
// Check if the files exist and are valid
@isurfer21
isurfer21 / zipnsync.ps1
Created July 29, 2023 14:26
A PowerShell based CLI app to zip each folder in the source directory and paste them to the target directory.
$help = $false
if ($args -contains "-h" -or $args -contains "--help") {
$help = $true
}
if ($help) {
write-host "Usage"
write-host " zipnsync [options] <source> <target>"
write-host ""
write-host "Arguments"
@isurfer21
isurfer21 / cli-args-parser.rs
Last active July 26, 2023 08:20
Minimalistic embeddable CLI argument parser in rust without any dependencies.
use std::collections::HashMap;
use regex::Regex;
// Define a function that returns a tuple of a hashmap of flags and values and an array of non-flag arguments
fn parse_args() -> (HashMap<String, String>, Vec<String>) {
// Create a hashmap to store the flags and values
let mut flags = HashMap::new();
// Create an array to store the non-flag arguments
let mut args = Vec::new();
// Create regular expressions to match different kinds of flags
@isurfer21
isurfer21 / SnakeToCamelCaseConverter.js
Created July 11, 2023 15:56
The snake_case to camelCase converter CLI app
// Import the file system module
const fs = require('fs');
// Import the process module
const process = require('process');
// Define a function to convert snake_case to camelCase
function snakeToCamel(str) {
// Split the string by underscore
let words = str.split('_');
@isurfer21
isurfer21 / wasi.cmd
Created July 1, 2023 18:05
Download the latest wasi-sdk from https://github.com/WebAssembly/wasi-sdk/releases, extract it somewhere, create environment variable with WASI_SDK_PATH name and store the path of extracted wasi-sdk. Copy the wasi.cmd & wasi.ps1 files from given gist and paste it somewhere globally accessible. Now you can can access the wasi-sdk's executables vi…
@echo off
setlocal
set wasi_bin_path=%WASI_SDK_PATH%\bin
set wasi_self=%~n0
set wasi_cmd=%1
if "%wasi_cmd%" == "" (
echo Usage: %wasi_self% executable [arguments]
exit /b 1
)
if not exist "%wasi_bin_path%\%wasi_cmd%.exe" (
@isurfer21
isurfer21 / comment-remover.js
Created June 22, 2023 18:18
It can remove the single and multiline comments from any JavaScript or node.js files
const fs = require("fs").promises;
const path = require("path");
// A class to remove all comments from a js file
class CommentRemover {
// A constructor that takes a file name as an argument
constructor(filePath) {
// A property to store the file name
this.filePath = filePath;
// A property to store the file content
@isurfer21
isurfer21 / static-server.js
Last active June 22, 2023 15:15
Sample minimal static server
/*
@title Sample minimal static server
@file static-server.js
@setup npm install connect serve-static
@usage node static-server.js
*/
const os = require('os');
const path = require('path');
const connect = require('connect');
const serveStatic = require('serve-static');
@isurfer21
isurfer21 / ConvertArrayToCSV.js
Created April 18, 2023 11:21
This function takes an array of arrays as input and returns a CSV string. The first element of the input array is assumed to be the header row.
function convertArrayToCSV(arr) {
const arrayCopy = [...arr];
const header = arrayCopy.shift();
const csv = [
header.join(','),
...arrayCopy.map((row) => row.join(',')),
].join('\n');
return csv;
}
@isurfer21
isurfer21 / ConvertArrayOfObjectsToCSV.js
Created April 18, 2023 11:17
This function takes an array of objects as input and returns a CSV string. The keys of the first object in the input array are assumed to be the header row. If any object in the array has extra properties than others, the CSV will adjust accordingly. The updated function checks each cell value for commas, quotes, or newlines and wraps it in doub…
function convertArrayOfObjectsToCSV(data) {
const arrayCopy = [...data];
const header = Object.keys(arrayCopy[0]);
const csv = [
header.join(','),
...arrayCopy.map((row) => {
return header.map((fieldName) => {
let cellValue = row[fieldName] !== undefined ? row[fieldName] : '';
if (typeof cellValue === 'string') {
cellValue = cellValue.replace(/"/g, '""');
@isurfer21
isurfer21 / cli-arg-parser.js
Created July 28, 2022 17:05
Minimalistic embeddable argument parser in node.js without any dependencies
#!/usr/bin/env node
const APP_NAME = 'CliApp';
const APP_VER = '1.0.0';
class CliArgParser {
constructor() {
this.argv = process.argv.slice(2);
this.args = this.map();
}