@0x2e757/wrappers 中文文档教程
Wrappers
TypeScript 变量包装库,转译为 ES2015 JavaScript。 库包含很少的 getter,为了支持旧的 JavaScript 版本,您需要 fork 这个项目并将 getter 更改为函数。
Install
npm i @0x2e757/wrappers
Usage
库包含您感兴趣的两个类 - StaticWrapper
和 DynamicWrapper
。 还有 IStaticWrapper
和 IDynamicWrapper
接口,以及通用的 IWrapper
接口,它们对两个类都有效,但只包含那些方法在两个类中实现。
How to import
import { IWrapper, IStaticWrapper, StaticWrapper, IDynamicWrapper, DynamicWrapper } from "@0x2e757/wrappers";
StaticWrapper
可用作值容器的简单包装器,可分配更新回调(订阅者)。
使用示例:
import { StaticWrapper } from "@0x2e757/wrappers";
const wSomeValue = new StaticWrapper(0);
wSomeValue.applyMiddleware(next => value => next(value > 5 ? 5 : value));
wSomeValue.subscribe(console.log, true);
wSomeValue.set(1);
wSomeValue.set(10);
// Console output:
// > 0
// > 1
// > 5
DynamicWrapper
包含动态计算值的高级包装器(不能手动设置)。 值仅在它们将被使用之前被计算。
使用示例:
import { StaticWrapper, DynamicWrapper } from "@0x2e757/wrappers";
const wUsername = new StaticWrapper("John");
const wBalance = new StaticWrapper(100);
const wAccountInfo = new DynamicWrapper(wUsername, wBalance, (username, balance) => `${username} has ${balance}$.`);
wAccountInfo.subscribe(console.log, true);
wBalance.dec(50);
// Console output:
// > John has 100$.
// > John has 50$.
Available methods
General
包装器始终实现基础接口 IWrapperBase
:
interface IWrapperBase<T> {
* set: (value: T, debounce?: number) => void;
* setter: (value: T, debounce?: number) => () => void;
* applyMiddleware: (middleware: Middleware<T>) => void;
emit: () => T;
subscribe: (callback: Subscriber<T>, triggerImmediately?: boolean) => void;
unsubscribe: (callback: Subscriber<T>) => void;
dispose: () => void;
}
* 方法 set
、setter
和 applyMiddleware
是仅在 StaticWrapper 实例中可用。
Helpers
根据包装器值类型不同的辅助方法(其中很少有 getter 属性)可用。 它们可以修改包装器的值(仅在静态包装器中)或对值执行某些操作并返回结果。 事实上,它们都始终存在于类对象中,但出于使用 TypeScript 类型的静态类型目的,它们对 IDE 和静态代码分析器是隐藏的。
interface IPrimitiveWrapper<T> {
eq: (value: T) => boolean;
neq: (value: T) => boolean;
}
布尔值、数字和字符串被视为基本类型。
interface IBooleanWrapper {
* toggle: () => void;
}
* 方法 toggle
仅在 StaticWrapper 实例中可用。
interface INumberWrapper {
* inc: (delta?: number) => void;
* dec: (delta?: number) => void;
* random: (min: number, max: number, integer?: boolean) => void;
* round: (fractionDigits?: number) => void;
* clamp: (min: number, max: number) => void;
lt: (value: number) => boolean;
lte: (value: number) => boolean;
gt: (value: number) => boolean;
gte: (value: number) => boolean;
in: (min: number, max: number) => boolean;
}
* 方法 inc
、dec
、random
、round
和 clamp
仅在 StaticWrapper 实例中可用。
interface IStringWrapper {
length: number;
contains: (value: string) => boolean;
match: (regExp: RegExp) => boolean;
}
interface IArrayWrapper<T> {
* pop: () => ElementOf<T> | undefined;
* popm: () => ElementOf<T> | undefined;
* push: (...values: ElementOf<T>[]) => number;
* pushm: (...values: ElementOf<T>[]) => number;
* shift: () => ElementOf<T> | undefined;
* shiftm: () => ElementOf<T> | undefined;
* unshift: (...values: ElementOf<T>[]) => void;
* unshiftm: (...values: ElementOf<T>[]) => void;
length: number;
first: ElementOf<T> | undefined;
last: ElementOf<T> | undefined;
random: () => ElementOf<T> | undefined;
contains: (value: ElementOf<T>) => boolean;
every: (callback: (value: ElementOf<T>) => boolean) => boolean;
some: (callback: (value: ElementOf<T>) => boolean) => boolean;
none: (callback: (value: ElementOf<T>) => boolean) => boolean;
}
* 方法 pop
、push
、shift
、unshift
仅在 StaticWrapper 实例中可用。 这些方法创建一个新数组,对于值可变更新使用带有 m 后缀的方法(注意它们不受中间件的影响)。
Wrappers
TypeScript variable wrapper library, transpiled to ES2015 JavaScript. Library contains few getters, for supporting older JavaScript versions you will need to fork this project and change getters to functions.
Install
npm i @0x2e757/wrappers
Usage
Library contains two classes you are interested in - StaticWrapper
and DynamicWrapper
. Also there are IStaticWrapper
and IDynamicWrapper
interfaces, as well as general IWrapper
interface, which will work for both classes but will only contain those methods that are implemented in both classes.
How to import
import { IWrapper, IStaticWrapper, StaticWrapper, IDynamicWrapper, DynamicWrapper } from "@0x2e757/wrappers";
StaticWrapper
Simple wrapper that can be used as a value container with assignable on update callbacks (subscribers).
Usage example:
import { StaticWrapper } from "@0x2e757/wrappers";
const wSomeValue = new StaticWrapper(0);
wSomeValue.applyMiddleware(next => value => next(value > 5 ? 5 : value));
wSomeValue.subscribe(console.log, true);
wSomeValue.set(1);
wSomeValue.set(10);
// Console output:
// > 0
// > 1
// > 5
DynamicWrapper
Advanced wrapper which contains dynamically computed value (it can not be set manually). Values are being computed only before they are going to be used.
Usage example:
import { StaticWrapper, DynamicWrapper } from "@0x2e757/wrappers";
const wUsername = new StaticWrapper("John");
const wBalance = new StaticWrapper(100);
const wAccountInfo = new DynamicWrapper(wUsername, wBalance, (username, balance) => `${username} has ${balance}$.`);
wAccountInfo.subscribe(console.log, true);
wBalance.dec(50);
// Console output:
// > John has 100$.
// > John has 50$.
Available methods
General
Wrappers are always implementing base interface IWrapperBase
:
interface IWrapperBase<T> {
* set: (value: T, debounce?: number) => void;
* setter: (value: T, debounce?: number) => () => void;
* applyMiddleware: (middleware: Middleware<T>) => void;
emit: () => T;
subscribe: (callback: Subscriber<T>, triggerImmediately?: boolean) => void;
unsubscribe: (callback: Subscriber<T>) => void;
dispose: () => void;
}
* Methods set
, setter
and applyMiddleware
are available only in StaticWrapper instances.
Helpers
Depending on wrapper value type different helper methods (few of them are getter properties) are available. They can modify wrapper's value (in static wrappers only) or perform some action with value and return a result. In fact, all of them always exist in class objects, but for static typing purposes using TypeScript types they are hidden from IDE and static code analyzer.
interface IPrimitiveWrapper<T> {
eq: (value: T) => boolean;
neq: (value: T) => boolean;
}
Booleans, numbers and strings are considered as primitives.
interface IBooleanWrapper {
* toggle: () => void;
}
* Method toggle
is available only in StaticWrapper instances.
interface INumberWrapper {
* inc: (delta?: number) => void;
* dec: (delta?: number) => void;
* random: (min: number, max: number, integer?: boolean) => void;
* round: (fractionDigits?: number) => void;
* clamp: (min: number, max: number) => void;
lt: (value: number) => boolean;
lte: (value: number) => boolean;
gt: (value: number) => boolean;
gte: (value: number) => boolean;
in: (min: number, max: number) => boolean;
}
* Methods inc
, dec
, random
, round
and clamp
are available only in StaticWrapper instances.
interface IStringWrapper {
length: number;
contains: (value: string) => boolean;
match: (regExp: RegExp) => boolean;
}
interface IArrayWrapper<T> {
* pop: () => ElementOf<T> | undefined;
* popm: () => ElementOf<T> | undefined;
* push: (...values: ElementOf<T>[]) => number;
* pushm: (...values: ElementOf<T>[]) => number;
* shift: () => ElementOf<T> | undefined;
* shiftm: () => ElementOf<T> | undefined;
* unshift: (...values: ElementOf<T>[]) => void;
* unshiftm: (...values: ElementOf<T>[]) => void;
length: number;
first: ElementOf<T> | undefined;
last: ElementOf<T> | undefined;
random: () => ElementOf<T> | undefined;
contains: (value: ElementOf<T>) => boolean;
every: (callback: (value: ElementOf<T>) => boolean) => boolean;
some: (callback: (value: ElementOf<T>) => boolean) => boolean;
none: (callback: (value: ElementOf<T>) => boolean) => boolean;
}
* Methods pop
, push
, shift
, unshift
are available only in StaticWrapper instances. These methods create a new array, for value mutable updates use methods with m suffix (note that they aren't affected by middlewares).