Skip to content
This repository was archived by the owner on Mar 20, 2022. It is now read-only.

Commit c0f85b2

Browse files
committed
Add type declarations
1 parent f80b831 commit c0f85b2

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

index.d.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
export type ExtractAttribute =
2+
(entity: any) => string;
3+
4+
export type AssignEntity =
5+
(obj: any, key: string, entity: any, input: any, schema: SchemaValue) => void;
6+
7+
export type MergeIntoEntity =
8+
(entityA: any, entityB: any, entityKey: string) => void;
9+
10+
export type SchemaOptions = {
11+
idAttribute?: string | ExtractAttribute;
12+
meta?: any;
13+
assignEntity?: AssignEntity;
14+
}
15+
16+
export type IterableSchemaOptions = {
17+
schemaAttribute?: string | ExtractAttribute;
18+
}
19+
20+
export type UnionSchemaOptions = {
21+
schemaAttribute: string | ExtractAttribute;
22+
}
23+
24+
export type NormalizeOptions = {
25+
assignEntity?: AssignEntity;
26+
mergeIntoEntity?: MergeIntoEntity;
27+
}
28+
29+
export type NormalizeInput = Object | Array<Object>;
30+
31+
export type NormalizeOutput = {
32+
result: any;
33+
entities?: any;
34+
}
35+
36+
export class Schema {
37+
constructor (key: string, options?: SchemaOptions);
38+
39+
define(schema: SchemaMap): void;
40+
getKey(): string;
41+
getIdAttribute(): string;
42+
getMeta(prop: string): any;
43+
}
44+
45+
export class IterableSchema {
46+
constructor (schema: SchemaValue, options?: IterableSchemaOptions);
47+
48+
getItemSchema(): SchemaValue;
49+
}
50+
51+
export class UnionSchema {
52+
constructor (schema: SchemaValue, options: UnionSchemaOptions);
53+
54+
getItemSchema(): SchemaValue;
55+
}
56+
57+
export type SchemaValue = Schema | IterableSchema | UnionSchema | SchemaMap;
58+
59+
export type SchemaMap = {
60+
[key: string]: SchemaValue;
61+
}
62+
63+
export function arrayOf(
64+
schema: SchemaValue,
65+
options?: IterableSchemaOptions
66+
): IterableSchema;
67+
68+
export function valuesOf(
69+
schema: SchemaValue,
70+
options?: IterableSchemaOptions
71+
): IterableSchema;
72+
73+
export function unionOf(
74+
schema: SchemaValue,
75+
options?: UnionSchemaOptions
76+
): UnionSchema;
77+
78+
export function normalize(
79+
input: NormalizeInput,
80+
schema: SchemaValue,
81+
options?: NormalizeOptions
82+
): NormalizeOutput;

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.1.0",
44
"description": "Normalizes JSON according to schema for Redux and Flux applications",
55
"main": "lib/index.js",
6+
"typings": "index.d.ts",
67
"repository": {
78
"type": "git",
89
"url": "https://github.com/paularmstrong/normalizr.git"
@@ -44,6 +45,8 @@
4445
"chai": "^3.2.0",
4546
"mocha": "^2.4.5",
4647
"rimraf": "^2.4.2",
48+
"typescript": "^1.8.10",
49+
"typescript-definition-tester": "0.0.4",
4750
"webpack": "^1.13.0"
4851
},
4952
"dependencies": {

test/typescript.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as tt from 'typescript-definition-tester';
2+
3+
describe('TypeScript definitions', function () {
4+
this.timeout(0);
5+
6+
it('compile against index.d.ts', (done) => {
7+
tt.compileDirectory(
8+
__dirname + '/typescript',
9+
fileName => fileName.match(/\.ts$/),
10+
() => done()
11+
);
12+
});
13+
14+
});

test/typescript/normalize.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {Schema, normalize, arrayOf, unionOf, valuesOf} from "../../index.d.ts";
2+
3+
const isObject = function (obj: any) {
4+
return typeof obj === 'object';
5+
};
6+
7+
const assignEntity = function (output: any, key: string, value: any) {
8+
if (key === "timestamp") {
9+
output[key] = Date.now()
10+
}
11+
output[key] = value;
12+
};
13+
14+
const mergeIntoEntity = function (entityA: any, entityB: any, entityKey: string) {
15+
for (const key in entityB) {
16+
if (!entityB.hasOwnProperty(key)) {
17+
continue;
18+
}
19+
20+
if (!entityA.hasOwnProperty(key)) {
21+
entityA[key] = entityB[key];
22+
continue;
23+
}
24+
25+
if (isObject(entityA[key]) && isObject(entityB[key])) {
26+
// Merge the two entities.
27+
continue;
28+
}
29+
}
30+
};
31+
32+
const user = new Schema("users");
33+
const group = new Schema("groups", { assignEntity });
34+
const member = unionOf({ users: user, groups: group }, { schemaAttribute: "type" });
35+
36+
group.define({
37+
members: arrayOf(member),
38+
owner: member,
39+
relations: valuesOf(member)
40+
});
41+
42+
const normalizeWithObject = normalize({}, { group });
43+
const normalizeWithArray = normalize([], { groups: arrayOf(group) });
44+
const normalizeWithAssignEntity = normalize([], { groups: arrayOf(group) }, { assignEntity });
45+
const normalizeWithMergeIntoEntity = normalize([], { groups: arrayOf(group) }, { mergeIntoEntity });

test/typescript/schema.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Schema, IterableSchema, UnionSchema, SchemaValue} from "../../index.d.ts";
2+
3+
const idAttribute = "slug";
4+
const meta = { removeProps: ["year", "publisher"] };
5+
6+
const generateSlug = function (entity: any) {
7+
if (entity.slug != null) {
8+
return "slug";
9+
}
10+
return "id";
11+
};
12+
13+
const assignEntity = function (output: any, key: string, value: any, input: any, schema: SchemaValue) {
14+
const itemSchema = (schema instanceof IterableSchema || schema instanceof UnionSchema) ?
15+
schema.getItemSchema() : schema;
16+
const removeProps = (itemSchema instanceof Schema) ?
17+
itemSchema.getMeta("removeProps") : null;
18+
if (!removeProps || removeProps.indexOf(key) < 0) {
19+
output[key] = value;
20+
}
21+
};
22+
23+
const schemaWithKey = new Schema("articles");
24+
const schemaWithStringIdAttribute = new Schema("articles", { idAttribute });
25+
const schemaWithFunctionIdAttribute = new Schema("articles", { idAttribute: generateSlug });
26+
const schemaWithMeta = new Schema("articles", { meta });
27+
const schemaWithAssignEntity = new Schema("articles", { meta, assignEntity });
28+
29+
const someKey: string = schemaWithKey.getKey();
30+
const someIdAttribute: string = schemaWithStringIdAttribute.getIdAttribute();
31+
const someOtherIdAttribute: string = schemaWithFunctionIdAttribute.getIdAttribute();
32+
const someMeta: any = schemaWithMeta.getMeta("removeProps");
33+
34+
const article = new Schema("articles");
35+
const user = new Schema("users");
36+
article.define({
37+
author: user
38+
});

0 commit comments

Comments
 (0)