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

spawn-object

v1.2.1

Published

辅助 TableStore 列操作的工具函数,能够将大对象拆分为仅包含单个属性对象的列表

Downloads

9

Readme

spawn-object

专用于 TableStore 列操作的工具函数,能够将大对象拆分为仅包含单个属性对象的列表

介绍

非常轻量的的一个工具函数,将以下形式的对象:

{
  name: 'mark',
  age: 19,
  isGood: true,
}

转化为以下形式:

[
  {
    name: 'mark',
  },

  {
    age: 19,
  },

  {
    isGood: true,
  },
]

使用说明

除了将大对象拆分为小对象数组外,本工具还提供了以下异常处理:

  • 如果属性值为对象(object)和数组(array),那么进行一次 JSON.stringify 转化。
  • 如果属性值为整数(integer),那么将其转化为 TableStore 能够识别的整数类型。
  • 如果属性值为空或其他 TableStore 不能处理的数据类型,自动过滤。

我们来模拟一个常见需求,用户编辑个人资料,提交后,需要将信息存起来,有些项目必填,有些选填,因此服务端需要判断是否为空(undefined 存入 TableStore 会报错)。

假设需要存入的对象属性如下:

const userInfo = {
  name: 'mark',
  age: 19,
  height: 170.5,
  idGood: true,
  tag: ['one', 'two', 'three'],
  extInfo: {
    nickname: 'good mark',
  },
}

如果不使用本工具,那么 attributeColumns 属性需要这么写:

attributeColumns: [
  {
    name: userInfo.name,
  },

  {
     age: TableStore.Long.fromNumber(userInfo.age),
  },

  {
     height: userInfo.height,
  },

  {
     idGood: userInfo.idGood,
  },

  {
     tag: JSON.stringify(userInfo.tag),
  },

  {
     extInfo: JSON.stringify(userInfo.extInfo),
  },
    ],

以上这种写法还是在不考虑值可能为空的情况下,实际上,TableStore 不允许存入 undefined,你需要过滤掉异常值,正常情况下,使用原生的写法,将会异常麻烦,以下是使用了本工具之后的写法:

首先引入

const spa = require('spawn-object')

属性列的写法:

attributeColumns: spa(userInfo)

使用场景

当前工具库仅用于阿里云(Aliyun)的表格存储(TableStore)的列(column)操作(可同时用于主键列和属性列),提供了一个更便捷的操作方式,在其他场景下可能并不适用。