Skip to content

Instantly share code, notes, and snippets.

View ashalfarhan's full-sized avatar
💻
Working Remotely

Ashal Farhan ashalfarhan

💻
Working Remotely
View GitHub Profile
@ashalfarhan
ashalfarhan / array-utils.ts
Last active October 22, 2023 10:00
Utilities
function chunk(arr = [], size = 1) {
const compact = arr.slice(0, arr.length - (arr.length % size));
const chunks = [];
for (let i = 0; i < compact.length/ size; i++) {
let start = i === 0 ? 0 : i * size;
let end = i === 0 ? size : i * size + size;
chunks.push(arr.slice(start, end))
}
chunks.push(arr.slice(compact.length, arr.length));
return chunks
import { useEffect, useRef, useState } from 'react';
type GeoState =
| {
status: 'idle';
}
| {
status: 'success';
state: GeolocationPosition;
}
@ashalfarhan
ashalfarhan / type-utils.ts
Last active November 9, 2024 12:36
Typescript type utilities
// Used for flatten complex object type
type Flatten<T> = {
[K in keyof T]: T[K];
};
// Set K as required
type RequiredPick<T, K extends keyof T> = Flatten<
{
[P in K]-?: T[P];
} & Omit<T, K>