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

@aimwhy/transform-data

v0.1.1

Published

transform-data

Downloads

4

Readme

背景

平时接口返回的数据,可能会有冗余或后期不断迭代与原有字段已改名

所以想用一种更直观的方式来映射转换数据

dataTransform(data, pattern) 由此由来。 data 为原数据、 pattern 为映射模式, 按照 pattern 的形状进行填充数据, 属性的值为 data 中的 path, 从而拿到 data[path] 中的值填充。

当然有的时候是想填充真正的字面量值, 此时只需要用 Value('aaa') 就可拿到 'aaa'

当然还有更多的功能,如: OriginRemoveRetainValue 来方便映射, 导出的 api 十分简单, 源代码也只有 90 行。

结构提取数组中的值

import { dataTransform, Origin } from "@aimwhy/transform-data";

console.log(
  dataTransform(
    {
      a: 44,
      e: [
        { a: 'a', b: 'b' },
        { a: 'a1', b: 'b1' },
        { a: 'a2', b: 'b2' },
      ],
    },
    {
      e2: ['a', Origin, 'e'], // 参考原数据 path 为 'e' 指向的数据
    }
  );
);

// { e2: [ 'a', 'a1', 'a2' ] }

导出的字段

| export | description | type | dep | | ------------------- | ----------- | -------- | -----| | Origin | 指向原数据的 path | string | - | | Remove | 移除的属性 「参考 Origin 指向的对象」 | string[] | Origin | | Retain | 保留的属性 「参考 Origin 指向的对象」 | string[] | Origin | | Value | 直接生成具体值 | function | - | | dataTransform | 转换函数 | function | - |

重命名 Key

import { dataTransform } from "@aimwhy/transform-data";

console.log(
  dataTransform(
    {
      a: '1',
      b: '2',
      c: '3'
    },
    {
      a1: 'a',
      b1: 'b',
      c1: 'c'
    }
  )
);
// { a1: '1', b1: '2', c1: '3' }
import { dataTransform, Retain, Value } from "@aimwhy/transform-data";

console.log(
  dataTransform(
    {
      a: '1',
      b: '2',
      c: '3',
      f: [2, 3],
      g: { g1: {g11: 'g11'}, g2: {g22: 'g22'}, g3: {g33: 'g33'} }
    },
    {
      b1: 'b',
      c1: Value('c'), // 使用传入的值作为最终值,而非模式
      x: { [Origin]: 'g', [Retain]: ['g1'], ff: 'g2.g22'}, // 参考 ’g‘ 指向的数据生成
      y: (record) => record['g.g3.g33'] + record['a']  // 用函数自己生成,record 为原数据各层 path 的记录
    }
  )
);
// { b1: '2', c1: 'c', x: { g1: { g11: 'g11' }, ff: 'g22' }, y: 'g331' }

让数据映射转换 更直观