0ui

中文文档
1.2.0发布于 2018/03/27MIT

A set of utilities for building UIs with React

原始使用文档
文档目录9 节
  1. UMD build
  2. core lib
  3. Stupid pattern matching
  4. imtbl
  5. State
  6. State object
  7. Derived state
  8. Multimethod
  9. UI

A set of utilities for building UIs with React

Demo

v1.2.0

npm i 0ui

See an example of how everything is used together in example/src/index.js

UMD build

<script src="https://unpkg.com/0ui/dist/0ui.umd.js"></script>

core lib

lib/core.js

Random utilities

Stupid pattern matching

More like a shorthand for switch/case

import { match } from "0ui/lib/core";

match("loading")({
  loading: () => console.log("Loading"),
  error: () => console.log("Error")
});
// "Error"

imtbl

Immutable style data manipulation for plain JavaScript objects, see imtbl lib.

State

lib/state.js

State object

Independent state object

import State from "0ui/lib/state";

const state = State.create(0);

state.addWatch("key", (key, state, prevVal, nextVal) => {
  console.log(prevVal, nextVal);
});

state.swap(val => val + 1); // 0 1

Derived state

lib/derived-state.js

Derives state -> state' with a function and propagates updates from state -> state'

import State from "0ui/lib/state";
import DerivedState from "0ui/lib/derived-state";

const bucket = State.create({
  items: [
    {
      price: 104.5
    },
    {
      price: 78.2
    }
  ]
});

const bucketPrice = DerivedState.create(bucket, ({ items }) =>
  items.reduce((sum, { price }) => sum + price, 0)
);

bucketPrice.state; // 182.7

bucket.swap(removeFirstItem);

bucketPrice.state; // 178.2

Multimethod

lib/multimethod.js

Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case some other attribute, of more than one of its arguments. Wikipedia

import MultiMethod from "0ui/lib/multimethod";

const toJSON = MultiMethod.create(v => {
  if (Array.isArray(v)) {
    return "array";
  } else {
    return typeof v;
  }
});

toJSON.addMethod("string", v => JSON.stringify(v));

toJSON.addMethod("number", v => v.toString());

toJSON.addMethod("array", arr => {
  const jsonArr = arr.map(v => toJSON.dispatch(v)).join(", ");
  return `[${jsonArr}]`;
});

toJSON.addMethod("object", o => {
  const jsonObj = Object.entries(o)
    .map(([k, v]) => toJSON.dispatch(k) + ":" + toJSON.dispatch(v))
    .join(", ");
  return `{${jsonObj}}`;
});

toJSON.dispatch({
  items: [1, "two"]
});
// {"items":[1, "two"]}

UI

lib/ui.js

React Component API wrapper. Subscribes to multiple State objects.

import { createComponent } from "0ui/lib/ui";
import State from "0ui/lib/state";

const Counter = createComponent({
  states: {
    count: State.create(0) // local state
  },
  render(states, props) {
    const { count } = states;

    return (
      <div>
        <button onClick={() => count.swap(v => v - 1)}>-</button>
        <span>{count.state}</span>
        <button onClick={() => count.swap(v => v + 1)}>+</button>
      </div>
    );
  }
});

版本与发布记录

0ui 共发布 3 个版本,首次发布于 2018/03/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2018/03/278 年前
1.1.02018/03/268 年前
1.0.02018/03/268 年前

关于 0ui 的常见问题

0ui 是什么?

一组用于用React构建ui的实用程序

如何安装 0ui?

在项目目录运行 npm install 0ui,或使用 pnpm add / yarn add / bun add。

0ui 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/0ui 是否存在。

0ui 可以商用吗?

0ui 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

0ui 还在维护吗?

最新版本 1.2.0 发布于 2018/03/27(8 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/03/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。