Many APIs, public or not, return JSON data that has deeply nested objects. Using data in this kind of structure is often very difficult for JavaScript applications, especially those using Flux or Redux.
大部分 API(无论是否公开),返回的 JSON 数据都含有深层的嵌套对象。对于 JavaScript 应用(特别是使用 Flux 或 Redux),使用该类型数据时常常会感到困难。
Normalizr is a small, but powerful utility for taking JSON with a schema definition and returning nested entities with their IDs, gathered in dictionaries.
Normalizr 是一个轻量却强大的工具,它通过定义 schema 将 JSON 转换为带有自身 ID 的 嵌套实体,汇集在字典中。
Consider a typical blog post. The API response for a single post might look something like this:
考虑一个典型的博客帖子。API 返回一个帖子可能是以下数据:
{
"id": "123",
"author": {
"id": "1",
"name": "Paul"
},
"title": "My awesome blog post",
"comments": [
{
"id": "324",
"commenter": {
"id": "2",
"name": "Nicole"
}
}
]
}
We have two nested entity types within our article
: users
and comments
. Using various schema
, we can normalize all three entity types down:
我们的 article
内有两个嵌套的实体:users
和 comments
。使用各自 schema
,我们能规范化(normalize
)前面提到的三个实体类型:
import { normalize, schema } from 'normalizr';
// Define a users schema
// 定义一个 users schema
const user = new schema.Entity('users');
// Define your comments schema
// 定义你的 comments schema
const comment = new schema.Entity('comments', {
commenter: user
});
// Define your article
// 定义你的 article
const article = new schema.Entity('articles', {
author: user,
comments: [ comment ]
});
const normalizedData = normalize(originalData, article);
Now, normalizedData
will be:
现在,normalizedData
变量将是:
{
result: "123",
entities: {
"articles": {
"123": {
id: "123",
author: "1",
title: "My awesome blog post",
comments: [ "324" ]
}
},
"users": {
"1": { "id": "1", "name": "Paul" },
"2": { "id": "2", "name": "Nicole" }
},
"comments": {
"324": { id: "324", "commenter": "2" }
}
}
}
None.
无。
Normalizr was originally created by Dan Abramov and inspired by a conversation with Jing Chen. Since v3, it was completely rewritten and maintained by Paul Armstrong. It has also received much help, enthusiasm, and contributions from community members.
Normalizr 最初由 Dan Abramov 创建,这源于他与 Jing Chen 的对话中受到了启发。从 v3 开始,Normalizr 被 Paul Armstrong 完全重写并维护。它也从 社区成员 中得到很多的帮助、鼓励与贡献。