npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ticatec-ts-dictionary

v1.0.5

Published

拥有管理数据字典的工具类库

Downloads

2

Readme

公共数据字典管理

在前台的开发中,我们经常需要中使用数据字典用于选择,key-value转换工作。通过这个管理类库可以简单的实现。里面总共包括了三个组件

数据字典的类型

单层字典 Dictionary

单层字典建立一组key-value映射组。

  • 构造函数: new Dictionary(loader, keyName, getText, missingText);

其中loader是一个加载数据的函数,返回一个数组,keyName是字典的中key关键字的字段名,getText可以简单的是一个表示值的字段名, 也可以是一个函数,用于对应项目的显示值,missingText是没有对应数据项的时候返回的表达值。 使用方法如下:

  import Dictionary from 'ticatec-ts-dictionary';
  
  const deviceTypeLoader = async()=>await deviceService.getList();
  /**
   * 构造一个设备类型的数据字典,其中的关键字的字段为code,表达值的字段为text
   */
  const dicDeviceType = new Dictionary(deviceTypeLoader, 'code', 'text', '不存在的设备类型');
  
  
  const faultTypeLoader = async()=>await faultService.getList();
  const getFaultDesc = item => `${code}-${name}`;
  /**
   * 构造一个故障类型的数据字典,其中的关键字的字段为id,表达值的根据函数计算,返回code和name组合的字符串
   */
  const  dicFaultTypeDic = new Dictionary(deviceTypeLoader, 'id', getFaultDesc, '错误的故障类型编码');
  

数据字典类也支持加载本地静态数据,比如json格式的数据,示例代码如下: JSON数据:gender.json

  [
    {"code":"M", "text":"男"}, 
    {"code":"F", "text":"女"}, 
    {"code":"N", "text":"未知"}
  ]

ts示例代码:

    import genders from './data/gender.json';
    
    const dicGender = new Dictionary(()=>genders, 'code', 'text', '错误的性别编码');
  • 属性
  1. list 数据字典中数据项列表
  2. initialized 数据字典是否初始化完毕
  • 方法
  1. reload() - 异步方法,加载数据
  2. get(key) - 根据key值返回对应的数据项
  3. getValue(key) - 根据key值返回对应的表达值,如果不存在,返回预定义的missingText

层次型数据字典 TreeDictionary

层次型数据字典是一个树状的多层数据字典,用于记录多级机构,比如省-市-区县的字典类。层次型数据字典集成自普通的数据字典

特殊要求: loader返回的数据项中包含子数据项的,属性名称必须是children

  • 构造函数: new TreeDictionary(loader, keyName, getText, missingText, isLeaf); 前面的四个参数同普通的数据字典构造参数,最后一个是一个函数,判断数据项是否是一个叶节点。示例代码:
const isLeaf = item => item.children == null;
  • 属性
  1. leafs 所有的叶节点
  • 方法
  1. getChildren(key) 获取指定节点的所有子数据项

通常来说,程序员没有必要自己初始化数据字典,可以通过数字字典管理器来管理所有的数据字典

数据字典管理器单一实例 dicManager

这是一个数据字典管理类的实例。项目中需要的所有数据字典都可以让这个来管理。使用方法如下:

  import dicManager from 'ticatec-ts-dictonary';

dicManager包括以下方法:

  • 注册一个普通数据字典,register
  • 获取一个数据字典,get
  • 初始化加载指定的数据字典 initialize
  • 初始化加载指定的数据字典 refresh

register

    /**
     * 注册一个普通的数据字典
     * @param key 数据字典的名称
     * @param dic 数据字典
     */
    dicManager.register('gender', dicGender);

get

返回对应的数据字典

    let dic = await dicManager.get('country');

initialize

    await dicManager.initialize(['country', 'gender', 'org', 'dept']);