-
Notifications
You must be signed in to change notification settings - Fork 16
/
normalizr.javascript.txt
80 lines (66 loc) · 5.07 KB
/
normalizr.javascript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
NORMALIZR
VERSION ==> #1.4.0
GOAL ==> #Flatten a nested/populated REST JSON response, e.g.:
# {
# a: {
# id: 1,
# a_attr: 2,
# c: {
# id: 3,
# c_attr: 4
# }
# }
# }
#Becomes:
# {
# result: [1],
# entities: {
# a: {
# id: 1,
# a_attr: 2
# c: 3
# },
# c: {
# id: 3,
# c_attr: 4
# }
# }
# }
/=+===============================+=\
/ : : \
)==: DEFINITION :==(
\ :_______________________________: /
\=+===============================+=/
new NORMALIZR.Schema #Declare a SCHEMA, i.e. a resource type
('SCHEMA'[, CONF])->SCHEMA #CONF:
# - idAttribute STR[(OBJ)] (def: 'id')
SCHEMA_VAL #Can be:
# - SCHEMA
# - { VAR: SCHEMA_VAL }
# - NORMALIZR.arrayOf(SCHEMA2[, OPT]): for SCHEMA2_ARR
# - OPT.schemaAttribute 'SCHEMA'[(OBJ)]: for polymorphic types
# - NORMALIZR.valuesOf(SCHEMA2[, OPT]): for { VAR2: SCHEMA2 }
# - OPT.schemaAttribute: same as arrayOf()
SCHEMA.define
({ VAR: SCHEMA_VAL ... }) #Declare the populated fields from SCHEMA (other attributes do not need to be declared).
/=+===============================+=\
/ : : \
)==: USAGE :==(
\ :_______________________________: /
\=+===============================+=/
NORMALIZR.normalize #Returns OBJ[_ARR] but after "flattening" populated resources:
(OBJ[_ARR], SCHEMA_VAL[, OPT]) # - result ID_ARR
# - entities.SCHEMA.ID:
# - ...: non-populated resources, returned as is
# - ID[_ARR|OBJ]: populated resources are replaced by their ID,
# and fills in a top-level entities.SCHEMA instead
#OPT:
# - mergeIntoEntity(OBJ.entities.SCHEMA.ID OBJ, OBJ2, 'SCHEMA'):
# - when assigning OBJ.entities.SCHEMA.ID OBJ = OBJ2,
# whether OBJ has already been assigned or not
# - by def, will merge and console.warn() id conflict
# - assignEntity(OBJ, 'KEY', VAL):
# - function used when deep copying objects, for each attribute
# - def: OBJ.KEY = VAL