Skip to content

Instantly share code, notes, and snippets.

@zzpmaster
zzpmaster / formatBytes.dart
Last active April 21, 2025 17:26
convert bytes to kb mb in dart
static String formatBytes(int bytes, int decimals) {
if (bytes <= 0) return "0 B";
const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
var i = (log(bytes) / log(1024)).floor();
return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) +
' ' +
suffixes[i];
}
@zzpmaster
zzpmaster / main.dart
Last active June 13, 2023 12:59
flutter ModalBottomSheet update state
showModalBottomSheet(
context: context,
builder: (context) {
return ModalBottomSheet(this.selected, this.data, (selected, data) {
this.selected = selected;
this.data = data;
});
});
@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({
#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 / 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
@zzpmaster
zzpmaster / number-mask.directive.ts
Last active September 13, 2021 09:59
Angular Directive - input框输入数字,自动千分位分割
import { CommonUtils } from 'src/shared/utils/common-utils';
import { DecimalPipe } from '@angular/common';
import {
Directive,
ElementRef,
forwardRef,
HostListener,
Input,
OnInit,
@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';