primevue-mirror/packages/nuxt-module/src/utils.ts

38 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-06-11 12:21:12 +00:00
import type { MetaType } from '@primevue/metadata';
import type { ConstructsType, ResolvePathOptions } from './types';
export const Utils = {
object: {
2024-06-11 12:21:12 +00:00
isEmpty(value: any) {
return value === null || value === undefined || value === '' || (Array.isArray(value) && value.length === 0) || (!(value instanceof Date) && typeof value === 'object' && Object.keys(value).length === 0);
},
2024-06-11 12:21:12 +00:00
isNotEmpty(value: any) {
return !this.isEmpty(value);
},
2024-06-11 12:21:12 +00:00
isFunction(value: any) {
return !!(value && value.constructor && value.call && value.apply);
},
2024-06-11 12:21:12 +00:00
isString(value: any, empty: boolean = true) {
return typeof value === 'string' && (empty || value !== '');
},
2024-06-11 12:21:12 +00:00
getValue(obj: any, ...params: any) {
return this.isFunction(obj) ? obj(...params) : obj;
},
2024-06-11 12:21:12 +00:00
getName(item: MetaType, options: ConstructsType) {
return this.isFunction(options?.name) ? options.name(item) : `${options.prefix}${item.name}`;
},
2024-06-11 12:21:12 +00:00
getPath(fn: any, options: ResolvePathOptions) {
return this.isFunction(fn) ? fn(options) : options.from;
},
2024-06-11 12:21:12 +00:00
createStyleAsString(css: string, options = { name: '' }) {
if (css) {
const { name, ...rest } = options;
return `'<style type="text/css" data-primevue-style-id="${name}"${Object.entries(rest).reduce((s, [k, v]) => s + `${k}="${v}"`, ' ')}>${css}</style>'`;
}
return '';
}
}
};