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

flextypeonjs

v2.0.1

Published

Universal runtime type inference & auto-conversion — strings to numbers, booleans, objects, arrays, and more

Readme

FlexType

一个 JavaScript 运行时类型推断与自动转换库。将字符串形式的数字、布尔值、JSON 等自动转换为对应的 JavaScript 类型,可作为通用变量类型使用。

特性

  • 自动类型转换"123"123"true"true'{"x":1}'{x:1}"[1,2]"[1,2]
  • 类型锁定机制strLock() / boolLock() / typeLock() 防止不必要的转换
  • 数学运算add / subtract / multiply / divide,自动处理类型安全
  • 数组/对象操作get() / set() / push() 链式访问嵌套数据
  • 批量声明declareFlex() 一次处理多组变量
  • 跨平台:ESM / CJS / UMD,支持 Node.js 和浏览器
  • 零依赖:核心源码约 110 行,构建后 ~4.5KB

安装

npm install flextypeonjs

CDN:

<script src="https://cdn.jsdelivr.net/gh/Mxher07/flextype@main/dist/flextype.umd.js"></script>
<script>
const { FlexType, flex, declareFlex } = FlexTypeRuntime;
</script>

快速开始

import { flex } from 'flextypeonjs';

const age = flex('age', '25');
console.log(age.value); // 25
console.log(age.type);  // "number"

const active = flex('active', 'true');
console.log(active.value); // true
console.log(active.type);  // "boolean"

const data = flex('data', '{"items": [1, 2, 3]}');
console.log(data.value);    // { items: [1, 2, 3] }
console.log(data.type);     // "object"

API

flex(name, value, options?)

创建单个 FlexType 实例。

flex('port', '3000');
flex('debug', 'true', { boolLock: true });

declareFlex(variables, options?)

批量创建。

const cfg = declareFlex({
  port: '3000',
  debug: 'true',
  db: '{"host":"localhost"}'
});
// cfg.port.value -> 3000
// cfg.debug.value -> true

FlexType 实例

属性

| 属性 | 类型 | 说明 | |------|------|------| | .value | any | 转换后的值 | | .type | string | 推断类型 (number, string, boolean, array, object, null, undefined, nan) | | .name | string | 变量名 | | .isLocked | boolean | 是否存在锁定 |

类型检查

.isString() .isNumber() .isBoolean() .isArray() .isObject() .isNull() .isUndefined()

锁定

flex('id', '123').strLock();      // 保持字符串 "123"
flex('flag', true).boolLock();    // true 参与运算时视为 1,结果限制在 [0,1]
flex('val', 'hello').typeLock();  // 不做任何类型转换
.unlock()                         // 解除所有锁定

数学运算

flex('a', '10').add(5).value;       // 15
flex('a', '10').subtract(3).value;  // 7
flex('a', '10').multiply(2).value;  // 20
flex('a', '10').divide(5).value;    // 2

数组 / 对象

flex('arr', [1,2,3]).get(1).value;   // 2
const arr = flex('arr', [1]);
arr.push(2, 3);                       // [1, 2, 3]

flex('obj', {x:1}).get('x').value;   // 1
const obj = flex('obj', {x:1});
obj.set('y', 2);                      // {x:1, y:2}

显式类型转换

flex('a', 42).toString().value;     // "42"
flex('a', '42').toNumber().value;   // 42
flex('a', 0).toBoolean().value;     // false

调试

flex('x', '123').debug();
// { name: 'x', value: 123, type: 'number', raw: '123', options: {}, isLocked: false }

使用场景

配置文件 / 环境变量

import { declareFlex } from 'flextypeonjs';

const config = declareFlex({
  port: process.env.PORT || '3000',
  debug: process.env.DEBUG || 'false',
  features: process.env.FEATURES || '["auth", "api"]'
});
// config.port.value  -> 3000 (number)
// config.debug.value -> false (boolean)

表单数据处理

const form = declareFlex({
  age: '25',
  newsletter: 'true'
});
// form.age.value        -> 25 (number)
// form.newsletter.value -> true (boolean)

布尔值数学运算

const active = flex('active', true).boolLock();
const result = active.subtract(1).toBoolean();
console.log(result.value); // false

许可证

MIT License