Skip to content

Instantly share code, notes, and snippets.

View deepakshrma's full-sized avatar
🎯
Focusing

Deepak Vishwakarma deepakshrma

🎯
Focusing
View GitHub Profile
@deepakshrma
deepakshrma / conditionalBreak.js
Created January 20, 2020 15:07
[conditionalBreak] Weird Part: How to break the LOOP in JavaScript
function conditionalBreak() {
let x = 0;
let z = 0;
while (true) {
console.log("Outer loops: " + x);
x += 1;
z = 1;
let breakOuterLoop = false;
while (true) {
console.log("Inner loops: " + z);
@deepakshrma
deepakshrma / tillWhen.js
Last active January 20, 2020 15:10
[tillWhen] Weird Part: How to break the LOOP in JavaScript
const _ = {};
// fn: (any[], function, () => boolean) -> any[]
_.tillWhen = (data, fn, predicate) => {
if (!data || !Array.isArray(data)) return data;
let index = 0;
let arr = [];
do {
arr.push(fn(data[index]));
} while (index < data.length && !predicate(data[index++]));
return arr;
@deepakshrma
deepakshrma / sort_table.js
Created March 19, 2020 15:22
Sort any table
const sort_table = (
sortColNum,
id = "#table_data",
isLastRowTable = true,
formatter = td => td.textContent,
reIndex = 1
) => {
let elm = document.querySelector(id);
if (!elm) {
document.querySelector("table").tBodies[0].setAttribute("id", "table_data");
@deepakshrma
deepakshrma / build_tree.js
Last active April 4, 2020 04:26
Create tree out of sorted data.
const source = [
{ Id: "1", Name: "abc", Parent: "", attr: "abc" },
{ Id: "2", Name: "abc", Parent: "1", attr: "abc" },
{ Id: "3", Name: "abc", Parent: "2", attr: "abc" },
{ Id: "4", Name: "abc", Parent: "2", attr: "abc" },
];
function tree(data, id, pId) {
const [result] = data.reduce(
([r, map], item) => {
const d = { ...item, children: [] };
const permutation = text => {
return permute("", text);
};
const permute = (prefix, sufix, arr = []) => {
if (sufix.length == 0) arr.push(prefix);
else {
for (let i = 0; i < sufix.length; i++) {
permute(
prefix + sufix.charAt(i),
sufix.substr(0, i) + sufix.substr(i + 1),
const tree = {
name: "A",
children: [
{ name: "A-1", children: [{ name: "A-1-A" }, { name: "A-1-B" }] },
{
name: "B-1",
children: [
{ name: "B-1-A", children: [{ name: "B-11-A" }, { name: "B-11-B" }] },
{ name: "B-1-B" }
]
@deepakshrma
deepakshrma / stackoverflow.js
Last active April 2, 2020 13:48
stackoverflow
const snippets = [
{
scope: "javascript,typescript",
prefix: "st_key_map",
body: `const mapKeys = (obj, fn) => {
let mapper = fn;
if (typeof fn === "string") mapper = x => x[fn];
return Object.keys(obj).reduce((acc, k) => {
acc[mapper(obj[k], k, obj)] = obj[k];
return acc;
class TicTac {
constructor() {
this.mat = [
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
];
}
move(i, j, m) {
m ? this.moveX([i, j]) : moveO([i, j]);
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="app.css">
<style>
td {
border: 1px solid;
@deepakshrma
deepakshrma / nodejs_init.js
Last active December 28, 2023 09:41
Init nodejs config like[prettier, husky]
/*
// How to use
curl -s https://gist.githubusercontent.com/deepakshrma/c77d297d5ff3e07ce172da2893bb49d0/raw/835f004cf234cf3a79fab1bd1b37df7db861773b/nodejs_init.js -O nodejs_init.js || node nodejs_init.js
*/
const path = require("path");
const fs = require("fs");
const cwd = process.cwd();
const fromRoot = (...args) => path.join(cwd, ...args);