@baloise/web-app-utils

中文文档
3.17.4发布于 2026/07/01Apache-2.0TypeScriptESM + CJS近 30 天下载 4152

Utilities for Baloise Web Applications

原始使用文档
文档目录41 节
  1. Installation guide
  2. Using npm
  3. Table of Content
  4. Language Utils
  5. LanguageModel
  6. Browser Utils
  7. Functions
  8. Integration Utils
  9. loadConsentManagerBaloiseSwitzerland
  10. Common Model Utils
  11. RequestState
  12. API
  13. areArraysEqual
  14. now
  15. today
  16. floorTime
  17. ceilTime
  18. formatDateString
  19. isValidIsoString
  20. format
  21. parse
  22. dateSeparator
  23. debounce
  24. isEmpty
  25. isEnterKey
  26. isSpaceKey
  27. isEscapeKey
  28. isBackspaceKey
  29. isArrowDownKey
  30. isArrowUpKey
  31. isArrowLeftKey
  32. isArrowRightKey
  33. isHomeKey
  34. isEndKey
  35. numberLocale
  36. dateLocale
  37. isValidMonetaryNumber
  38. getDecimalSeparator
  39. getThousandSeparator
  40. formatLocaleNumber
  41. parseLocaleNumber

@baloise/web-app-utils

Continuous Release npm npm bundle size npm GitHub GitHub issues

Javascript utilities for Baloise Web Applications.

Installation guide

Using npm

npm i @baloise/web-app-utils

Table of Content

Language Utils

The language utils helps to list the common languages and to detect the correct ones.

LanguageModel

valueOf

Returns the Language of the given language key or if the lauage is not registered it returns undefined.

Signature valueOf(key: string): Language | undefined

Example

const language = LanguageModel.valueOf('de')
if (language === undefined) {
  // language is not registered
}

valueOfOrDefault

Returns the Language of the given language key or if the language is not registered it returns the default one.

Signature valueOfOrDefault(key: string): Language

Example

const language = LanguageModel.valueOfOrDefault('de')

isValidKey

Verifies if the given language key is registered as a supported language or not.

Signature isValidKey(key: string): boolean

Example

if (LanguageModel.isValidKey('de')) {
  // language key is valid
} else {
  // language key is not valid
}

Languages

We provide various set of language list for our supported countries. Those Languages have the type LanguageModel.

LanguagesOfSwitzerland

List of german, french, italien & english with the default language german.

Example

LanguagesOfSwitzerland.isValidKey('de') // true
LanguagesOfSwitzerland.isValidKey('be') // false
LanguagesOfGermany

List of german & english with the default language german.

LanguagesOfBelgium

List of dutch, french, italien & english with the default language dutch.

LanguagesOfLuxembourg

List of luxembourgish, french & english with the default language luxembourgish.

Language

The language object has only one property the key which has the language key like en.

Browser Utils

The browser utils solve some common request when working with the broser api.

For working with cookies we recommand to use the library js-cookie.

For working with the url we recommand to use the library query-string.

Functions

scrollToAnchorElement

Scrolls to the ancor element with the attribute data-ref="heading".

Signature scrollToAnchorElement(referenceName: string): void

Example

<div data-ref="heading">...</div>
scrollToAnchorElement('heading')

scrollToTopOfBody

Scrolls to the top of the body.

Signature scrollToTopOfBody(): void

open

Opens a new location in the current tab.

Signature open(url: string): void

openInNewWindow

Opens the new location in a new tab.

Signature openInNewWindow(url: string): void

getBrowserLanguage

Returns the main language key. For example if the browser has the language de-CH this function return the language key de, so we can use our LangaugeModel.

Signature getBrowserLanguage(): string

Example

const langKey = getBrowserLanguage()
if(LanguagesOfSwitzerland.isValidKey(langKey)) { ... }
...

disableDragAndDropFiles

Disables for the whole web application drag and drop.

Signature disableDragAndDropFiles(): void

getBrowserInfo

Returns some useful information from the browser.

Signature getBrowserInfo(): BrowserInfo

interface BrowserInfo {
  cookiesEnabled?: boolean
  language?: string
  timezone?: string
  userAgent?: string
}

Integration Utils

The integration utils helps to work with our integration tools like one trust or user centrics.

loadConsentManagerBaloiseSwitzerland

Loads the consent management script directly from our main cms system with the necessary parameters.

Signature loadConsentManagerBaloiseSwitzerland(): void

Common Model Utils

Some useful models to use in the daily business.

RequestState

The RequestState enum helps to have a state variable to show the current stand of an api call.

export enum RequestState {
  INIT,
  RUNNING,
  SUCCESS,
  ERROR,
}

Example (Angular)

import { Component, OnInit } from '@angular/core'
import { RequestState } from '@baloise/web-app-utils'
import { ConfirmEmailService, EmailConfirmationResponse } from '../confirm-email.service'

@Component({
  selector: 'confirm-email-page',
  templateUrl: './confirm-email.page.component.html',
  styleUrls: ['./confirm-email.page.component.scss'],
})
export class ConfirmEmailPageComponent implements OnInit {
  confirmEmailResponse: EmailConfirmationResponse
  requestState: RequestState = RequestState.INIT

  constructor(private confirmEmailService: ConfirmEmailService) {}

  confirmEmail() {
    this.requestState = RequestState.RUNNING
    this.confirmEmailService
      .confirmEmail()
      .toPromise()
      .then(resp => {
        this.requestState = RequestState.SUCCESS
        this.confirmEmailResponse = resp
      })
      .catch(() => {
        this.requestState = RequestState.ERROR
      })
  }
}

Common Utils

API

areArraysEqual

areArraysEqual(a: any, b: any) => boolean

Returns true if the arrays are equal

areArraysEqual(['a', 'b'], ['b', 'a']) // true

now

now() => Date

Returns a JS Date instance of the exact moment

const date = now()
// Wed Mar 10 2021 20:30:32 GMT+0100 (Central European Standard Time)

today

today() => Date

Returns a JS Date instance of today with time being set to 0

const date = today()
// Wed Mar 10 2021 00:00:00 GMT+0100 (Central European Standard Time)

floorTime

floorTime(date: Date) => Date

Returns a JS Date instance with time being set to 0

const date = floorTime(new Date())
// Wed Mar 10 2021 00:00:00 GMT+0100 (Central European Standard Time)

ceilTime

ceilTime(date: Date) => Date

Returns a JS Date instance with the time set to the possible end

const date = ceilTime(new Date())
// Wed Mar 10 2021 23:59:59 GMT+0100 (Central European Standard Time)

formatDateString

formatDateString(date: Date) => string

Return the formatted date string in ISO 8601 format. Options may be passed to control the parts and notations of the date.

const dateString = formatDateString(new Date())
// '2022-02-14'

isValidIsoString

isValidIsoString(dateString: any) => any

Validates if the given date string matches the iso date format.

isValidIsoString('2022-02-14')
// 'true'

format

format(locale: any, date: Date) => any

Formats the dates according to the given locale.

format('de-CH', new Date())
// '14.2.2022'

parse

parse(dateString: string, locale: any) => any

Parses the iso date string into a javascript date object.

const dateString = parse('2021-03-10')
// Wed Mar 10 2021 00:00:00 GMT+0100 (Central European Standard Time)

dateSeparator

dateSeparator(locale: any) => string

Returns the char which separates day form month and year.

dateSeparator('de-CH')
// .

debounce

debounce(func: any, wait: any) => any

The debounce function wait a certain amount of time before running the inner function again. This should reduce the number of times a function is called.

const debounceFunction = debounce(function () {
  // The function's code
}, 250)

window.addEventListener('resize', debounceFunction)

isEmpty

isEmpty(value: any) => boolean

Returns true if the value is empty

isEnterKey

isEnterKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the Enter key

isSpaceKey

isSpaceKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the Space key

isEscapeKey

isEscapeKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the Escape key

isBackspaceKey

isBackspaceKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the Backspace key

isArrowDownKey

isArrowDownKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the ArrowDown key

isArrowUpKey

isArrowUpKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the ArrowUp key

isArrowLeftKey

isArrowLeftKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the ArrowLeft key

isArrowRightKey

isArrowRightKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the ArrowRight key

isHomeKey

isHomeKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the Home key

isEndKey

isEndKey(event: KeyboardEvent) => boolean

Returns true if the keyboard event was triggered by the End key

numberLocale

numberLocale(locale: any) => string

Parses the locale to the correct display locale

numberLocale('de-CH')
// 'de-CH'

dateLocale

dateLocale(locale: any) => string

Parses the locale to the correct display locale

numberLocale('de-CH')
// 'fr-CH'

isValidMonetaryNumber

isValidMonetaryNumber(stringValue: string) => boolean

Returns true if the arrays are equal

isValidMonetaryNumber(`1'000.99`) // true

getDecimalSeparator

getDecimalSeparator(locale: any) => string

Returns the decimal separator of the given locale

getDecimalSeparator('de-ch') // .

getThousandSeparator

getThousandSeparator(locale: any) => string

Returns the thousand separator of the given locale

getThousandSeparator('de-ch') // '

formatLocaleNumber

formatLocaleNumber(locale: any, number: number, minimumFractionDigits: number) => string

Formats the number into the given locale

formatLocaleNumber('de-ch', 1000.42) // 1'000.42

parseLocaleNumber

parseLocaleNumber(locale: any, stringNumber: string) => number

Parses the locale formatted number into a native number

parseLocaleNumber('de-ch', '1'000.42') // 1000.42

版本与发布记录

@baloise/web-app-utils 共发布 93 个版本,首次发布于 2021/04/29;过去 12 个月发布了 7 个版本。

版本发布日期距今
3.17.4latest2026/07/014 个月前
3.17.32026/07/014 个月前
3.17.22026/03/237 个月前
3.17.12026/03/237 个月前
3.16.52025/12/0910 个月前
3.16.42025/12/0910 个月前

其他发布通道:beta → 3.7.2-beta.1。

关于 @baloise/web-app-utils 的常见问题

@baloise/web-app-utils 是什么?

Utilities for Baloise Web Applications

如何安装 @baloise/web-app-utils?

在项目目录运行 npm install @baloise/web-app-utils,或使用 pnpm add / yarn add / bun add。

@baloise/web-app-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@baloise/web-app-utils 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@baloise/web-app-utils 可以商用吗?

@baloise/web-app-utils 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@baloise/web-app-utils 的下载量有多少?

根据 npm 官方下载统计,@baloise/web-app-utils 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,152 次,日均约 138 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@baloise/web-app-utils 还在维护吗?

最新版本 3.17.4 发布于 2026/07/01(4 个月前);至今共发布 93 个版本,其中过去 12 个月发布了 7 个;首次发布于 2021/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。