1.4.0 • Published 1 year ago

kinyx-core-types v1.4.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

types

define some commonly used types

Install

npm install kinyx-core-types

Usage

TYPES.{Type}.is - 判断对象是否是指定类型

import { TYPES } from 'kinyx-core-types';

console.assert(TYPES.String.is(1) === false);
console.assert(TYPES.String.is('str') === true);

// 简单判断数组类型
console.assert(TYPES.Array.is('str') === false);
console.assert(TYPES.Array.is([]) === true);
// 判断数组元素是否为指定类型
console.assert(TYPES.Array.is(['str'], TYPES.Integer) === false);
console.assert(TYPES.Array.is(['str'], TYPES.String) === true);
console.assert(TYPES.Array.is(['str', 1, 'str'], TYPES.String) === false);
// 单指定类型时,null视为满足条件。
console.assert(TYPES.Array.is(['str', null, 'str'], TYPES.String) === true);

since 1.4.0

import { TYPES } from 'kinyx-core-types';

// 判断数组元素是否为指定类型中的一种 console.assert(TYPES.Array.is('str', 1, 'str', TYPES.String, TYPES.Integer) === true); // 多指定类型时,null视为一种类型,如允许则需要指定。 console.assert(TYPES.Array.is('str', null, 1, 'str', TYPES.String, TYPES.Integer) === false); console.assert(TYPES.Array.is('str', null, 1, 'str', TYPES.String, TYPES.Integer, TYPES.Null) === true);

### TYPES.{Type}.check - 校验对象是否是指定类型
> since 1.0.0 (deprecated 1.2.0)
```javascript
import { TYPES } from 'kinyx-core-types';

TYPES.String.check('str', false);// OK
TYPES.String.check('str', true);// OK

// TYPES.String.check(null, false);// IllegalArgumentException [Error]: object is not String
TYPES.String.check(null, true);// OK

// TYPES.String.check(1, false);// IllegalArgumentException [Error]: object is not String
// TYPES.String.check(1, true);// IllegalArgumentException [Error]: object is not null or String

TYPES.Array.check([], false);// OK
TYPES.Array.check([], true,);// OK

TYPES.Array.check(['str'], TYPES.String, false);// OK
TYPES.Array.check(['str'], TYPES.String, true,);// OK

TYPES.Array.check([null], TYPES.String, false);// OK
TYPES.Array.check([null], TYPES.String, true,);// OK

// TYPES.Array.check([1], TYPES.String, false);// IllegalArgumentException [Error]: 
// TYPES.Array.check([1], TYPES.String, true);// IllegalArgumentException [Error]: 

since 1.2.0

import { TYPES } from 'kinyx-core-types';

TYPES.String.check(false, 'str');// OK TYPES.String.check(true, 'str');// OK

// TYPES.String.check(false, null);// IllegalArgumentException Error: object is not String TYPES.String.check(true, null);// OK

// TYPES.String.check(false, 1);// IllegalArgumentException Error: object is not String // TYPES.String.check(true, 1);// IllegalArgumentException Error: object is not null or String

TYPES.Array.check(false, []);// OK TYPES.Array.check(true, []);// OK

TYPES.Array.check(false, 'str', TYPES.String);// OK TYPES.Array.check(true, 'str', TYPES.String);// OK

TYPES.Array.check(false, null, TYPES.String);// OK TYPES.Array.check(true, null, TYPES.String);// OK

// TYPES.Array.check(false, 1, TYPES.String);// IllegalArgumentException Error: object is not Array of String // TYPES.Array.check(true, 1, TYPES.String);// IllegalArgumentException Error: object is not null or Array of String

// 首先验证对象是否为Integer,验证通过。不会回调验证方法。 TYPES.Integer.check((obj) => { TYPES.String.check(false, obj); }, 1);

// 首先验证对象是否为String,验证失败。回调验证方法,验证对象是否为Integer,验证通过。 TYPES.String.check((obj) => { TYPES.Integer.check(false, obj); }, 1);

// 首先验证对象是否为String,验证失败。回调验证方法,验证对象是否为Decimal,验证失败。抛出异常 // TYPES.String.check((obj) => { TYPES.Decimal.check(false, obj); }, 1);// IllegalArgumentException Error: object is not Decimal

### TYPES.{Type}.levelTo - 判断当前类型与指定类型的层级关系
```javascript
import { TYPES } from 'kinyx-core-types';

console.assert(TYPES.String.levelTo(TYPES.String) === 0);// 同类型
console.assert(TYPES.String.levelTo(TYPES.Number) === 0);// 不相关类型

console.assert(TYPES.Integer.levelTo(TYPES.Number) === -1);// 子类型
console.assert(TYPES.Number.levelTo(TYPES.Integer) === 1);// 父类型

console.assert(TYPES.PInfinity.levelTo(TYPES.Number) === -2);// 多级子类型
console.assert(TYPES.Number.levelTo(TYPES.PInfinity) === 2);// 多级父类型

console.assert(TYPES.PInfinity.levelTo(TYPES.Infinity) === -1);// 子类型
console.assert(TYPES.Infinity.levelTo(TYPES.PInfinity) === 1);// 父类型

TYPES.{Type}.getSuperType - 取父类型

import { TYPES } from 'kinyx-core-types';

console.assert(TYPES.String.getSuperType(true) === TYPES.String);// 取顶级父类型(类型本身就是顶级类型,则返回类型本身。)
console.assert(TYPES.String.getSuperType(false) === TYPES.String);// 取直接父类型(类型本身就是顶级类型,则返回类型本身。)

console.assert(TYPES.Integer.getSuperType(true) === TYPES.Number);// 取顶级父类型
console.assert(TYPES.Integer.getSuperType(false) === TYPES.Number);// 取直接父类型

console.assert(TYPES.PInfinity.getSuperType(true) === TYPES.Number);// 取顶级父类型
console.assert(TYPES.PInfinity.getSuperType(false) === TYPES.Infinity);// 取直接父类型

TypeUtils.getType - 获取对象的类型

import { TYPES, TypeUtils } from 'kinyx-core-types';

console.assert(TypeUtils.getType(1, false) === TYPES.Integer);
console.assert(TypeUtils.getType(1, true) === TYPES.Number);

console.assert(TypeUtils.getType('str', false) === TYPES.String);
console.assert(TypeUtils.getType('str', true) === TYPES.String);

TypeUtils.matchTypes - 匹配类型

import { KIND, TYPES, TypeUtils } from 'kinyx-core-types';

const types = TypeUtils.matchTypes(null);// 所有类型
const types = TypeUtils.matchTypes(TYPES.String);// 指定类型 => [TYPES.String]
const types = TypeUtils.matchTypes([TYPES.String, TYPES.Integer]);// 指定类型 => [TYPES.String, TYPES.Integer]
const types = TypeUtils.matchTypes(KIND.API.singleton);// 指定类型种类 => [TYPES.Math, TYPES.JSON]
const types = TypeUtils.matchTypes('String|Integer');// 指定类型 => [TYPES.String, TYPES.Integer]

TypeUtils.overloading - 创建重载方法

import { TYPES, TypeUtils, ADD_IMPLEMENTED_FUNCTION } from 'kinyx-core-types';

const func = TypeUtils.overloading();

// 添加无参实现方法
func[ADD_IMPLEMENTED_FUNCTION](
    function (types, obj) {
        console.assert(types.length === 0);
        console.log('no arg');
    },
);

// 添加单参数(Undefined)实现方法
func[ADD_IMPLEMENTED_FUNCTION](
    function (types, obj) {
        console.assert(types.length === 1);
        console.log('one arg for Undefined', obj);
    },
    TYPES.Undefined,
);

// 添加单参数(Integer)实现方法
func[ADD_IMPLEMENTED_FUNCTION](
    function (types, obj) {
        console.assert(types.length === 1);
        console.log('one arg for Integer', obj);
    },
    TYPES.Integer,
);

// 添加单参数(String)实现方法
func[ADD_IMPLEMENTED_FUNCTION](
    function (types, obj) {
        console.assert(types.length === 1);
        console.log('one arg for String', obj);
    },
    TYPES.String,
);

func();// no arg
func(undefined);// one arg for Undefined undefined
func(1);// one arg for Integer 1
func('str');// one arg for String str

TypeUtils.overloading for StringValue - 创建重载方法(对于StringValue类型的特殊处理)

since 1.3.0

import { StringValue } from 'kinyx-core-types/src/js/StringValue.mjs';

// 添加单参数(StringValue)实现方法 funcADD_IMPLEMENTED_FUNCTION( function (types, obj) { console.assert(types.length === 1); console.log('one arg for StringValue =>', obj); }, TYPES.StringValue, ); func(new StringValue('test'));// one arg for StringValue StringValue => { value: 'test' } func(new StringValue('demo'));// one arg for StringValue StringValue => { value: 'demo' }

// 添加单参数(StringValue(指定值demo))实现方法 funcADD_IMPLEMENTED_FUNCTION( function (types, obj) { console.assert(types.length === 1); console.log('one arg for StringValue(demo) =>', obj); }, new StringValue('demo'), ); func(new StringValue('demo'));// one arg for StringValue(demo) => StringValue { value: 'demo' }

// 添加多参数(StringValue, Integer)实现方法 funcADD_IMPLEMENTED_FUNCTION( function (types, obj1, obj2) { console.assert(types.length === 2); console.log('two args for (StringValue, Integer) =>', obj1, obj2); }, TYPES.StringValue, TYPES.Integer, ); func(new StringValue('test'), 1);// two args for (StringValue, Integer) => StringValue { value: 'test' } 1 func(new StringValue('demo'), 1);// two args for (StringValue, Integer) => StringValue { value: 'demo' } 1

// 添加多参数(StringValue(指定值demo), Integer)实现方法 funcADD_IMPLEMENTED_FUNCTION( function (types, obj1, obj2) { console.assert(types.length === 2); console.log('two args for (StringValue(demo), Integer) =>', obj1, obj2); }, new StringValue('demo'), TYPES.Integer, ); func(new StringValue('demo'), 1);// two args for (StringValue(demo), Integer) => StringValue { value: 'demo' } 1

### TypeUtils.overloading for combination Array - 创建重载方法(支持数组组合形式添加方法)
> since 1.4.0
```javascript
import { TYPES, TypeUtils, ADD_IMPLEMENTED_FUNCTION } from 'kinyx-core-types';

const func = TypeUtils.overloading();

// 添加无参实现方法
func[ADD_IMPLEMENTED_FUNCTION](
    function (types, obj) {
        console.assert(types.length === 0);
        console.log('no arg');
    },
);

// 添加单参数(Undefined、Integer、String)实现方法
func[ADD_IMPLEMENTED_FUNCTION](
    function (types, obj) {
        console.assert(types.length === 1);
        console.log('one arg for Undefined or Integer or String', obj);
    },
    [TYPES.Undefined, TYPES.Integer, TYPES.String,],
);

func();// no arg
func(undefined);//one arg for Undefined or Integer or String undefined
func(1);// one arg for Undefined or Integer or String 1
func('str');// one arg for Undefined or Integer or String str

TypeUtils.anyToString - 转换成字符串

since 1.4.0

import { TypeUtils, ANY_TO_STRING__WRAP } from 'kinyx-core-types';

TypeUtils.anyToString(obj, ANY_TO_STRINGWRAP.ES_ORIGINAL);// String(obj),有发生错误的可能。 TypeUtils.anyToString(obj, ANY_TO_STRINGWRAP.ES_ORIGINAL_WITHOUT_EXCEPTION);// String(obj),当发生错误时,进行捕获,并返回描述性字符串。 TypeUtils.anyToString(obj, ANY_TO_STRINGWRAP.NODEJS);// 与NodeJS直接输出对象展示保持一致 TypeUtils.anyToString(obj, ANY_TO_STRINGWRAP.VALUE);// 用于辅助wrapfalse方法。NodeJS输出对象时,值的表现形式。 TypeUtils.anyToString(obj, ANY_TO_STRINGWRAP.WRAPPER);// 对所有类型都进行统一格式的包装。${className}(${value}) TypeUtils.anyToString(obj, ANY_TO_STRINGWRAP.LOG);// 消除所有输出的歧义,且尽可能的精简。

### TypeUtils.getClassName - 取类名
```javascript
import { TYPES, TypeUtils } from 'kinyx-core-types';
console.assert(TypeUtils.getClassName(1) === TYPES.Integer.className);
console.assert(TypeUtils.getClassName('str') === TYPES.String.className);
console.assert(TypeUtils.getClassName({}) === TYPES.Object.className);
console.assert(TypeUtils.getClassName([]) === TYPES.Array.className);

console.assert(TypeUtils.getClassName(new TypeError()) === TypeError.name);

class X { };
console.assert(TypeUtils.getClassName(new X()) === X.name);

ObjectUtils.isObject - 判断对象是否为object类型

since 1.1.0

import { ObjectUtils } from 'kinyx-core-types';

console.assert(ObjectUtils.isObject(1) === false); console.assert(ObjectUtils.isObject('str') === false); console.assert(ObjectUtils.isObject({}) === true); console.assert(ObjectUtils.isObject([]) === false);

### ObjectUtils.isEmptyObject - 判断对象是否为空对象。即有没有自定义属性。
> since 1.1.0
```javascript
import { ObjectUtils } from 'kinyx-core-types';

console.assert(ObjectUtils.isEmptyObject(1) === false);
console.assert(ObjectUtils.isEmptyObject('str') === false);
console.assert(ObjectUtils.isEmptyObject([]) === false);

console.assert(ObjectUtils.isEmptyObject({}) === true);
console.assert(ObjectUtils.isEmptyObject({ x: 1 }) === false);

ObjectUtils.isNullObject - 判断对象是否为Object.create(null)。

since 1.1.0

import { ObjectUtils } from 'kinyx-core-types';

console.assert(ObjectUtils.isNullObject(1) === false); console.assert(ObjectUtils.isNullObject('str') === false); console.assert(ObjectUtils.isNullObject([]) === false);

console.assert(ObjectUtils.isNullObject({}) === false); console.assert(ObjectUtils.isNullObject({ x: 1 }) === false);

console.assert(ObjectUtils.isNullObject(Object.create(null)) === true);

### ObjectUtils.hasProperty - 判断对象是否有某个属性
> since 1.1.0
```javascript
import { ObjectUtils } from 'kinyx-core-types';

{
    // 没有属性,所有方法结果一致。
    const property = 'x';
    const obj = {};
    console.assert(ObjectUtils.hasProperty(obj, property, 0) === false);
    console.assert(ObjectUtils.hasProperty(obj, property, 1) === false);
    console.assert(ObjectUtils.hasProperty(obj, property, 2) === false);
}

{
    // 正常定义的属性,所有方法结果一致。
    const property = 'x';
    const obj = { [property]: 1 };
    console.assert(ObjectUtils.hasProperty(obj, property, 0) === true);
    console.assert(ObjectUtils.hasProperty(obj, property, 1) === true);
    console.assert(ObjectUtils.hasProperty(obj, property, 2) === true);
}

{
    const property = Symbol('');
    const obj = { [property]: 1 };
    console.assert(ObjectUtils.hasProperty(obj, property, 0) === false);// 无法取得Symbol属性
    console.assert(ObjectUtils.hasProperty(obj, property, 1) === true);// 可以取得Symbol属性
    console.assert(ObjectUtils.hasProperty(obj, property, 2) === true);// 可以取得Symbol属性
}

{
    const property = 'x';
    class X { };
    X.prototype[property] = 1;// 在原型上普通定义属性
    const obj = new X();
    console.assert(ObjectUtils.hasProperty(obj, property, 0) === false);// 无法取得原型上的属性
    console.assert(ObjectUtils.hasProperty(obj, property, 1) === false);// 无法取得原型上的属性
    console.assert(ObjectUtils.hasProperty(obj, property, 2) === true);// 可以取得原型上的属性
}

{
    const property = 'x';
    const obj = {};
    // 使用Object.defineProperty方法定义不可枚举属性
    Object.defineProperty(obj, property, {
        value: 1,
        enumerable: false,
    });
    console.assert(ObjectUtils.hasProperty(obj, property, 0) === false);// 无法取得不可枚举的属性
    console.assert(ObjectUtils.hasProperty(obj, property, 1) === true);// 可以取得不可枚举的属性
    console.assert(ObjectUtils.hasProperty(obj, property, 2) === true);// 可以取得不可枚举的属性
}

ObjectUtils.clone - 深度克隆

since 1.4.0

import { ObjectUtils } from 'kinyx-core-types';

const obj = { x: { y: 1 } }; console.assert(obj.x.y === 1);

const clone = ObjectUtils.clone(obj); console.assert(clone.x.y === 1);

// 修改clone对象,不影响原对象。 clone.x.y = 2; console.assert(obj.x.y === 1); console.assert(clone.x.y === 2);

### ArrayUtils.combination - 数组组合化
> since 1.4.0
```javascript
import { ArrayUtils } from 'kinyx-core-types';

{
    const arr = [1, 2];
    const result = ArrayUtils.combination(arr);
    console.assert(result.length === 1);
    console.assert(result[0].length === 2);
}
{
    const arr = [1, 2, ['x', 'y']];
    const result = ArrayUtils.combination(arr);
    console.assert(result.length === 2);
    console.assert(result[0].length === 3);
    console.assert(result[0][2] === 'x');
    console.assert(result[1].length === 3);
    console.assert(result[1][2] === 'y');
}
{
    const arr = [1, ['x', 'y'], 3];
    const result = ArrayUtils.combination(arr);
    console.assert(result.length === 2);
    console.assert(result[0].length === 3);
    console.assert(result[0][1] === 'x');
    console.assert(result[1].length === 3);
    console.assert(result[1][1] === 'y');
}
1.4.0

1 year ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago