@aligov/lib

lib

Usage no npm install needed!

<script type="module">
  import aligovLib from 'https://cdn.skypack.dev/@aligov/lib';
</script>

README

lib

@aligov/gov-lib

API

getGlobalConfig

getGlobalConfig(key: string, prefix = DEF_GLOBAL_PREFIX): unknown

获取指定的全局配置,即获取 window[${prefix}${key}] 的值。

getGlobalConfig('siteConfig') 实际是返回 window['G_GOV_siteConfig'] 的值。

参数

  • key:待获取的全局变量的名称,会在前面添加 prefix
  • prefix: key 前缀,默认是 G_GOV_

siteConfig

siteConfig(key: string, prefix = DEF_GLOBAL_PREFIX): unknown

获取指定的全局站点配置,即获取 window[${prefix}siteConfig] 下对应 key 的值。

siteConfig('domain') 实际是返回 window['G_GOV_siteConfig'].domain 的值。

参数

  • key:待获取的 siteConfig 下的属性的 key
  • prefix: key 前缀,默认是 G_GOV_

text

text(key: string, ...args: any[]): string

window.G_GOV_MESSAGE 对象中获取 key 对应的全局文案,并返回经 format 方法做变量插值后的字符串。

如果有 window.G_GOV_MESSAGE = { 'domain.page.mod.str': '计算:{1} + {0} = {2}' },那么

text('home_notice', 1, 2, 3) 将得到 计算:2 + 1 = 3

不存在的文案,返回空字符串。

参数

  • key: 文案 key
  • ...args:可选参数,用于做文案替换

format

format(str: string, ...args: any[]): string

基于 string-template 做的格式化。

var greeting

// Format using optional arguments

greeting = format("Hello {0}, you have {1} unread messages", "Robert", 12)
// greeting -> "Hello Robert, you have 12 unread messages"

// Format using an object hash with keys matching [0-9a-zA-Z]+
greeting = format("Hello {name}, you have {count} unread messages", {
    name: "Robert",
    count: 12
})
// greeting -> "Hello Robert, you have 12 unread messages"

// Format using a number indexed array
greeting = format("Hello {0}, you have {1} unread messages", ["Robert", 12])
// greeting -> "Hello Robert, you have 12 unread messages"