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

data-envelope

v1.1.1

Published

Get data safely

Downloads

4

Readme

data-envelope

数据包装器

NPM: https://www.npmjs.com/package/data-envelope

安装

npm i data-envelope

初衷

我们可能经常需要这样处理数据

if (result.data.data.data1.code === 0) return false;
result.data.data.data1 = result.data.data.data1 || {};
result.data.data.data1.data = result.data.data.data1.data || {};

if(!result.data.data.data1.data.itemInfoVo){
  result.data.data.data1.data.itemInfoVo = {};
}
//...

result.data.data.data1.data这种调用,无论是读取或是赋值,都是不安全的。如果数据对象结构不严格,中间的路径链条断掉,则会发生报错。

安全的做法是:

// if (result.data.data.data1.code === 0) return false;
if(result
    && result.data
    && result.data.data
    && result.data.data.data1
    && result.data.data.data1.code === 0) return false

为了避免这种书写问题,使用data-envelope进行安全读取,代码改动成本较低,可以有效增强系统健壮性。

const { envelope } = require('data-envelope');

//...

const r = envelope(result);

// if (result.data.data.data1.code === 0) return false;
if(r.pick('data.data.data1.code') === 0) return false;

// result.data.data.data1 = result.data.data.data1 || {};
r.inject(
    'data.data.data1',
    r.pick('data.data.data1', {})
);

// result.data.data.data1.data = result.data.data.data1.data || {};
r.inject(
    'data.data.data1.data',
    r.pick('data.data.data1.data', {})
);

// if(!result.data.data.data1.data.itemInfoVo){
if(!r.pick('data.data.data1.data.itemInfoVo')){
  // result.data.data.data1.data.itemInfoVo = {};
  r.inject('data.data.data1.data.itemInfoVo', {})
}
//...

这看起来并不优雅,但确实可以增强健壮性。更优雅的方案,建议使用Proxy对象进行对象标签拦截,但Proxy对ES版本要求较高,会有兼容问题。

安装

$ npm i data-envelope

使用

const { envelope } = require('data-envelope');

const env = envelope({
    a: {
        b: ['a.b.0', 'a.b.1'],
        c: { 'a.b': 'bingo' }
    },
});

env.pick('a.b', '##defaultValue##');
env.pick('a.c', '##defaultValue##');
env.pick('a.c[a.b]', '##defaultValue##');

env.inject('aaaa[0]', 123);
env.inject('bbbb.ccc', 123);
env.inject(['c', 1], 123);
env.inject(['a', 'b', 'c', '0', 'a'], 123);

模组

DataEnvelope

对数据进行简单封装,开放pick inject来拾取和注入路径值值。

picker

可以安全地拾取对象路径的值。当路径不通或者拾取失败,则返回用户指定的默认值。

injecter

可以按指定路径,向对象注入值。

path-resolver

解析路径表达式,兼容大部分对象/数组标签取值写法,规则相对要松散一些。

a.b.c => ['a', 'b', 'c']
a.b[0]["a.b.c"] => ['a', 'b', '0', 'a.b.c']
['a'].6[b.c][d] => ['a', '6', 'b.c', 'd']
'' => 数据本身