Skip to content

Instantly share code, notes, and snippets.

@zzpmaster
zzpmaster / amount-mask.directive.ts
Last active July 14, 2021 05:10
Transforms a number to a currency string and Currency thousandth format in angular form.
@Directive({
selector: '[formControlName][appAmountMask]'
})
export class AmountMaskDirective implements OnInit {
private hasFocus = false;
private element: HTMLInputElement;
private digits = '1.0-7';
@zzpmaster
zzpmaster / types.ts
Created August 26, 2021 05:21
typescript types
interface Person<Type> {
id: Type extends "A" ? number : string;
name?: string;
}
/**
* a
*/
type Category = "A" | "B"
@zzpmaster
zzpmaster / index.ts
Last active December 21, 2022 02:35
Watch for Object Changes with JavaScript by Typescript
// Event Source
type propEvent<Type> = {
on<Key extends string & keyof Type>(
eventName: `${Key}Changed`,
callback: (newValue: Type[Key]) => void): void;
};
function makeWatchedObject<Type>(obj: Type): Type & propEvent<Type> {
const cbMap = new Map<string, Function>();
var proxy = new Proxy({
@zzpmaster
zzpmaster / guard1.service.ts
Created September 13, 2021 09:27
Angular multiple guards async.
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { tap, delay } from 'rxjs/operators';
import { of } from 'rxjs';
import { SequentialRoutingGuardService } from './sequential-routing-guard.service';
@Injectable({
providedIn: 'root'
})
export class Guard1Service implements CanActivate {
@zzpmaster
zzpmaster / hello.component.html
Last active November 3, 2021 05:13
Angular - input formControl limit max length
<!--
使用[inputlimit], 可以使用默认值。
使用[inputlimit]="2", 可以设置要check的长度。
note that: 不能直接使用 inputlimit或inputlimit="2" 会导致默认值为空或是字符串类型。
-->
<input type="text" [inputlimit]="2" formControlName="name" />
@zzpmaster
zzpmaster / index.ts
Last active November 2, 2021 10:50
Use Enum as restricted key type in Typescript
enum Foo {
A = 'a1', B = 'b1', C = 'c1'
}
// use enum value
const finish = (): Partial<{ -readonly [key in Foo]: string }> => {
return { [Foo.A]: 'hello'}; // or { 'a1': 'hello' }
}
// use enum key
#This one-liner removes all cordova installed plugins and then adds them again.
#Note: If you are not using Ionic, change 'ionic cordova plugin' to 'cordova plugin'
#! /bin/bash
cordova plugin list | cut -d ' ' -f 1 | while read plugin; do ionic cordova plugin rm $plugin && ionic cordova plugin add $plugin; done