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 🙏

© 2025 – Pkg Stats / Ryan Hefner

prefix-encoder

v1.0.2

Published

The Prefix Encoder supports encoding an original string and another type of value into a string. By adding a type prefix to the serialized result, it distinguishes the original value type before serialization. 前缀编码器支持将原始字符串及另一种类型值编码为字符串。通过给序列化结果添加类型前缀,区分序

Readme

prefix-encoder

English | 简体中文

介绍

前缀编码器(Prefix Encoder)支持将原始字符串及另一种类型值(记为类型 T)编码为字符串。通过给序列化结果添加类型前缀,区分序列化前的原始值类型。

  • 类型 T 序列化为字符串的算法、类型前缀及转义字符均由你设定。
  • 常规字符串的序列化结果为其自身;如有必要,前缀编码器会添加转义字符以避免冲突。

接口

createPrefixEncoder 方法根据传入的 EncoderOptions 创建并返回一个 PrefixEncoder 实例。

EncoderOptions

配置对象包含以下属性:

| 属性 | 类型 | 作用 | | --------------- | ------------------ | ------------------------------- | | prefix | string | 类型 T 被编码为字符串后的前缀。 | | stringify | (arg: T) => string | 类型 T 的序列化方法。 | | parse | (arg: string) => T | 类型 T 的反序列化方法。 | | escapeCharacter | string | 转义字符。 |

PrefixEncoder

提供了以下方法:

| 方法 | 类型 | 作用 | | -------- | ---------------------------- | --------------------------------------- | | encode | (arg: T | string) => string | 将字符串或 T 类型值编码为字符串。 | | decode | (str: string) => T | string | 将字符串解码回原来的字符串或 T 类型值。 |

示例

Bigint

以下示例代码创建了处理 BigInt 的编码器, 并对 BigInt 数值或字符串进行编码和解码操作。

import {createPrefixEncoder} from 'prefix-encoder';

const bigIntEncoder = createPrefixEncoder<bigint>({
    prefix: '$big-int:',
    // 将 BigInt 值序列化为字符串。
    stringify: (num: bigint) => String(num),
    // 将字符串反序列化回 BigInt 值。
    parse: (str: string) => BigInt(str),
    // 用于处理包含前缀的字符串的转义字符。
    escapeCharacter: '_',
});

// 使用指定的前缀将 BigInt 值 2025n 编码为字符串。
// 结果是一个以 "$big-int:" 为前缀的字符串。
// "$big-int:2025"
console.log(bigIntEncoder.encode(2025n));

// 解码一个编码后的字符串,以获取原始的 BigInt 值。
// 输入的字符串应该是以 "$big-int:" 为前缀的格式。
// 1234n
console.log(bigIntEncoder.decode('$big-int:1234'));

// 对一个不符合编码格式的普通字符串进行编码, 将原样返回。
// "hello, world"
console.log(bigIntEncoder.encode('hello, world'));

// 解码一个不符合编码格式的普通字符串, 也将原样返回。
// "hello, world"
console.log(bigIntEncoder.decode('hello, world'));

// 对已经包含前缀的字符串进行编码, 为避免混淆,会在前面添加转义字符 "_"。
// "_$big-int:2025"
console.log(bigIntEncoder.encode('$big-int:2025'));

Date

以下代码展示了针对 Date 类型的编码器的创建和使用。

const dateEncoder = createPrefixEncoder<Date>({
    escapeCharacter: '_',
    prefix: '$time:',
    stringify: (date: Date) => String(date.getTime()),
    parse: (str: string) => new Date(Number(str)),
});

// "$time:818035920000"
console.log(dateEncoder.encode(new Date('04 Dec 1995 00:12:00 GMT')));

// Date: Thu Jan 01 1970 01:00:00 GMT+0100
console.log(dateEncoder.decode('$time:0'));

许可

MIT