- extend(BaseRecord, descriptor, [label])
Extends a record type and allows the addition of new fields
- Maybe(Type, [defaultValue])
Defines an optional type, similar to typed-immutable's Maybe, but provides extended options.
Benefits over typed-immutable's Maybe:
- Allows both
undefined
andnull
as values - Allows defining a default value for when the value is
undefined
- Extracts the default value from the Type parameter if one is defined
- Allows both
- Enum(enumValues, [defaultValue])
Restricts the values which can be set on a property.
- Discriminator(property, typeMap, [defaultType])
Chooses the type to use based on the value of a property.
Extends a record type and allows the addition of new fields
Kind: global function
Param | Type | Description |
---|---|---|
BaseRecord | Record |
Record to extend |
descriptor | object |
Descriptor object of new fields to add |
[label] | string |
Label for the new Record type |
Example
const BaseValue = Record({
type: String,
});
const StringValue = extend(BaseValue, {
value: String,
});
const NumberValue = extend(BaseValue, {
value: Number,
});
Defines an optional type, similar to typed-immutable's Maybe, but provides extended options.
Benefits over typed-immutable's Maybe:
- Allows both
undefined
andnull
as values - Allows defining a default value for when the value is
undefined
- Extracts the default value from the Type parameter if one is defined
Kind: global function
Param | Type | Description |
---|---|---|
Type | * |
Type of the value |
[defaultValue] | * |
Default value (must be undefined , null , or of the specified Type) |
Example
const MyRecord = Record({
//Required string
id: String,
//Required string with a default value
name: 'Some Name',
//Optional string - defaults to undefined
value: Maybe(String),
//Optional string with a default value
type: Maybe(String, 'point'),
//Optional string with a default value (extracted from the type)
text: Maybe('Some Text'),
//Optional string with a default value of null
title: Maybe(String, null),
});
Restricts the values which can be set on a property.
Kind: global function
Param | Type | Description |
---|---|---|
enumValues | Array.<*> |
Array of possible values |
[defaultValue] | * |
Default value (must be in the set of enumValues) |
Example
const MyRecord = Record({
//Restricts values to "text" and "image"
type: Enum(['text', 'image']),
//Restricts values to "left", "center" and "image" with a default value of "left" if the value is undefined
alignment: Enum(['left', 'center', 'right'], 'left')
});
Chooses the type to use based on the value of a property.
Kind: global function
Param | Type | Description |
---|---|---|
property | string |
Property to use for determining the type |
typeMap | object.<Record> |
Map of property values to types |
[defaultType] | Record |
Default type when no property value is found in the typeMap |
Example
const StringValue = Record({
type: String,
value: String,
});
const NumberValue = Record({
type: String,
value: Number,
});
const AnyValue = Record({
type: String,
value: Any,
});
const MyRecord = Record({
//Chooses the Record type to use based on the "type" property value
value: Discriminator('type', {
'string': StringValue,
'number': NumberValue,
}),
//Defaults to AnyValue if no matching type is found
other: Discriminator('type', {
'string': StringValue,
'number': NumberValue,
}, AnyValue),
});