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

smarts-object

v1.0.2

Published

[JS] Can find or create complex object by string

Downloads

1

Readme

smarts-object

介绍

根据 JS 对象字符串智能查找对象值,新建对象,改变对象值,并提供 vue.$set 使用帮助。

安装教程

npm i smarts-object

yarn add smarts-object

使用说明

  1. 文件引入 :

    import smartsObj from "smarts-object";

  2. 参数:

    • { string } type [ find,created,change,vue ]【可选的操作类型】
    • { string } propertyName【对象字符串】
    • { object } target【目标对象】
    • { any } value [ null ]【如需添加修改值可选】
  3. 使用:

    例子对象: const example = { a: 1, b: { c: [1, 2, 3] } };

    • 查找
    smartsObj("find", "b.c[0]", example); //1
    smartsObj("find", "d", example); //undefined
    • 新建

    假如查询对象存在,则直接返回查询值;不存在则返回 undefined ,并同时为目标创建新对象,值默认为 null。

    smartsObj("created", "b.d", example); //undefined
    console.log(example); //{"a":1,"b":{"c":[1,2,3],"d":null}}

    添加值:

    //查询对象不存在情况
    smartsObj("created", "d[2]", example, 4); //undefined
    console.log(example); //{"a":1,"b":{"c":[1,2,3]},"d":[null,null,4]}
    //查询对象存在情况
    smartsObj("created", "b.c[0]", example, 4); //1
    console.log(example); //{"a":1,"b":{"c":[1,2,3]}},并不会改变值;

    也可当作对象字符串转对象使用:

    //创建一个规定格式的全新对象
    var tag = {};
    smartsObj("created", "a.b.c", tag, 4); //undefined
    console.log(tag); //{"a":{"b":{"c":4}}}
    • 修改

    与 created 大致相同,只是当查询对象存在时必定修改其值,如未传 value 则默认修改为 null

    //查询对象存在情况
    smartsObj("change", "b.c[0]", example, 4); //1 返回被修改前的值
    console.log(example); //{"a":1,"b":{"c":[4,2,3]}}
    • vue.$set

    因 vue2 响应式原理的问题,不能检测数组和对象的变化,对于未创建的对象更是如此,为了能保证监听一般采用 vue.$set 这个 api,这里提供了方便此 api 的参数生成的方法。

    • 查询对象存在情况
    //返回对象 {"target":[1,2,3],"prop":"0","value":4}
    const { target, prop, value } = smartsObj("vue", "b.c[0]", example, 4);
    //直接使用
    this.$set(target,prop,value)
    • 查询对象不存在情况

    target 会返回最后一个存在的对象; prop 最后一个存在对象的下一个 key 名; value 创建的新对象;

    //返回对象 {"target":{"c":[1,2,3]},"prop":"d","value":{"e": 4}}
    const { target, prop, value } = smartsObj("vue", "b.d.e", example, 4);
    //直接使用
    this.$set(target,prop,value)