Skip to content

Instantly share code, notes, and snippets.

@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();
}
});
@av1v3k
av1v3k / gist:be4b18cf9d638b75d83f5567072b7bcd
Created February 25, 2024 16:11
How to create angular project with specific angular cli ?
npx @angular/cli@16 new app --directory=./
npx @angular/cli@8 new app --directory=./
@av1v3k
av1v3k / future.delayed.dart
Created August 30, 2023 15:23
Usage of delayed() method with Future; Duration() in dart.
//use of async, await, futures in dart
import 'dart:async';
void main() async {
var data = await get('hi');
print(data);
}
@av1v3k
av1v3k / time-remaining.dart
Created August 30, 2023 14:44
Time remaining in dart
void main() {
var to = DateTime(2023, 8, 31, 0, 0);
DateTime now = DateTime.now();
var d = now.difference(to);
print(to);
print(now);
var remainingtime = d.abs();
print('$remainingtime');
}
@av1v3k
av1v3k / isomorphic.js
Created April 28, 2023 06:20
compare 2 string are isomorphic #code #isomorphic
// Globals
// DOM elements
const stringOne = document.querySelector("#string-one");
const stringTwo = document.querySelector("#string-two");
const message = document.querySelector("#result");
// Initialize application
function isomorphic() {
@av1v3k
av1v3k / immutability.md
Created April 18, 2023 09:58
Importance of Immutability in JavaScript

Importance of Immutability in JavaScript

What is the good part of the immutability? Why do we need to care about the immutability in the first place? Why even bother?

Immutability gives stricter control over your data immediately making your code safer and more predictable. In other words, immutable objects allow you to control the interface and data flow in a predictable manner, discovering the changes efficiently. It also makes it easier to implement complex features such as undo/redo, time travel debugging, optimistic updates and rollback, and so on.

If we talk about the frontend library React, Vue os state management library Redux then immutability can also help achieve better performance by enabling quick and cheap comparisons between versions of the state before and after changes. Components can take advantage of this and intelligently re-render itself only when needed. This can significantly boost performance.

The main benefits of immutability are predictability, performance, and better mutation

@av1v3k
av1v3k / date.sh
Created March 27, 2023 22:24
Current Timestamp in different Timezone
TZ=Asia/Kolkata date +'%Y-%m-%d-%H-%M-%S'
@av1v3k
av1v3k / comp.ts
Created March 7, 2023 06:45
Validating with Service call on typing text - Angular
searchSubject = new Subject();
searchResult$: Observable<any>;
this.searchResult$ = this.searchSubject.pipe(
debounceTime(1000),
distinctUntilChanged(),
switchMap((searchText: string) => {
return this.service.validateZipCode(searchText)