
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
类型转换并验证转换结果是否满足条件,并且支持 TypeScript 类型输出
import { typeCast } from "typecasts";
const var1 = typeCast("123", { type: "int" });
console.log(var1); // 123
console.log(typeof var1); // number
使用 typeCastPick 挑选对象中的多个数据
import { typeCastPick } from "typecasts";
// 输入数据
const inputData = {
a: 1,
b: 'yes',
c: 'str ',
d: '1,2,3,4',
e: '2021-5-13 13:11:22',
f: 'http://test',
obj: {
g: 2,
h: 'a,b,c,d'
}
};
// 类型处理
const v = typeCastPick(inputData, {
a: '!int', // 整数类型,! 感叹号开头表示此项必须并且不能为null
b: 'bool',
c: 'trim',
d: 'int[]', // 如果以 [] 结尾则代表转换为类型数组形式
e: 'date',
f: {
// 如果不使用快捷类型,可以分别指定类型的转换规则
type: 'trim', // 目标类型
default: 'not-url', // 默认值
splitter: ',', // 是否切割为数组,定义切割字符
minItems: 1, // 切割数组后最少需要的元素数量
maxItems: 10, // 切割数组后最多不能超过的元素数量
// 数据验证规则
validate: {
url: true, // 验证是否符合链接格式
maxLength: 500, // 字符串长度不能超过 500
// 更多规则参照下表 “支持的数据验证”
},
},
obj: {
// 嵌套对象
type: {
g: '!int',
g: '~trim[]'
}
}
});
console.log(v);
// 输出
{
a: 1,
b: true,
c: 'str',
d: [ 1, 2, 3, 4 ],
e: new Date('2021-5-13 13:11:22'),
f: 'http://test',
obj: {
g: 2,
h: [ 'a', 'b', 'c', 'd' ]
}
}
例如 int 类型名称,可以写成 !int 或者 int[] 或两者的组合 !int[]
类型前缀规则
| 前缀 | 可传入类型 |
|---|---|
| 无 | 非必填, 允许 null |
| ? | 非必填, 不允许 null |
| ~ | 必填, 允许 null |
| ! | 必填, 不允许 null |
| 类型名 | 目标类型 | 功能描述 |
|---|---|---|
| number | number | 任何数值类型,整数、浮点 |
| int | number | 转换为整数类型 |
| float | number | 转换为浮点数类型 |
| bool | boolean | 转换为布尔类型,只有值为 y, 1, yes, on, true (不区分类型)才会被判定为 true,其他都为 false |
| string | string | 转换为字符串 |
| trim | string | 转换为字符串并去除两端的空字符 |
| trim1 | string | 转换为字符串并去除两端的空字符,但结果不能为空字符串 |
| any | any | 不进行类型转换,直接输出原始类型 |
| date | Date | 转换为日期类型 |
| 验证名 | 功能描述 | 参数 |
|---|---|---|
| lt | 小于 < | any |
| lte | 小于等于 <= | any |
| gt | 大于 > | any |
| gte | 大于等于 >= | any |
| eq | 等于 == | any |
| neq | 不等于 != | any |
| maxLength | 最大长度,调用值的 .length 属性 | number |
| minLength | 最小长度,调用值的 .length 属性 | number |
| in | 只能出现的值 | any[] |
| notIn | 不能出现的值 | any[] |
| regexp | 正则表达式匹配 | string | RegExp |
| 是否满足Email地址规则 | boolean | |
| slug | 是否满足URL路径规则 | boolean |
| url | 是否满足 URL 规则 | string[] | boolean |
FAQs
类型转换并验证转换结果是否满足条件,并且支持 TypeScript 类型输出
We found that typecasts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.