Skip to content

Instantly share code, notes, and snippets.

View vishnuroshan's full-sized avatar
:shipit:
maybe the bug is actually a feature

Vishnu Roshan vishnuroshan

:shipit:
maybe the bug is actually a feature
View GitHub Profile
empty
@vishnuroshan
vishnuroshan / groupBy.js
Created August 22, 2026 03:02
group by pollyfil
Array.prototype.grpBy = function (grpKey) {
const array = this
if (array.length < 1) return {};
const groupedObj = {};
array.forEach((item) => {
const propertyKey = item[grpKey];
if (groupedObj[propertyKey]) {
groupedObj[propertyKey].push(item)
} else {
groupedObj[propertyKey] = [item]
@vishnuroshan
vishnuroshan / deepcopy.js
Last active August 19, 2026 04:21
Deepcopy and flatten array
function deepCopy(obj) {
if (obj == null || typeof obj !== 'object') return obj;
if (Array.isArray(obj)) {
return obj.map((item) => deepCopy(item));
}
let copy = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = deepCopy(obj[key])
}
@vishnuroshan
vishnuroshan / sort_array.js
Created November 16, 2024 07:00
Devi's solution to sort
function sortBy(arr, fn= ()=>{}) {
const array = [...arr];
for (let i=0; i<array.length; i++) {
for (let j=i+1;j<array.length;j++){
if (array[j] < array[i]) {
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
// Definition of Leader: Any Element which is greater than all the values to its right
function findLeadersInArray(arr) {
const leaders = [];
arr.forEach((a, i)=> {
if (i=== arr.length-1) {
leaders.push(a);
return leaders;
}
@vishnuroshan
vishnuroshan / cycle.js
Last active March 20, 2024 14:33
Find number of rotations needed for the strings to match
function cycle(a, b) {
if (a.length !== b.length) return 0;
const arr = a.split('');
const temp = [...arr]; // i am copying the array.
let count = 0;
for(let e of arr) {
count += 1;
console.log(temp.join(''));
const firstElement = temp.shift(); // this removes the 1st element of the array
temp.push(firstElement);
@vishnuroshan
vishnuroshan / types.ts
Created August 11, 2023 10:40 — forked from ClickerMonkey/types.ts
Typescript Helper Types
// when T is any|unknown, Y is returned, otherwise N
type IsAnyUnknown<T, Y, N> = unknown extends T ? Y : N;
// when T is never, Y is returned, otherwise N
type IsNever<T, Y = true, N = false> = [T] extends [never] ? Y : N;
// when T is a tuple, Y is returned, otherwise N
// valid tuples = [string], [string, boolean],
// invalid tuples = [], string[], (string | number)[]
const sum = (a) => (b) => b === undefined ? a : sum(a+b);
const arr = []
arr[0] = sum(1)(2)(3)(); // output: 6
arr[1] = sum(1)(2)(3)(1)(1)(2)(2)(10)(); // output: 22
console.log(arr);
@vishnuroshan
vishnuroshan / useState.js
Created June 9, 2023 12:50
A very crude imagination of how useState works internally in react (definitely wrong)
function useState(initVal) {
let _val = initVal;
let state = () => _val;
const setState = (newVal) => _val = newVal;
return [state, setState]
}
const [c, setC] = useState(1);
console.log(c());
setC(2);
@vishnuroshan
vishnuroshan / TS-generics-custom-array-every-method.ts
Last active July 5, 2023 12:11
Typescript custom array methods with generics. this is a exercise to understand how generics work and where to use them in real life
interface Array<T> {
customEvery(callback: (each: T, index?: number, array?: Array<T>) => boolean): boolean;
}
Array.prototype.customEvery = function(callback) {
let res= []
for (let i = 0; i < this.length; i++) res.push(callback(this[i], i, this));
return res.includes(false) ? false : true
}