New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

aura-enum

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aura-enum

aura-enum

latest
Source
npmnpm
Version
1.0.6
Version published
Maintainers
1
Created
Source

🔥🔥🔥 aura-enum 是一个管理前端 字典数据的方法

🔥🔥🔥🔥内置包含defineEnum方法

🚀🚀
const  enumColors  = defineEnum(
   name: string, // 必须提供,且为string类型
   enumObj: Record<string, string | { [key: string]: any }>,
    options: EnumFactoryOptions = {
        strict: false,          // 默认不启用严格模式
        defaultLang: 'zh',      // 默认语言设置为英语
        isEnumStore: true,     // 默认自动注册到全局store
        plugins: []             // 默认插件列表为空
    })



🔥🔥🔥🔥内置包含enumStore方法 只要在defineEnum 里面设置 isEnumStore,就会自动收集到 enumStore里面。

import { EnumStore, EnumFactory } from 'aura-enum';

// 创建枚举存储实例
const enumStore = new EnumStore({
    enableCache: true,
    storagePrefix: 'my_enum_store'
});

🚀 1. register - 注册枚举
const statusEnum = new EnumFactory([
    { label: '启用', value: 1 },
    { label: '禁用', value: 0 }
]);
enumStore.register('status', statusEnum);

const genderEnum = new EnumFactory([
    { label: '男', value: 'male' },
    { label: '女', value: 'female' }
]);
enumStore.register('gender', genderEnum);

🚀 2. getOptions - 获取选项列表
const statusOptions = enumStore.getOptions('status');
console.log(statusOptions);
// 输出:
// [
//   { label: '启用', value: 1 },
//   { label: '禁用', value: 0 }
// ]

🚀 3. get - 获取枚举工厂实例
const statusFactory = enumStore.get('status');
console.log(statusFactory.disposedList);
// 输出完整的枚举项列表

🚀 4. update - 更新枚举
const updatedStatusEnum = new EnumFactory([
    { label: '启用', value: 1 },
    { label: '禁用', value: 0 },
    { label: '待审核', value: 2 }
]);
enumStore.update('status', updatedStatusEnum);

🚀 5. remove - 移除枚举
enumStore.remove('gender');

🚀 6. has - 检查枚举是否存在
console.log(enumStore.has('status')); // true
console.log(enumStore.has('gender')); // false

🚀 7. getKeys - 获取所有注册的枚举键名
const keys = enumStore.getKeys();
console.log(keys); // ['status']

🚀 8. search - 搜索枚举项
const searchResults = enumStore.search('status', '启', ['label']);
console.log(searchResults);
// 输出:
// [{ label: '启用', value: 1, key: '1' }]

🚀 9. getValueByLabel - 通过标签获取值
const value = enumStore.getValueByLabel('status', '启用');
console.log(value); // 1

🚀 10. getLabelValueMap - 获取标签值映射
const labelValueMap = enumStore.getLabelValueMap('status');
console.log(labelValueMap);
// 输出:
// {
//   '启用': 1,
//   '禁用': 0,
//   '待审核': 2
// }

🚀 11. subscribe - 订阅枚举变化
const unsubscribe = enumStore.subscribe((key, action) => {
    console.log(`枚举 ${key} 发生 ${action} 操作`);
});

🚀 12. addPlugin - 添加插件
const logPlugin = {
    name: 'logPlugin',
    onEnumChange: (key: string, action: string) => {
        console.log(`[Plugin] 枚举 ${key} 发生 ${action} 操作`);
    }
};
enumStore.addPlugin(logPlugin);

🚀 13. getConfig - 获取配置
const config = enumStore.getConfig();
console.log(config);
// 输出:
// {
//   enableCache: true,
//   storagePrefix: 'my_enum_store'
// }

🚀 14. clear - 清空所有枚举
enumStore.clear();

// 完整的使用场景示例
function initializeEnums() {
    // 1. 创建并注册状态枚举
    const statusEnum = new EnumFactory([
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 }
    ]);
    enumStore.register('status', statusEnum);

    // 2. 监听枚举变化
    enumStore.subscribe((key, action) => {
        console.log(`枚举 ${key} 发生 ${action} 操作`);
    });

    // 3. 在表单中使用
    const form = {
        status: null,
        statusOptions: enumStore.getOptions('status')
    };

    // 4. 搜索功能
    function searchStatus(keyword: string) {
        return enumStore.search('status', keyword);
    }

    // 5. 获取显示文本
    function getStatusLabel(value: number) {
        const factory = enumStore.get('status');
            return factory.getLabel(value);
        }
    }

    // React组件示例
    function EnumSelect({ enumKey, value, onChange }) {
        const options = enumStore.getOptions(enumKey);

        return (
            <select value={value} onChange={e => onChange(e.target.value)}>
                {options.map(option => (
                    <option key={option.value} value={option.value}>
                        {option.label}
                    </option>
                ))}
            </select>
        );
    }

    // Vue组件示例
    const EnumSelect = {
        props: ['enumKey', 'value'],
        computed: {
            options() {
                return enumStore.getOptions(this.enumKey);
            }
        },
        template: `
            <select v-model="value">
                <option v-for="option in options"
                        :key="option.value"
                        :value="option.value">
                    {{ option.label }}
                </option>
            </select>
        `
    };

    // 动态更新枚举示例
    async function updateStatusEnum() {
        // 假设从API获取新的枚举数据
        const newStatusData = await fetchStatusEnum();
        const newStatusEnum = new EnumFactory(newStatusData);

        // 更新枚举
        enumStore.update('status', newStatusEnum);
    }

    // 多语言支持示例
    function initializeI18nEnums(locale: string) {
        const statusEnum = new EnumFactory([
            { label: locale === 'zh' ? '启用' : 'Enabled', value: 1 },
            { label: locale === 'zh' ? '禁用' : 'Disabled', value: 0 }
        ]);

        if (enumStore.has('status')) {
            enumStore.update('status', statusEnum);
        } else {
            enumStore.register('status', statusEnum);
        }
    }

🔥🔥🔥🔥内置包含getEnum方法 快速获取enum 方法


🚀获取枚举工厂中的已处理列表(disposedList)。
@param {string | null | undefined} params - 枚举工厂的键值。如果为 null 或 undefined,则会抛出错误。
@returns {Array<any> | null}
- 如果 `params` 对应的枚举工厂存在且包含 `disposedList`,则返回该列表;
- 如果 `params` 无效或对应的枚举工厂不存在,则返回 null。

@example
正常情况:获取 disposedList
const result = getEnum("exampleKey");
console.log(result); // 输出对应的 disposedList 或 null

@example
错误情况:传入 null 或 undefined
const result = getEnum(null);
控制台输出: "params cannot be null or undefined"
console.log(result); // 输出 null
const xxxxxxxx = getEnum('xxxxxxxx')

浏览器支持

本地开发推荐使用 Chrome 80+ 浏览器

支持现代浏览器, 不支持 IE

alt IEalt Edgealt Firefoxalt Chromealt Safari
IEEdgeFirefoxChromeSafari
not supportlast 2 versionslast 2 versionslast 2 versionslast 2 versions

Keywords

nin

FAQs

Package last updated on 23 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts