Skip to content

Instantly share code, notes, and snippets.

#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
@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
@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 / 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 / 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 / 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 / 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 / clone.ts
Created June 16, 2021 02:30
clone object
function shallowCopy<T>(origin: T): T {
return Object.assign(
Object.create(Object.getPrototypeOf(origin)),
origin
)
}
function shallowCopy2<T>(origin: T): T {
const obj = { ...origin };
Object.setPrototypeOf(obj, (origin as Object).constructor.prototype);
@zzpmaster
zzpmaster / app.component.ts
Created March 22, 2021 03:10
Angular Create multi-instance HttpClient
import { Component, Inject, InjectionToken } from "@angular/core";
import { HttpClient, HttpXhrBackend } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
// import { CUSTOMER_HTTP_CLENT } from "./app.module";
export const CUSTOMER_HTTP_CLENT = new InjectionToken("xx");
@Component({
selector: "my-app",
template: `
@zzpmaster
zzpmaster / format.sh
Last active March 22, 2021 02:51
Git - Convert all files to LF (Git提交代码 - 将所有文件格式转为LF格式)
ls -r -n -file | foreach{ (cat -encoding UTF8 $_ ) -join "`n" | set-content -encoding UTF8 $_ }