Skip to content

Instantly share code, notes, and snippets.

@av1v3k
av1v3k / pick_omit.js
Created September 20, 2025 06:03
Difference between Pick and Omit in Typescript
interface Address {
street: string;
area: string;
landmark: string;
pincode: number;
}
type previewAddress = Pick<Address, 'pincode' | 'area'>;
type previewAddress2 = Omit<Address, 'pincode'>;
@av1v3k
av1v3k / pagination.js
Created August 23, 2025 03:02
MongoDB pagination advanced - to return totalcount, next page, last page, etc,....
// monogodb
ITEMS_PER_PAGE = 2;
exports.getIndex = (req, res, next) => {
let page = req.query.page;
let totalItems;
Product.find()
.count()
@av1v3k
av1v3k / pagination.js
Created August 23, 2025 01:59
Mongodb DB Pagination logic
// monogodb
ITEMS_PER_PAGE = 2;
exports.getIndex = (req, res, next) => {
let page = req.query.page;
Product.find()
.skip((page - 1) * ITEMS_PER_PAGE)
.limit(ITEMS_PER_PAGE)
@av1v3k
av1v3k / validator.js
Created August 12, 2025 06:31
validator pattern for email in angular forms
Validators.pattern(/^(?=.*[a-zA-Z])[a-zA-Z\s]+(?:-[a-zA-Z\s]+)*[a-zA-Z]$/)
@av1v3k
av1v3k / reactive-form-validation.js
Created July 31, 2025 15:45
To validate a form field using angular's reactive form
this.form.get('optionalField').valueChanges.subscribe(value => {
const control = this.form.get('optionalField');
if (value && value.length > 0) {
// If user enters any character, add 'required' validator
control.setValidators([Validators.required]);
} else {
// Otherwise, no validator (optional)
control.clearValidators();
}
control.updateValueAndValidity({emitEvent: false});
@av1v3k
av1v3k / index.js
Created July 16, 2025 17:57
nodejs file system : copy entire folder and child folders to another folder
import {readdir, cp} from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const currentPath = dirname(fileURLToPath(import.meta.url));
const currentDir = await readdir(currentPath + '/browser', {withFileTypes: true});
const desitination = join(currentPath, 'server');
const source = join(currentPath, 'browser', 'assets');
await cp(source, desitination + '/assets', {recursive: true});
@av1v3k
av1v3k / react_custom_hook.js
Created July 3, 2025 12:23
custom hook for API call using react
//App.js
import React from 'react';
import './style.css';
import { fetchUsers } from './custom_hook.js';
export default function App() {
const { data, loading, error } = fetchUsers(
'https://jsonplaceholder.typicode.com/posts'
);
@av1v3k
av1v3k / authgaurd.js
Created June 5, 2024 14:27
authguard as function in angular 16 - example
export const AuthGuard: CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
const store = inject(Store);
return new Promise<boolean>(resolve => {
const sub = store.select(getAgeSelector).subscribe(res => {
if (res && res?.token && res?.token?.length) {
resolve(true);
} else {
window.open(environment.WHATEVER, '_SELF');
resolve(false);
}
@av1v3k
av1v3k / removenested.js
Created March 20, 2024 09:58
remove undefined, empty string, empty array key values in Object
function deepCopy(obj) {
if (typeof obj !== 'object' || obj === null) {
return obj; // Return primitive values and null as is
}
const copy = Array.isArray(obj) ? [] : {};
for (let key in obj) {
copy[key] = deepCopy(obj[key]);
}
@av1v3k
av1v3k / form.js
Last active March 8, 2024 02:37
re-trigger Validation in angular form after patching
Object.values(this.anyform.controls).filter(control => {
if (control.invalid) {
control.patchValue(control?.value?.length === 0 ? ' ' : control?.value);
control.markAsDirty();
control.markAsTouched();
control.setErrors({ incorrect: true });
control.updateValueAndValidity();
}
});