@achrinza/ 强型
强打本地JS,不硬要求转接.
类型脚本应该是什么 . @achrinza/strong-type 也将在 TypeScript 中工作.
任何地方javascript都可以运行ES6模块的类型检查模块. 这包括节点,电子和浏览器. 完全异形. 不同于TypeScript等其他解决方案, @achrinza/strong-type 除非你想,否则无需转接或捆绑。 这使得它包括任何转录或不转录的框架.
npm install --save @achrinza/strong-type
@achrinza/强力型做什么?
@achrinza/strong-type 允许对所有类型的JS对象和类别进行简单的类型执行. 它还支持 type unions ,以及特殊类型,例如: .any 财务报告和审定财务报表 .defined。它也可以扩展,并且为您自定义的类别和类型提供简单的检查.
这样做不需要任何额外的工具或转接。 这样你或你的组织就可以自由使用任何你想要的工具链或框架,甚至写出香草原生JS.
测试和覆盖范围
@achrinza/strong-type 正在使用 @node-ipc/vanilla-test 这是支持无害环境管理的js的裸骨测试框架,涵盖范围包括: C8 默认覆盖工具。 这对是测试ES6+模块的最有效和准确的方法.
运行测试并通过运行在本地机器上构建覆盖文件 node-test 或查看在 @achrinza/强型CDN家 页:1
示例 严格与非严格
使用 strict 财务报告和审定财务报表 non-strict 模式。 默认 @achrinza/strong-type 运行 strict 并将 throw 如果检查失败,您可以处理和使用动词错误,如果通过,则返回真实值.
用于 non-strict 模式,简单通过 false 给建筑师。 在这个模式下, @achrinza/strong-type 将返回 false 而不是在支票失败时扔出, 并且仍然会返回 true 当它经过.
严格
import Is from '@achrinza/strong-type';
//strict
const is = new Is;
const is = new Is();
const is = new Is(true);
//throws
is.string(1);
//union or multiple possible types
//should not throw
is.union(1,'string|number');
//union or multiple possible types
//should throw
function check(any){
is.defined(any);
}
非限制性
import Is from '@achrinza/strong-type';
//non-strict
const is = new Is(false);
//returns false
is.string(1);
//union or multiple possible types
//should return true
is.union(1,'string|number');
类型检查方法
所有这些方法都只需要一个词,就是 value 检查.
工会可以加入任何由 Is。 。 。 .
| 工会 | 参数 |
|---|---|
is.union | value, (中文(简体) ),pipe|seperated|type|list |
| 最常见的类型方法 | 参数 |
|---|---|
is.globalThis | value |
is.array | value |
is.bigint | value |
is.boolean | value |
is.date | value |
is.finite | value |
is.generator | value |
is.asyncGenerator | value |
is.infinity | value |
is.map | value |
is.NaN | value |
is.null | value |
is.number | value |
is.object | value |
is.promise | value |
is.regExp | value |
is.set | value |
is.string | value |
is.symbol | value |
is.undefined | value |
is.weakMap | value |
is.weakSet | value |
| 函数类型方法 | 参数 |
|---|---|
is.function | value |
is.asyncFunction | value |
is.generatorFunction | value |
is.asyncGeneratorFunction | value |
| 错误类型方法 | 参数 |
|---|---|
is.error | value |
is.evalError | value |
is.rangeError | value |
is.referenceError | value |
is.syntaxError | value |
is.typeError | value |
is.URIError | value |
| 缓冲/拖动阵列类型方法 | 参数 |
|---|---|
is.arrayBuffer | value |
is.dataView | value |
is.sharedArrayBuffer | value |
is.bigInt64Array | value |
is.bigUint64Array | value |
is.float32Array | value |
is.float64Array | value |
is.int8Array | value |
is.int16Array | value |
is.int32Array | value |
is.uint8Array | value |
is.uint8ClampedArray | value |
is.uint16Array | value |
is.uint32Array | value |
| 输入类型方法 | 参数 |
|---|---|
is.intlDateTimeFormat | value |
is.intlCollator | value |
is.intlDisplayNames | value |
is.intlListFormat | value |
is.intlLocale | value |
is.intlNumberFormat | value |
is.intlPluralRules | value |
is.intlRelativeTimeFormat | value |
| 垃圾收集方法 | 参数 |
|---|---|
is.finalizationRegistry | value |
is.weakRef | value |
核心方法
您可以使用这些来直接检查自己的类型/ 类 或扩展 Is 类,以添加您自己的方法,用于检查更多类型,特别是自定义类型和类.
| 方法 | 参数 | 说明 |
|---|---|---|
is.throw | valueType, (中文(简体) ), expectedType | 这将使用 Type 和 valueType 的值来创建和扔出新的 TypeError |
is.typeCheck | value, (中文(简体) ), type | 这将检查返回的 javascript 光谱类型 typeof。这样 type arg会是一个字符串 'string', (中文(简体) ), 'boolean', (中文(简体) ), 'number', (中文(简体) ), 'object', (中文(简体) ), 'undefined', (中文(简体) ), 'bigint' 包括:. |
is.instanceCheck | value=:new Fake, (中文(简体) ), constructor=:FakeCore | 核心默认参数为 a Fake 实例和实例 FakeCore 课。 允许不支持的 js spec 类型以 a 的预期失败 TypeError 而不是一个 Reference 错误(参见 ./example/web/ firefox中缺少一些支持的示例 Intl (第1条)。 此方法比较 value 与 constructor 保值为 instanceof 建筑师. |
is.symbolStringCheck | value, (中文(简体) ), type | 这可以用来检查 Symbol.toStringTag 但是在核心里我们只用它来检查 generator, (中文(简体) ), GeneratorFunction, (中文(简体) ), async function,以及 async GeneratorFunction 因为它们没有其他方法来检查它们的类型。 发电机 比如说 有一类 [object generator] 这边 所以你通过一个预期 generator 作为 value 字符串 'generator' 作为类型,我们处理其余的, 包括降低一切 保险交叉浏览器和平台检查 |
is.compare | value, (中文(简体) ), targetValue, (中文(简体) ), typeName | 这将可以明确比较 value 财务报告和审定财务报表 targetValue在核心中,我们只用于JS原始人/康斯坦丁人,他们没有其他方法来检查,例如: Infinity 财务报告和审定财务报表 globalThis。类型名称是类类型的字符串表示式,或者一个非常明显的错误字符串,因为这个参数唯一使用的地方是当 compare 结果a throws。 。 。 . |
is.defined | value | 这将检查一个值不是 is.undefined 任何类型都有效,但 undefined。 。 。 . |
is.any | value | 这是别名 is.defined 允许任何类型,但 undefined。 。 。 . |
is.exists | value | 这是别名 is.defined 以确定是否存在某种东西。 非常有用的特性测试 。 为了简单和透明,设立了别名. |
示例 QQ 基本类型检查
@achrinza/strong-type 它的用法非常简单.
import Is from '@achrinza/strong-type';
//strict
const is = new Is;
function strongTypeRequired(aNumber,aString,anAsyncFunction){
is.number(aNumber);
is.string(aString);
is.asyncFunction(anAsyncFunction);
}
function unionStrongTypeRequired(aNumberOrString){
is.union(aNumberOrString,'number|string');
}
//this will throw because we do not pass an async Function, but rather a normal function.
strongTypeRequired(1,'a',function(){})
//these will both pass because we accept both types in a union for the first param.
unionStrongTypeRequired(1);
unionStrongTypeRequired('a');
浏览器
节点
示例 QQ 生成器类型检查
发电机在输入许多devs时会出名。 这就是为什么我们选择以它们为例.
import Is from '@achrinza/strong-type';
//strict
const is = new Is;
//empty generator for this example's sake
function* myGenFunc(){};
const myGen=myGenFunc();
//we'll show async as well
async function* myAsyncGenFunc(){}
const myAsyncGen=myAsyncGenFunc();
//empty function
function myFunc(){};
//will pass and allow contunue
is.generator(myGen);
/*
will fail because a generatorFunction is a
GeneratorFunction, not a Generator
*/
try{
is.generator(myGenFunc);
}catch(err){
console.log(err);
}
//will pass and allow contunue
is.generatorFunction(myGenFunc);
//will fail because a function is not a generatorFunction
try{
is.generatorFunction(myFunc);
}catch(err){
console.log(err);
}
/*
will fail because this is @achrinza/strong-type, a
generatorFunction is explicitly a GeneratorFunction,
and not a Function
*/
try{
is.function(myGenFunc);
}catch(err){
console.log(err);
}
//will fail because a function is not a generatorFunction
try{
is.generatorFunction(myFunc);
}catch(err){
console.log(err);
}
//will pass and allow contunue
is.asyncGeneratorFunction(myAsyncGenFunc);
//will pass and allow contunue
is.asyncGenerator(myAsyncGen);
/*
will fail becase @achrinza/strong-type
asyncGenerators and generators are explicitly different
this is the same for generatorFunctions and functions
*/
try{
is.asyncGenerator(myGen);
}catch(err){
console.log(err);
}
try{
is.generator(myAsyncGen);
}catch(err){
console.log(err);
}
浏览器
节点
日期示例
import Is from '@achrinza/strong-type';
const is = new Is;
//returns true
is.date(new Date());
//throws in strict or returns false in non-strict
is.date(1975);
是NaN( ) vs 是. NaN( )
Javascripts类型因性质而软弱,所以在 isNaN() 函数返回任何非数字的正则,但 is.NaN() 仅在明确通过时返回真实 NaN。 。 。 .
import Is from '@achrinza/strong-type';
const is = new Is;
//built in JS isNaN
//returns false
isNaN(1);
//all return true
isNaN(NaN);
isNaN(undefined);
isNaN('a');
//@achrinza/strong-type is.NaN all return false in non-strict mode,
//or throw in default strict mode
is.NaN(1);
is.NaN(undefined);
is.NaN('a');
//in @achrinza/strong-type only this returns true
is.NaN(NaN);
浏览器
节点
运行浏览器和节点类型支持示例
运行 npm i 在此模块的 root dir 中,可以确保您得到devDependents 的安装.
节点示例 :
npm run nodeExample 整个屏幕应该是绿色的,因为所有类型都支持在节点.
浏览器示例和支持测试
npm start
这个会转动 node-http-server 在端口8000上的此模块根中。 浏览器示例在 ./example/web/ 文件夹。 你可以看到他们 通过去这个本地地址: http://localhost:8000 (英语)
Chrome, Opera, and Edge 支持所有类型,所以所有行会是绿色的.
您会在 Firefox 中看到一些红行, 因为它尚未支持所有类型 。 未支持的类型会在检查/验证时丢出类型错误 .
数字海洋静态App
我们使用免费的数字海洋静态Apps来托管本地服务器的版本. 跟你跑的一样 npm start 在你的机器上。 也可以像CDN一样使用,因为它每次更新分支时都会自动从主/主重建. @achrinza/强型CDN家 页:1
为自己的类型扩展类
如果你在生产中使用类型检查,你最好继续扩展模块,而不是叫更麻烦的 Core Methods 许多次.
自定义披萨类型
//custom class type constructor
class Pizza{
constructor(topping){
this.eat=true;
}
}
export {default:Pizza, Pizza}
扩展
import Is from '@achrinza/strong-type';
import Pizza from 'my-delicious-pizza';
class IsMy extends Is{
//custom pizza type
pizza(value){
return this.instanceCheck(value,Pizza);
}
}
export={default:IsMy, IsMy};
测试
import IsMy from 'my-delicious-typechecks';
import Pizza from 'my-delicious-pizza';
const is=new IsMy;
//will throw because 42 is not a Pizza Type
//and
is.pizza(42)