@yaser2us/json-api-nestjs-shared
Version:
Shared Helper for JsonApi Plugin for NestJs
39 lines • 1.29 kB
JavaScript
import { pascalCase } from 'change-case-commonjs';
export const ObjectTyped = {
keys: Object.keys,
values: Object.values,
entries: Object.entries,
fromEntries: Object.fromEntries,
};
export function isObject(item) {
return typeof item === 'object' && !Array.isArray(item) && item !== null;
}
export function isString(value) {
return typeof value === 'string' || value instanceof String;
}
export function createEntityInstance(name) {
const entityName = pascalCase(name);
return Function('return new class ' + entityName + '{}')();
}
export const getEntityName = (entity) => {
if (entity === null)
throw new Error('Entity is null');
if (typeof entity === 'string') {
return entity;
}
if (typeof entity === 'object' && 'name' in entity) {
return `${entity['name']}`;
}
if (typeof entity === 'object' &&
'constructor' in entity &&
'name' in entity.constructor) {
return entity['constructor']['name'];
}
if (typeof entity === 'function' &&
'constructor' in entity.prototype &&
'name' in entity.prototype.constructor) {
return entity.prototype.constructor.name;
}
throw new Error('Entity is not object');
};
//# sourceMappingURL=object-utils.js.map