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 🙏

© 2024 – Pkg Stats / Ryan Hefner

path-object-builder

v1.0.4

Published

PathObjectBuilder

Downloads

54

Readme

PathObjectBuilder

在日常使用中我们经常会需要快速操作一个对象的某个属性。当层次比较深时,可通过路径来快速建立对象和获取对象会非常方便,同时为接口也可减少很多压力。


使用方法

安装

npm install path-object-builder

初始化

PathObjectBuilder([Objedt])

Objedt 为初始化的对象数据 非必填

//创建一个空对象
const builder = new PathObjectBuilder();

//创建一个空对象
const builder = new PathObjectBuilder({});

//创建一个{a:1}的对象
const builder = new PathObjectBuilder({ a: 1 });

设置及获取

// 设置初始数据
const builder = new PathObjectBuilder({...});
// 设置或替换数据可省略result
builder.foo = {bar:{value:'test'}}
// 设置数据
builder['<path>']=value
// 通过path获取数据
console.log(builder['<path>'])
// 通过object获取数据
console.log(builder.foo.bar.value) // return 'test'

<path>:是一个以"."分隔的路径. 如果没有设置数据,则返回 null.数组可以以[]表示.会榆次增加.如果需要修改指字数组中的数据可以指定索引如[12]

获取结果集

console.log(JSON.stringify(builder.result, null, 2));

方法

toString

返回 字符串 数据

console.log(builder.toString());
console.log(String(builder));

toJSON

返回 JSON 数据

console.log(builder.toJSON());
console.log(JSON.parse(builder));

测试代码

0

const builder = new PathObjectBuilder();
// 设置数据
builder["aaa.bbb.a1a"] = 111;
builder["aaa.bbb.a2a.aa"] = 123;
builder["aaa.bbb.a2a.bb"] = 321;
builder["aaa.bbb.a3a[].aa"] = 123;
builder["aaa.bbb.a3a[].bb"] = 321;
// 获取数据
console.log(builder["*"]);
console.log(builder["aaa"]);
console.log(builder["aaa.bbb"]);
console.log(builder["aaa.bbb.a1a"]);
console.log(builder["aaa.bbb.a3a"]);
console.log(builder["aaa.bbb.a3a[1]"]);
console.log(builder["aaa.bbb.a3a[1].bb"]);
console.log(builder["aaa.bbb.a3a[1].bbb"]);
// 获取所有数据
console.log(JSON.stringify(builder.result, null, 2));

1

const builder = new PathObjectBuilder();
// 设置数据
builder["[]"] = {
  aa: 11,
  bb: 22,
};
builder["[]"] = 111;
builder["[]"] = 222;
builder["[].[].[]"] = 111;
builder["[].aaa"] = 111;
builder["[].bbb"] = 222;
// 获取数据
console.log(builder["[]"]);
console.log(builder["*"]);
console.log(builder["[0]"]);
console.log(builder["[1]"]);
console.log(builder["[3]"]);
console.log(builder["[3].[]"]);
console.log(builder["[3].[0].[0]"]);
console.log(builder["[5].bbb"]);
console.log(builder["[5].aaa"]);
// 获取所有数据
console.log(JSON.stringify(builder.result, null, 2));

2

const builder = new PathObjectBuilder();
// 设置数据
builder["aaa.bbb.aa"] = 111;
builder["aaa.bbb.aa.aa"] = 123;
builder["aaa.bbb.aa.bb"] = 321;
builder["aaa.bbb.aa.aa"] = 444;
builder["aaa.bbb.aa.bb"] = 555;
// 获取数据
console.log(builder["*"]);
console.log(builder["aaa"]);
console.log(builder["aaa.bbb"]);
console.log(builder["aaa.bbb.aa.aa"]);
console.log(builder["aaa.bbb.aa.aa.ab"]);
console.log(builder["aaa.bbb.aa.ab"]);
// 获取所有数据
console.log(JSON.stringify(builder.result, null, 2));

3

const builder = new PathObjectBuilder();
// 设置数据
builder["aaa.bbb.a1a"] = 111;
builder["aaa.bbb.a2a.aa"] = 123;
builder["aaa.bbb.a2a.bb"] = "query 321";
builder["aaa.bbb.a3a.[].aa"] = 123;
builder["aaa.bbb.a3a.[].bb"] = 321;
// 获取数据
console.log(builder["*"]);
console.log(builder["aaa"]);
console.log(builder["aaa.bbb"]);
console.log(builder["aaa.bbb.a1a"]);
console.log(builder["aaa.bbb.a3a"]);
console.log(builder["aaa.bbb.a3a.[1]"]);
console.log(builder["aaa.bbb.a3a.[1].bb"]);
console.log(builder["aaa.bbb.a3a.[1].bbb"]);
// 获取所有数据
console.log(JSON.stringify(builder.result, null, 2));

end

TODO:

  • 增加搜索并支持关键字查询及模糊查询.支持通配符: *, ?.
  • 内部改用 Set 替换 Array