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

@dafengzhen/resolve-styles

v0.1.3

Published

Parse mixed styles into a flat camelCased object — strings, objects, arrays, with override.

Downloads

581

Readme

resolve-styles

GitHub License npm version PRs Welcome

English

将混合样式解析为扁平的驼峰命名对象 — 支持字符串、对象、数组,可覆盖。零依赖。

安装

npm install @dafengzhen/resolve-styles

快速开始

import resolveStyles from '@dafengzhen/resolve-styles';

const styles = resolveStyles({
  color: 'red',
  fontSize: '16px',
  height: '50px',
  margin: '0px',
  padding: '10px',
  width: '100px',
});

console.log(styles);
// {
//   color: 'red',
//   fontSize: '16px',
//   height: '50px',
//   margin: '0px',
//   padding: '10px',
//   width: '100px'
// }

注意: 数值会被转换为纯字符串,不会自动添加单位后缀。请使用显式字符串值来指定 CSS 单位(例如,使用 '16px' 而不是 16)。

特性

  • 多种输入类型 — 支持对象、数组和原始值作为样式输入
  • 扁平驼峰输出 — 所有属性名自动转换为驼峰命名
  • 数组合并 — 合并数组,最后的元素优先级最高
  • 嵌套对象扁平化 — 嵌套选择器以连字符分隔的前缀展平
  • 条件样式null / undefined / false 值会被静默跳过
  • 属性移除 — 将属性设为 null 可从结果中移除
  • 循环引用检测 — 检测到循环引用时抛出错误
  • 布尔值处理 — 发出警告并忽略
  • 迭代保护 — 强制最大迭代次数以防止无限循环
  • 零依赖 — 无运行时依赖

API

resolveStyles(...inputs: StyleInput[]): Record<string, string>

接收任意数量的样式输入,返回一个扁平的 Record<string, string>,键为驼峰命名,值为 CSS 字符串。

合并优先级

输入按逆序处理 — 最后一个输入具有最高优先级。数组中的元素也是最后的元素优先级最高。

resolveStyles({ color: 'red', fontSize: '14px' }, { color: 'blue' }, { fontSize: '18px' });
// → { color: 'blue', fontSize: '18px' }

数组合并

数组会被展平并合并,后面的元素会覆盖前面的:

resolveStyles([{ color: 'red' }, { color: 'blue', fontSize: '14px' }]);
// → { color: 'blue', fontSize: '14px' }

嵌套对象

嵌套对象以连字符分隔的键展平,适用于伪类、媒体查询或嵌套选择器:

resolveStyles({
  '&:hover': {
    color: 'red',
    backgroundColor: 'blue',
  },
});
// → { '&:hover-color': 'red', '&:hover-backgroundColor': 'blue' }

条件样式

假值(nullundefinedfalse)会被跳过,方便实现条件样式:

const isActive = false;

resolveStyles({ color: 'red' }, isActive && { color: 'blue' }, { fontSize: '16px' });
// → { color: 'red', fontSize: '16px' }

属性移除

将属性设为 null 可从最终结果中移除该属性:

resolveStyles({ color: 'red', fontSize: '16px' }, { fontSize: null });
// → { color: 'red' }

已废弃的输入

将原始 stringnumber 作为顶层输入已被废弃,会产生警告。请改用对象语法。

类型

export type StyleInput = StyleInput[] | StyleObject | StylePrimitive;

export interface StyleObject {
  [key: string]: StyleInput;
}

export type StylePrimitive = null | number | string | undefined;

License

MIT