dts-element-fp
Version:
typescript definition generator for functional programming
60 lines (59 loc) • 2.78 kB
JavaScript
;
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
exports.__esModule = true;
var dts = require("dts-element");
var create_curried_types_1 = require("./create-curried-types");
var create_curried_various_types_1 = require("./create-curried-various-types");
var constants_1 = require("./utils/constants");
var sort_signatures_1 = require("./utils/sort-signatures");
function create_curried_declarations(name, type,
// istanbul ignore next
options) {
if (options === void 0) {
// istanbul ignore next
options = {}; }
var type_declarations = dts.is_function_type(type)
? create_curried_types_1.create_curried_types(name, type, options)
: (function (keys) {
return keys.length === 1
? create_curried_types_1.create_curried_types(name, type[keys[0]], options)
: create_curried_various_types_1.create_various_curried_types(name, type, options);
})(Object.keys(type));
// istanbul ignore next
var _a = options.get_placeholder_type, get_placeholder_type = _a === void 0 ? constants_1.get_placeholder_type_default : _a, _b = options.placeholder, placeholder = _b === void 0 ? constants_1.placeholder_default : _b, _c = options.inline_return_type, inline_return_type = _c === void 0 ? constants_1.inline_return_type_default : _c;
var is_placeholder = function (value) {
return dts.is_general_type(value) && value.name === get_placeholder_type();
};
var sorted_type_declarations = type_declarations.map(function (type_declaration) {
var type_declaration_type = type_declaration.type;
if (!dts.is_object_type(type_declaration_type)) {
return type_declaration;
}
return __assign({}, type_declaration, { type: __assign({}, type_declaration_type, { members: sort_signatures_1.sort_signatures(type_declaration_type.members, { is_placeholder: is_placeholder }) }) });
});
return [
dts.create_variable_declaration({
name: name,
"const": true,
type: dts.create_general_type({
name: sorted_type_declarations[0].name
})
})
].concat(sorted_type_declarations.filter(function (type_declaration) {
if (inline_return_type && /_1+$/.test(type_declaration.name)) {
return false;
}
if (!placeholder && !/_1*?0*?$/.test(type_declaration.name)) {
return false;
}
return true;
}));
}
exports.create_curried_declarations = create_curried_declarations;