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 🙏

© 2026 – Pkg Stats / Ryan Hefner

soon-utils

v0.0.11

Published

a collection of functions usually used

Readme

soon-utils

A collection of functions usually used

Installation

npm install soon-utils

Usage

import { downloadUrl, obj2keyObj, parseJSON } from 'soon-utils';

// Example usage
downloadUrl('https://example.com/file.pdf', 'example.pdf');
const data = obj2keyObj({ name: 'world' });
const parsed = parseJSON('{"name": "world"}');

Functions Documentation

Language

English

Download

downloadUrl

Function: Start a browser download task from a URL

Definition:

export function downloadUrl(path: string, filename: string): void;

Example:

import { downloadUrl } from 'soon-utils';

downloadUrl('https://example.com/file.pdf', 'example.pdf');
downloadBlob

Function: Start a browser download task from a Blob

Definition:

export function downloadBlob(blob: Blob, filename: string): void;

Example:

import { downloadBlob } from 'soon-utils';

const blob = new Blob(['Hello, world!'], { type: 'text/plain' });
downloadBlob(blob, 'hello.txt');
downloadResponse

Function: Download file from HTTP response

Definition:

export async function downloadResponse(response: Response, filename?: string): Promise<void>;

Example:

import { downloadResponse } from 'soon-utils';

// Fetch a file and download it
async function downloadFile() {
  const response = await fetch('https://example.com/file.pdf');
  await downloadResponse(response, 'example.pdf');
}

downloadFile();
getHeaderFilename

Function: Extract filename from headers which have header key "content-disposition"

Definition:

export function getHeaderFilename(headers?: Headers): string | undefined;
export function getHeaderFilename(contentDispositionHeaderValue?: string | null): string | undefined;

Example:

import { getHeaderFilename } from 'soon-utils';

// From Headers object
const headers = new Headers();
headers.append('content-disposition', 'attachment; filename="example.pdf"');
const filename1 = getHeaderFilename(headers);
console.log(filename1); // "example.pdf"

// From string
const filename2 = getHeaderFilename('attachment; filename="example.pdf"');
console.log(filename2); // "example.pdf"

Object

obj2keyObj

Function: Convert object to key-value pairs where values are the same as keys

Definition:

export function obj2keyObj<T extends object>(data: T): { [Key in keyof Omit<T, OptionalKeysOf<T>>]: Key };

Example:

import { obj2keyObj } from 'soon-utils';

const data = obj2keyObj({ name: "world", goods: { price: 1, amount: 10 } });
console.log(data);
// {name:'name',goods:'goods'}
obj2keyPathObj

Function: Convert object to key-value pairs where values are the full path of the keys

Definition:

export function obj2keyPathObj<Data extends object>(data: Data, parentKey?: string): Simplify<Obj2KeyPathObj<Data, void>>;

Example:

import { obj2keyPathObj } from 'soon-utils';

const data = obj2keyPathObj({ name: "world", goods: { price: 1, amount: 10 } });
console.log(data);
// {name:'name',goods:{price:'goods.price',amount:'goods.amount'}}
getTreePathArr

Function: Get the path array from tree structure

Definition:

export const getTreePathArr = <T>(tree: T[], childrenKey: ArrayKeys<T>, equal: (item: T) => boolean) => T[];

Example:

import { getTreePathArr } from 'soon-utils';

const tree = [
  { id: 1, name: 'Root', children: [
    { id: 2, name: 'Child 1', children: [
      { id: 3, name: 'Grandchild 1' }
    ]}
  ]}
];

const path = getTreePathArr(tree, 'children', (item) => item.id === 3);
console.log(path);
// [{ id: 1, name: 'Root' }, { id: 2, name: 'Child 1' }, { id: 3, name: 'Grandchild 1' }]

JSON

parseJSON

Function: Parse JSON string to object

Definition:

export function parseJSON<T>(json: string): T | undefined;

Example:

import { parseJSON } from 'soon-utils';

const jsonString = '{"name": "world", "age": 18}';
const data = parseJSON<{ name: string; age: number }>(jsonString);
console.log(data);
// { name: 'world', age: 18 }
json2type

Function: Convert JSON to TypeScript type

Definition:

export function json2type(json: object | any[], name: string | undefined, config?: {
    useInterface?: boolean;
    useArray?: boolean;
    addExport?: boolean;
    addDeclare?: boolean;
    extract?: number;
    optional?: boolean;
    nullable?: boolean;
}): { name: string; code: string }[];

Example:

import { json2type } from 'soon-utils';

const json = { name: 'world', age: 18, address: { street: 'Main St', city: 'New York' } };
const types = json2type(json, 'Person', { useInterface: true, addExport: true });
console.log(types[0].code);
// export interface Person {
//   name: string;
//   age: number;
//   address: {
//     street: string;
//     city: string;
//   };
// }

Style

replaceStyleTag

Function: Add or replace style tag

Definition:

export function replaceStyleTag(id: string, cssText: string): void;

Example:

import { replaceStyleTag } from 'soon-utils';

const css = '.container { background: #f0f0f0; padding: 10px; }';
replaceStyleTag('my-styles', css);

Types

TransKey

Type: Convert key to string format

Definition:

export type TransKey<Key> = Key extends string ? Key : Key extends number ? `[${Key}]` : never;
MergeKey

Type: Merge parent key and child key

Definition:

export type MergeKey<ParentKey, Key extends string> = ParentKey extends string
  ? `${ParentKey}.${Key}`
  : Key;
RequiredUndefined

Type: Convert optional properties to required properties with undefined type

Definition:

export type RequiredUndefined<T> = Simplify<{
  [Key in keyof Required<T>]: undefined | T[Key]
}>;

Example:

import type { RequiredUndefined } from 'soon-utils';

interface User {
  name: string;
  age?: number;
}

type RequiredUser = RequiredUndefined<User>;
// Equivalent to:
// interface RequiredUser {
//   name: string;
//   age: undefined | number;
// }

中文

下载

downloadUrl

功能:从URL开始浏览器下载任务

定义

export function downloadUrl(path: string, filename: string): void;

示例

import { downloadUrl } from 'soon-utils';

downloadUrl('https://example.com/file.pdf', 'example.pdf');
downloadBlob

功能:从Blob开始浏览器下载任务

定义

export function downloadBlob(blob: Blob, filename: string): void;

示例

import { downloadBlob } from 'soon-utils';

const blob = new Blob(['Hello, world!'], { type: 'text/plain' });
downloadBlob(blob, 'hello.txt');
downloadResponse

功能:从HTTP响应下载文件

定义

export async function downloadResponse(response: Response, filename?: string): Promise<void>;

示例

import { downloadResponse } from 'soon-utils';

// 获取文件并下载
async function downloadFile() {
  const response = await fetch('https://example.com/file.pdf');
  await downloadResponse(response, 'example.pdf');
}

downloadFile();
getHeaderFilename

功能:从带有"content-disposition"头的响应头中提取文件名

定义

export function getHeaderFilename(headers?: Headers): string | undefined;
export function getHeaderFilename(contentDispositionHeaderValue?: string | null): string | undefined;

示例

import { getHeaderFilename } from 'soon-utils';

// 从Headers对象
const headers = new Headers();
headers.append('content-disposition', 'attachment; filename="example.pdf"');
const filename1 = getHeaderFilename(headers);
console.log(filename1); // "example.pdf"

// 从字符串
const filename2 = getHeaderFilename('attachment; filename="example.pdf"');
console.log(filename2); // "example.pdf"

对象

obj2keyObj

功能:将对象转换为键值对,其中值与键相同

定义

export function obj2keyObj<T extends object>(data: T): { [Key in keyof Omit<T, OptionalKeysOf<T>>]: Key };

示例

import { obj2keyObj } from 'soon-utils';

const data = obj2keyObj({ name: "world", goods: { price: 1, amount: 10 } });
console.log(data);
// {name:'name',goods:'goods'}
obj2keyPathObj

功能:将对象转换为键值对,其中值是键的完整路径

定义

export function obj2keyPathObj<Data extends object>(data: Data, parentKey?: string): Simplify<Obj2KeyPathObj<Data, void>>;

示例

import { obj2keyPathObj } from 'soon-utils';

const data = obj2keyPathObj({ name: "world", goods: { price: 1, amount: 10 } });
console.log(data);
// {name:'name',goods:{price:'goods.price',amount:'goods.amount'}}
getTreePathArr

功能:从树结构中获取路径数组

定义

export const getTreePathArr = <T>(tree: T[], childrenKey: ArrayKeys<T>, equal: (item: T) => boolean) => T[];

示例

import { getTreePathArr } from 'soon-utils';

const tree = [
  { id: 1, name: '根节点', children: [
    { id: 2, name: '子节点1', children: [
      { id: 3, name: '孙节点1' }
    ]}
  ]}
];

const path = getTreePathArr(tree, 'children', (item) => item.id === 3);
console.log(path);
// [{ id: 1, name: '根节点' }, { id: 2, name: '子节点1' }, { id: 3, name: '孙节点1' }]

JSON

parseJSON

功能:解析JSON字符串为对象

定义

export function parseJSON<T>(json: string): T | undefined;

示例

import { parseJSON } from 'soon-utils';

const jsonString = '{"name": "world", "age": 18}';
const data = parseJSON<{ name: string; age: number }>(jsonString);
console.log(data);
// { name: 'world', age: 18 }
json2type

功能:将JSON转换为TypeScript类型

定义

export function json2type(json: object | any[], name: string | undefined, config?: {
    useInterface?: boolean;
    useArray?: boolean;
    addExport?: boolean;
    addDeclare?: boolean;
    extract?: number;
    optional?: boolean;
    nullable?: boolean;
}): { name: string; code: string }[];

示例

import { json2type } from 'soon-utils';

const json = { name: 'world', age: 18, address: { street: 'Main St', city: 'New York' } };
const types = json2type(json, 'Person', { useInterface: true, addExport: true });
console.log(types[0].code);
// export interface Person {
//   name: string;
//   age: number;
//   address: {
//     street: string;
//     city: string;
//   };
// }

样式

replaceStyleTag

功能:添加或替换样式标签

定义

export function replaceStyleTag(id: string, cssText: string): void;

示例

import { replaceStyleTag } from 'soon-utils';

const css = '.container { background: #f0f0f0; padding: 10px; }';
replaceStyleTag('my-styles', css);

类型

TransKey

类型:将键转换为字符串格式

定义

export type TransKey<Key> = Key extends string ? Key : Key extends number ? `[${Key}]` : never;
MergeKey

类型:合并父键和子键

定义

export type MergeKey<ParentKey, Key extends string> = ParentKey extends string
  ? `${ParentKey}.${Key}`
  : Key;
RequiredUndefined

类型:将可选属性转换为带有undefined类型的必填属性

定义

export type RequiredUndefined<T> = Simplify<{
  [Key in keyof Required<T>]: undefined | T[Key]
}>;

示例

import type { RequiredUndefined } from 'soon-utils';

interface User {
  name: string;
  age?: number;
}

type RequiredUser = RequiredUndefined<User>;
// 等同于:
// interface RequiredUser {
//   name: string;
//   age: undefined | number;
// }