Skip to content

Instantly share code, notes, and snippets.

View harbirchahal's full-sized avatar

Harbir Chahal harbirchahal

View GitHub Profile
@harbirchahal
harbirchahal / my_object.ts
Last active January 19, 2023 17:35
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 October 16, 2022 14:58
Computed value with Observable chains
class Observable {
constructor(val) {
this._value = val;
this._listeners = [];
}
get value() {
return this._value;
}
@harbirchahal
harbirchahal / more-less-items.directive.ts
Last active October 16, 2022 15:04
Show more/less button to toggle extra list items
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
Created June 22, 2021 11:31
Composable array functions that avoid intermediate creation of resultant array.
/*
Ref: https://monet.github.io/monet.js/
*/
const { Some, None } = Monet;
function map(fn) {
return function (v) {
return Some(fn(v));
}
}
@harbirchahal
harbirchahal / try.js
Last active May 22, 2021 14:06
Try functional data type
class Try {
constructor(val) {
this._val = val;
}
static of(fn) {
try {
return new Success(fn());
} catch (err) {
return new Failure(err);
@harbirchahal
harbirchahal / buffer_iterator.js
Created May 17, 2021 08:11
ES6: Buffer Iterator
function BufferIterator(collection, bufferSize) {
this[Symbol.iterator] = function () {
let nextIndex = 0;
return {
next: () => {
if (nextIndex < collection.length) {
const buffer = new Array(bufferSize);
for (let i = 0; i < bufferSize; i++) {
buffer[i] = collection[nextIndex++];