Skip to content

Instantly share code, notes, and snippets.

View wilsoncook's full-sized avatar

Wilson Zeng wilsoncook

  • Alibaba Inc.
  • China
View GitHub Profile
@wilsoncook
wilsoncook / LazyObject.ts
Created December 24, 2020 13:23
懒对象:该对象只有在被使用(被访问/设置/删除/遍历属性等)时才会被实例化
/**
* 构建一个懒对象:该对象只有在被使用(被访问/设置/删除/遍历属性等)时才会被实例化
* 其他方案:采用getter方式,如:module.exports = { get XXX() { return new ClsXXX(); } };
* @param factory 实例化函数
*/
function unstable_makeLazyInstance<T>(factory: () => T): T {
let instance: T;
function wrap<H extends (...args: any[]) => any>(handle: H): H {
return ((...args: any[]) => {
@wilsoncook
wilsoncook / LazyCache.ts
Created December 24, 2020 13:20
全局性的数据流懒缓存,应用场景:部分逻辑代码依赖数据流中的“数据 + 转换逻辑”形成的结果数据,而在使用的时候需要以mutable的形式按需使用(即虽然依赖数据流数据,但该数据变更不会触发对应逻辑重新执行,只有特定的其他依赖会导致其执行),避免频繁转换
import { Store, Unsubscribe } from 'redux';
export class LazyCache<V = any, S = any, M = any> {
/** 最后一次更新并转换后的数据 */
private value: V;
/** 当前selector后的值,用于作变更对比 */
private currentSelected: M | S;
/** 标记对应数据流是否有变更,在使用方使用数据的时候,便可重新生成 */
@wilsoncook
wilsoncook / makeLazyComponent.ts
Last active January 25, 2021 03:19
Allow define React Component with async dependencies
/**
* Allow define React Component with async dependencies
* @param defineClass Return a new class that depends on deps
* @param asyncDeps Define what the class depends on
*/
export function makeDepsComponent<T extends React.ComponentType<any>, D>(
defineClass: (deps: D) => T,
asyncDeps: () => Promise<D>,
) {
return makeLazyComponent(async () => defineClass(await asyncDeps()));
@wilsoncook
wilsoncook / immutableDefaultsDeep.ts
Last active July 16, 2020 13:33
Fill target with default values by source via immutable, better than fp.defaultsDeep()
/**
* Fill target with default values by source via immutable
* Benefits: unlike fp.defaultsDeep(), we don't deep clone target to get the real immutable.
* @param target target
* @param source source
* @param options.ignoreArray whether append elements to array
*/
export function immutableDefaultsDeep(target: any, source: any, options?: { ignoreArray?: boolean }): any {
const targetType = typeof target;
const sourceType = typeof source;
@wilsoncook
wilsoncook / watchObject.ts
Last active February 4, 2021 06:11
Monitor a object's properties modifications or reads deeply(用于监控对象属性更改的工具方法(支持无限层级,循环引用),可以非常快速地追溯Mutable更改(可保留执行栈))
export class WatchObjectOptions {
/** How deep do we put a proxy */
depth? = 10;
nameKey? = Symbol('$keyName');
nameParent? = Symbol('$parent');
// nameAppliedProxy? = Symbol('$appliedProxy');
@wilsoncook
wilsoncook / deepClone.ts
Last active April 12, 2018 12:07
Deep clone for javascript
/**
* Simple clone JSON-like object (No circular reference, No class inherit, Plain object)
* [NOTE] Not tested on all cases
* @param obj object to be clone
* @param excludeFields exclude fields that will ignored copying
*/
export function deepClone<T>(obj: any, excludeFields?: string[]) {
if (obj === null || typeof obj !== 'object') { return obj; }
if (Object.prototype.toString.call(obj) === '[object Array]') {
return obj.map((element) => deepClone(element, excludeFields));
@wilsoncook
wilsoncook / recursive-array.ts
Last active May 9, 2018 09:42
Recursive a array, support addson operations: filter, map, restore
/**
* Recursively iterate table data rows, if iterator() returns false, then will stop iterating
* @param rows Data list which may contain children
* @param iterator Iterator, return false to stop iterating
* @param childrenField Specify the children field's key
* @param _parent [Private] The parent of the rows, null by default, used for iterator() ONLY
* @return If iterated all items, this method will return true
*/
function recurseRows<T>(
rows: T[],
@wilsoncook
wilsoncook / input-boolean-angular.ts
Created March 30, 2018 04:45
Angular simple boolean inputing converting decorator
/**
* Input decorator that handle a prop to do get/set automatically with toBoolean
*
* Why not using @InputBoolean alone without @Input? AOT needs @Input to be visible
*
* @howToUse
* ```
* @Input() @InputBoolean() visible: boolean = false;
*
* // Act as below:
@wilsoncook
wilsoncook / lfu-cache.cpp
Created February 9, 2017 07:25
LFU缓存算法-简单实现
/**
* Author: Wilson Zeng
*/
#define NULL 0
class Element {
public:
int key;
int value;
@wilsoncook
wilsoncook / hashtable.cpp
Last active February 7, 2017 08:45
C版哈希表(Hash Table)的简单实现(支持自动扩容)
/**
* Author: Wilson Zeng
*/
#include <stdio.h>
#include <cstdlib>
#include <string.h>
using namespace std;
// DECLARATIONS