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

@what-a-faka/obj-mutation

v0.0.7

Published

The Object Mutation

Downloads

5

Readme

@what-a-faka/obj-mutation

对象字段转化器,用于剔除字段、转化字段、改变键名、合并键值。

初始化

首先,安装依赖:

$ npm install @what-a-faka/obj-mutation

然后在项目中导入:

import ObjMutation from '@what-a-faka/obj-mutation'

接着通过传入字段信息(SCHEMA)与初始化选项(OPTIONS)即可初始化一个对象:

const mutator = new ObjMutation(SCHEMA, OPTIONS)

此时即可通过parse方法获取转化结果:

mutator.parse(DATA)

字段信息

schema主要功能为定义对象的字段转化规则,支持mutateformatcreate,首先我们初始化一个带转化对象:

const data = {
  test1: 1,
  test2: 2,
  test3: ''
}

mutate

const mutator = new ObjMutation({
  test1: {
    mutate: {
      test1_mutate: value => `${value}_mutate`,
    }
  }
})

const result = mutator.parse(data)

最终得到的结果为:

{
  test1_mutate: "1_mutate",
  test2: 2,
}

create

const mutator = new ObjMutation({
  test4: {
    create(araw) {
      return raw.test1 + raw.test2
    }
  }
})

const result = mutator.parse(data)

最终得到的结果为:

{
  test1: 1,
  test2: 2,
  test4: 3
}

format

const mutator = new ObjMutation({
  test2: {
    format: value => 222
  },
})

const result = mutator.parse(data)

最终得到的结果为:

{
  test1: 1,
  test2: 222,
}

选项

  • 参数:
    • {string} schema,表配置项,以下会详细说明
    • {string} options.clean,过滤掉Falsy的key,默认为true
    • {string[]} options.omit,剔除对象的字段
    • {string} options.cleanValue, 过滤掉与cleanValue全等的key,默认会过滤掉所有Falsy

由上可知,默认会过滤掉一切Falsy值:

const data = {
  test1: '',
  test2: 0,
  test3: false,
}
const mutator = new ObjMutation({})
const result = mutator.parse(data)
console.log(result) // {}

若是要过滤掉指定的值,可以传递cleanValue任意值:

const data = {
  test1: '',
  test2: 0,
  test3: false,
}
const mutator = new ObjMutation({}, { cleanValue: '' })
const result = mutator.parse(data)
console.log(result) // {test2: 0, test3: false}

如果不使用过滤功能,只需要将options.clean设为false即可。

omit的用法请参考lodash/omit。