utils

package
v0.3.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package utils provides small shared helpers for common value handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CJKRatio added in v0.1.13

func CJKRatio(s string) float64

CJKRatio 计算字符串中中文字符(CJK Unified Ideographs)占比,忽略空白字符。

func CJKRatioMarkdown added in v0.1.13

func CJKRatioMarkdown(s string) float64

CJKRatioMarkdown 计算 Markdown 文本中的中文占比。 与 CJKRatio 的区别是:先过滤代码块、行内代码、链接 URL 等非自然语言内容, 并将中文标点计为中文倾向字符,避免技术文档中代码示例拉低中文比例。

func Default

func Default[T comparable](value, fallback T) T

Default returns fallback when value is the zero value for T.

func DefaultString

func DefaultString(value, fallback string) string

DefaultString returns fallback when value is empty.

Types

type DebouncedHandler

type DebouncedHandler[T any] func(ctx context.Context, value T) error

DebouncedHandler handles the final value that survives a debounce window.

type ErrorHandler

type ErrorHandler func(ctx context.Context, err error)

ErrorHandler observes handler errors without coupling callers to background execution.

type MergeFunc

type MergeFunc[T any] func(existing T, incoming T) T

MergeFunc merges an incoming value into the existing pending value. Return the merged result. If nil, the incoming value replaces the existing one.

type TrailingDebouncer

type TrailingDebouncer[T any] struct {
	// contains filtered or unexported fields
}

TrailingDebouncer keeps the latest call for each key and runs it after the window is quiet.

func NewTrailingDebouncer

func NewTrailingDebouncer[T any](window time.Duration, handler DebouncedHandler[T], onError ErrorHandler, merge MergeFunc[T]) (*TrailingDebouncer[T], error)

NewTrailingDebouncer creates a keyed trailing-edge debouncer. If merge is nil, Call replaces the pending value; otherwise it merges via merge(existing, incoming).

func (*TrailingDebouncer[T]) Call

func (d *TrailingDebouncer[T]) Call(ctx context.Context, key string, value T)

Call resets the key's debounce window and keeps value as the latest pending work. If a merge function is configured, the incoming value is merged into the existing pending value.

func (*TrailingDebouncer[T]) Close added in v0.1.17

func (d *TrailingDebouncer[T]) Close()

Close stops pending timers and rejects future calls.

Handlers that have already started are allowed to finish. Pending values are discarded; callers that need pending-result notification must coordinate it outside the debouncer before calling Close.

func (*TrailingDebouncer[T]) PendingValues added in v0.3.9

func (d *TrailingDebouncer[T]) PendingValues() []T

PendingValues returns a point-in-time copy of values that are still inside their debounce window. Values already handed to the handler are not included. It is intended for read-only observability and must not be mutated by callers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL