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

immot

v0.3.4

Published

Create a new copy without changing the original data.

Downloads

19

Readme

immot

NPM version Build status Test coverage Downloads Minified size Gzip size

在不改变原始数据的情况下,创建新的拷贝。

安装

pnpm i immot

或者

yarn add immot

介绍

不可变数据 是函数式编程中极其重要的一个基本概念。Reactstate 设计为只读,即 不可变数据,只能通过 setState 修改。此模块可以完美配合 React,以简化 setState 操作不可变数据的繁琐步骤。

React 中,你不能这样做:

state.a.b.c = 1;
// or...
state.c.d.f.push(2);

通常,你会这样做:

const nextState = {
  ...state,
  a: {
    ...state.a,
    b: {
      ...state.a.b,
      c: 1,
    },
  },
};

现在有了 immot,让操作深层数据变得简单:

const nextState = $setIn(state, ['a', 'b', 'c'], 1);

nextState === state;
// false

注意 immot 不是深拷贝,深拷贝操作极其昂贵,无法适用于日常开发。immot 只会创建改变的数据,会引用原对象中未改变的部分。

immot 的 API 灵感来自于 immutable-js,但 immutable-js 有独立的结构模型,复杂度高。immot 的设计理念是要求简单、易用,不需要过多的心智负担。因此在设计之初就亲和原生的 JSON 结构,只提供辅助函数,大小 < 1KB,就做到像 immutable-js 一样的效果。

immot 做到了 typescript 类型安全。$updateIn$setIn$mergeIn 中的 keyPath 路径支持类型自动提示(目前只支持小于 7 层结构)。

code.gif

使用

import * as immot from 'immot';

或者只导入其中某个函数

import { $updateIn } from 'immot';

immot 所有函数操作都会返回一个新的对象。

$set

用于设置 对象/数组/Map 中的属性值。keyPath 为字符串。

const result = immot.$set(demo, 'a', 1);

$setIn

用于设置 对象/数组/Map 中的属性值。它可以为深层对象做操作,keyPath 为路径数组

const result = immot.$setIn(demo, ['a', 'b', 1, 'c'], 'good');

$merge

用于合并 对象/数组 中的属性列表。

const result = immot.$merge(demo, { tom: 1, jack: 2 });

const result1 = immot.$merge(demo1, [5, 6]);

$mergeIn

用于合并 对象/数组 中的属性列表。它可以为深层对象做操作,keyPath 为路径数组

const result = immot.$mergeIn(demo, ['a', 1, 'b'], { tom: 1, jack: 2 });

$update

通过回调函数设置 对象/数组/Map 中的属性值。keyPath 为字符串。

const result = immot.$update(demo, 'money', (prev) => prev + 1);

$updateIn

通过回调函数设置 对象/数组/Map 中的属性值。它可以为深层对象做操作,keyPath 为路径数组

const result = immot.$updateIn(demo, ['todoList', 0, 'complete'], (complete) => !complete);

$delete

用于删除 对象/数组/Map 中的可选属性值,keyPath 为字符串或者数组

const result = immot.$delete(demo, 'a1');
const result1 = immot.$delete(demo, ['a1', 'a2']);

$push

类似 Array.prototype.push,但返回新数组

const result = immot.$push(demo, 4);

$pop

类似 Array.prototype.pop,但返回新数组

const result = immot.$pop(demo);

$shift

类似 Array.prototype.shift,但返回新数组

const result = immot.$shift(demo);

$unshift

类似 Array.prototype.unshift,但返回新数组

const result = immot.$unshift(demo, 4);

$splice

类似 Array.prototype.splice,但返回新数组

const result = immot.$splice(demo, 1, 0, 'test');

性能测试

/bench 目录中有性能测试对比的样例,可以 clone 本项目测试

cd bench
pnpm i
node index.mjs

注意:

  1. 数值为每秒操作数量,越高越好
  2. 样例中 immer 关闭了自动冻结对象的特性,否则结果会更差。
  3. 数组性能测试图中隐藏了 immutableJS 数据,用空间换取时间的方式导致数值太高,影响对比。

在 Node v14.17.0 的测试结果:

常规数据和深层数据 Performance

50000 长度的数组 Performance1