Skip to content

Instantly share code, notes, and snippets.

View ashalfarhan's full-sized avatar
💻
Remotely working

Ashal Farhan ashalfarhan

💻
Remotely working
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 October 18, 2023 11:09
Typescript type utilities
// Used for making complex object type
// to be plain object
type Flatten<T> = {
[K in keyof T]: T[K];
};
// Partially making field `K` required,
// and should be the keyof `T`
type RequiredPick<T, K extends keyof T> = Flatten<
{