arayts
Version:
让 TypeScript 开发如丝般顺滑。ArayTS 提供了一套高效、优雅的算法工具集,包含常用的数据结构与算法实现,帮助开发者轻松构建可靠的应用程序。
117 lines (116 loc) • 4.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var TypeInfer = /** @class */ (function () {
function TypeInfer() {
}
/**
* 从数据结构推导类型定义
*/
TypeInfer.generateType = function (data, typeName) {
if (typeName === void 0) { typeName = 'Generated'; }
var type = this.inferType(data);
return this.formatType(type, typeName);
};
/**
* 从数据结构生成 TypeScript 接口定义
*/
TypeInfer.generateInterface = function (data, interfaceName) {
if (interfaceName === void 0) { interfaceName = 'IGenerated'; }
var type = this.inferType(data);
return this.formatInterface(type, interfaceName);
};
/**
* 生成类型声明文件
*/
TypeInfer.generateTypeDefinition = function (data, name) {
if (name === void 0) { name = 'Generated'; }
var interfaceContent = this.generateInterface(data, "I".concat(name));
var typeContent = this.generateType(data, name);
return "// \u751F\u6210\u7684\u7C7B\u578B\u5B9A\u4E49\u6587\u4EF6\n".concat(typeContent, "\n\n").concat(interfaceContent, "\n");
};
TypeInfer.inferType = function (data) {
if (data === null)
return 'null';
if (data === undefined)
return 'undefined';
switch (typeof data) {
case 'string':
return this.inferStringLiteralType(data);
case 'number':
return this.inferNumberType(data);
case 'boolean':
return 'boolean';
case 'object':
if (Array.isArray(data)) {
return this.inferArrayType(data);
}
return this.inferObjectType(data);
default:
return 'any';
}
};
TypeInfer.inferStringLiteralType = function (value) {
// 检查是否是日期格式
if (this.isDateString(value))
return 'Date';
// 检查是否是邮箱格式
if (this.isEmailString(value))
return 'string /* email */';
// 检查是否是URL格式
if (this.isUrlString(value))
return 'string /* url */';
return 'string';
};
TypeInfer.inferNumberType = function (value) {
if (Number.isInteger(value))
return 'number /* int */';
return 'number';
};
TypeInfer.inferArrayType = function (arr) {
var _this = this;
if (arr.length === 0)
return 'any[]';
var elementTypes = new Set(arr.map(function (item) { return _this.inferType(item); }));
if (elementTypes.size === 1) {
return "".concat(elementTypes.values().next().value, "[]");
}
return "(".concat(Array.from(elementTypes).join(' | '), ")[]");
};
TypeInfer.inferObjectType = function (obj) {
var properties = [];
for (var _i = 0, _a = Object.entries(obj); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], value = _b[1];
var propertyType = this.inferType(value);
properties.push("".concat(key, ": ").concat(propertyType));
}
return "{\n".concat(this.indent(properties.join(',\n')), "\n}");
};
TypeInfer.formatType = function (type, name) {
return "type ".concat(name, " = ").concat(type, ";");
};
TypeInfer.formatInterface = function (type, name) {
if (!type.startsWith('{'))
return this.formatType(type, name);
return "interface ".concat(name, " ").concat(type);
};
TypeInfer.indent = function (str) {
return str.split('\n').map(function (line) { return " ".concat(line); }).join('\n');
};
TypeInfer.isDateString = function (str) {
return !isNaN(Date.parse(str));
};
TypeInfer.isEmailString = function (str) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
};
TypeInfer.isUrlString = function (str) {
try {
new URL(str);
return true;
}
catch (_a) {
return false;
}
};
return TypeInfer;
}());
exports.default = TypeInfer;