Skip to content

Latest commit

 

History

History
81 lines (60 loc) · 2.2 KB

README.md

File metadata and controls

81 lines (60 loc) · 2.2 KB

constant-sage

constant-sage can generate various commonly used data structures with complete types based on the constant dictionary you define.

GitHub   |   简体中文文档

Features

  • ✨ Convenient and easy to use
  • 🦾 Full TypeScript support
  • 💯 100% test coverage

Install

npm i constant-sage
pnpm i constant-sage
yarn add constant-sage

Usage

import { defineConstants } from 'constant-sage';

// Define the basic structure of constants
const defs = [
  { key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
  { key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
  { key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
] as const;

// Generate the constant collection
const { KEYS, VALUES } = defineConstants(defs);

// Use the constants
console.log(KEYS); // ['KEY_1', 'KEY_2', 'KEY_3']
console.log(VALUES); // ['VALUE_1', 'VALUE_2', 'VALUE_3']
// ...

API

defineConstants(defs: readonly IBaseDef[], labelspace?: string)

Generate a constant collection

  • defs: An array defining the basic structure of constants, where each element contains the key, value, and name fields.
  • labelspace (optional): The namespace of the constant collection.

Returns: An object containing the generated constants.

Constant Collection Object

  • KEYS: An array of all constant keys.
  • VALUES: An array of all constant values.
  • KV: An object mapping constant keys to values.
  • VK: An object mapping constant values to keys.
  • MAP_BY_KEY: A constant mapping object indexed by key.
  • MAP_BY_VALUE: A constant mapping object indexed by value.
  • MAP: An object mapping constant keys to values.
  • KN: An object mapping constant keys to names.
  • VN: An object mapping constant values to names.
  • LIST: An array containing all the constants.

License

MIT License © 2023 4xii