tsioc
Version:
tsioc is AOP, Ioc container, via typescript decorator
354 lines (352 loc) • 8.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Registration_1 = require("../Registration");
/**
* check target is function or not.
*
* @export
* @param {*} target
* @returns
*/
function isFunction(target) {
if (!target) {
return false;
}
return typeof target === 'function';
}
exports.isFunction = isFunction;
/**
* check Abstract class with @Abstract or not
*
* @export
* @param {*} target
* @returns {target is AbstractType<any>}
*/
function isAbstractDecoratorClass(target) {
if (!isFunction(target)) {
return false;
}
if (Reflect.hasOwnMetadata('@Abstract', target)) {
return true;
}
return false;
}
exports.isAbstractDecoratorClass = isAbstractDecoratorClass;
/**
* check target is class or not.
*
* @export
* @param {*} target
* @returns
*/
function isClass(target) {
if (!isFunction(target)) {
return false;
}
if (target.prototype) {
if (!target.name || target.name === 'Object') {
return false;
}
if (Reflect.hasOwnMetadata('@Abstract', target)) {
return false;
}
var type = target;
// for uglify
if (/^[a-z]$/.test(type.name)) {
if (type.classAnnations && type.classAnnations.name) {
return true;
}
else {
return false;
}
}
else {
if (type.classAnnations && type.classAnnations.name) {
return true;
}
if (!/^[A-Z@]/.test(target.name)) {
return false;
}
}
// for IE 8, 9
if (!isNodejsEnv() && /MSIE [6-9]/.test(navigator.userAgent)) {
return true;
}
try {
target.arguments && target.caller;
return false;
}
catch (e) {
return true;
}
}
return false;
}
exports.isClass = isClass;
/**
* is run in nodejs or not.
*
* @export
* @returns {boolean}
*/
function isNodejsEnv() {
return (typeof process !== 'undefined') && (typeof process.versions.node !== 'undefined');
}
exports.isNodejsEnv = isNodejsEnv;
/**
* check target is token or not.
*
* @export
* @param {*} target
* @returns {target is Token<any>}
*/
function isToken(target) {
if (!target) {
return false;
}
if (isString(target) || isSymbol(target) || isClass(target) || (isObject(target) && target instanceof Registration_1.Registration)) {
return true;
}
return false;
}
exports.isToken = isToken;
/**
* is target promise or not.
*
* @export
* @param {*} target
* @returns {target is Promise<any>}
*/
function isPromise(target) {
if (!target) {
return false;
}
if (isFunction(target.then) && isFunction(target.catch)) {
return true;
}
return false;
}
exports.isPromise = isPromise;
/**
* is target rxjs observable or not.
*
* @export
* @param {*} target
* @returns {boolean}
*/
function isObservable(target) {
if (!target && !isObject(target)) {
return false;
}
if (isFunction(target.subscribe) && isFunction(target.toPromise)) {
return true;
}
return false;
}
exports.isObservable = isObservable;
/**
* is target base object or not.
* eg. {}, have not self constructor;
* @export
* @param {*} target
* @returns {target is Promise<any>}
*/
function isBaseObject(target) {
if (!target) {
return false;
}
if (target.constructor && target.constructor.name === 'Object') {
return true;
}
return false;
}
exports.isBaseObject = isBaseObject;
/**
* is metadata object or not.
*
* @export
* @param {any} target
* @param {string[]} [props]
* @param {string[]} [extendsProps]
* @returns {boolean}
*/
function isMetadataObject(target, props, extendsProps) {
if (!target) {
return false;
}
if (isToken(target)) {
return false;
}
if (target instanceof RegExp || target instanceof Date) {
return false;
}
if (target.constructor && target.constructor.name !== 'Object') {
return false;
}
props = props || ['type'];
if (extendsProps) {
props = extendsProps.concat(props);
}
return Object.keys(target).some(function (n) { return props.indexOf(n) > 0; });
}
exports.isMetadataObject = isMetadataObject;
/**
* check object is class metadata or not.
*
* @export
* @param {any} target
* @param {string[]} [extendsProps]
* @returns {boolean}
*/
function isClassMetadata(target, extendsProps) {
return isMetadataObject(target, ['singleton', 'provide', 'alias', 'type'], extendsProps);
}
exports.isClassMetadata = isClassMetadata;
/**
* check object is param metadata or not.
*
* @export
* @param {any} target
* @param {string[]} [extendsProps]
* @returns {boolean}
*/
function isParamMetadata(target, extendsProps) {
return isMetadataObject(target, ['type', 'provider', 'index'], extendsProps);
}
exports.isParamMetadata = isParamMetadata;
/**
* check object is param prop metadata or not.
*
* @export
* @param {any} target
* @param {string[]} [extendsProps]
* @returns {boolean}
*/
function isParamPropMetadata(target, extendsProps) {
return isMetadataObject(target, ['type', 'provider', 'index'], extendsProps);
}
exports.isParamPropMetadata = isParamPropMetadata;
/**
* check object is property metadata or not.
*
* @export
* @param {any} target
* @param {string[]} [extendsProps]
* @returns {boolean}
*/
function isPropertyMetadata(target, extendsProps) {
return isMetadataObject(target, ['type', 'provider'], extendsProps);
}
exports.isPropertyMetadata = isPropertyMetadata;
/**
* check target is string or not.
*
* @export
* @param {*} target
* @returns {target is string}
*/
function isString(target) {
return typeof target === 'string';
}
exports.isString = isString;
/**
* check target is boolean or not.
*
* @export
* @param {*} target
* @returns {target is boolean}
*/
function isBoolean(target) {
return typeof target === 'boolean' || (target === true || target === false);
}
exports.isBoolean = isBoolean;
/**
* check target is number or not.
*
* @export
* @param {*} target
* @returns {target is number}
*/
function isNumber(target) {
return typeof target === 'number';
}
exports.isNumber = isNumber;
/**
* check target is undefined or not.
*
* @export
* @param {*} target
* @returns {target is undefined}
*/
function isUndefined(target) {
return typeof target === 'undefined' || target === undefined;
}
exports.isUndefined = isUndefined;
/**
* check target is unll or not.
*
* @export
* @param {*} target
* @returns {target is null}
*/
function isNull(target) {
return target === null;
}
exports.isNull = isNull;
/**
* check target is array or not.
*
* @export
* @param {*} target
* @returns {target is Array<any>}
*/
function isArray(target) {
return Array.isArray(target);
}
exports.isArray = isArray;
/**
* check target is object or not.
*
* @export
* @param {*} target
* @returns {target is object}
*/
function isObject(target) {
var type = typeof target;
return target != null && (type === 'object' || type === 'function');
}
exports.isObject = isObject;
/**
* check target is date or not.
*
* @export
* @param {*} target
* @returns {target is Date}
*/
function isDate(target) {
return isObject(target) && target instanceof Date;
}
exports.isDate = isDate;
/**
* check target is symbol or not.
*
* @export
* @param {*} target
* @returns {target is Symbol}
*/
function isSymbol(target) {
return typeof target === 'symbol' || (isObject(target) && /^Symbol\(/.test(target.toString()));
}
exports.isSymbol = isSymbol;
/**
* check target is regexp or not.
*
* @export
* @param {*} target
* @returns {target is RegExp}
*/
function isRegExp(target) {
return target && target instanceof RegExp;
}
exports.isRegExp = isRegExp;
//# sourceMappingURL=../sourcemaps/utils/typeCheck.js.map