Skip to content

Instantly share code, notes, and snippets.

@harbirchahal
harbirchahal / App.jsx
Last active September 8, 2025 13:27
React | Reimplementing useState hook
import useCustomState from "./useState";
export default function App() {
const [likes, setLikes] = useCustomState(0);
const [dislikes, setDislikes] = useCustomState(0);
return (
<Flex gap="small">
<Button
icon={<LikeOutlined />}
@harbirchahal
harbirchahal / my_object.ts
Last active September 8, 2025 13:38
JavaScript | Object Utility Functions
interface Indexable {
[key: string]: any;
}
function isObject(val: any): boolean {
return Object.prototype.toString.call(val) === "[object Object]";
}
function isObjectLike(val: any): boolean {
return val != null && typeof val === "object";
@harbirchahal
harbirchahal / monet_ex.ts
Last active October 24, 2022 16:17
Extends Monet js functionality
// https://monet.github.io/monet.js/
import { Observable } from 'rxjs';
import { Maybe } from 'monet';
import { filter, map } from 'rxjs/operators';
export function filterIsSome<T extends NonNullable<{}>>() {
return function (source: Observable<Maybe<T>>): Observable<Maybe<T>> {
return source.pipe(filter((maybe) => maybe.isSome()));
};
@harbirchahal
harbirchahal / computed_value.js
Last active September 8, 2025 13:39
JavaScript | Computed Value
class Observable {
constructor(val) {
this._value = val;
this._listeners = [];
}
get value() {
return this._value;
}
@harbirchahal
harbirchahal / more-less-items.directive.ts
Last active September 8, 2025 13:39
Angular | Show more/less Directive
import {
AfterContentInit,
ContentChildren,
Directive,
ElementRef,
Input,
QueryList,
Renderer2,
} from '@angular/core';
import { startWith } from 'rxjs/operators';
@harbirchahal
harbirchahal / _mixins.scss
Created June 2, 2022 18:55
Mixins in scss styles
@mixin flex($dir: row, $justify: start, $align: start, $wrap: wrap) {
display: flex;
@if $dir != row {
flex-direction: $dir;
}
@if $wrap != wrap {
flex-wrap: $wrap;
}
@if $justify != start {
justify-content: $justify;
@harbirchahal
harbirchahal / stack_based.js
Created May 21, 2022 06:50
Next greater or smaller element in array
/*
Next greater element left or right
Next smaller element left or right
*/
// [1, 3, 2, 4] => [-1, 1, 1, 2]
// [1, 3, 0, 0, 1, 2, 4] => [-1, 1, -1, -1, 0, 1, 2]
function nextSmallerLeft(nums) {
const ans = [];
const stack = [];
@harbirchahal
harbirchahal / single_linked_list.js
Last active May 2, 2022 10:26
Single LL Operations
/**
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
function length(head) {
if (head) {
let count = 1;
@harbirchahal
harbirchahal / array_fp_util.js
Last active September 8, 2025 13:41
JavaScript | Composable Array Functions
/*
Ref: https://monet.github.io/monet.js/
Avoids intermediate creation of resultant array.
*/
const { Some, None } = Monet;
function map(fn) {
return function (v) {
return Some(fn(v));
}
@harbirchahal
harbirchahal / try.js
Last active September 8, 2025 13:40
JavaScript | Try as Optional Type
class Try {
constructor(val) {
this._val = val;
}
static of(fn) {
try {
return new Success(fn());
} catch (err) {
return new Failure(err);