serialized
v1.0.0
Published
Serialize javascript objects into compact strings
Downloads
9
Maintainers
Readme
serialized
Serialize and deserialize javascript objects into compact, url encoded strings. Useful when you have to store rich objects (such as ElasticSearch queries) in browser url.
Example
// Describe field types
const ProductField = oneOfType([
constant('id'),
constant('name'),
constant('price'),
constant('category'),
constant('manufacturer')
])
const PriceRange = mapOf(
constant('price'),
objectOf({ gte: numberType(), lte: numberType() })
)
function getType(productField) {
switch (productField) {
case 'id': return integerType()
case 'price': return numberType()
default: return stringType()
}
}
const TermOrWildcard = withCalculatedType(
getType,
(from, Calculated) => mapOf(from(ProductField), Calculated)
)
const boolType = template(T => oneOfType([
objectOf({ should: arrayOfType(T), must: arrayOfType(T) }),
objectOf({ should: arrayOfType(T) }),
objectOf({ must: arrayOfType(T) })
]))
const Query = oneOfType([
objectOf({ term: TermOrWildcard }),
objectOf({ wildcard: TermOrWildcard }),
objectOf({ range: PriceRange }),
objectOf({ bool: boolType(() => Query) })
])
const Search = objectOf({
from: integerType(),
size: oneOfType([constant(10), constant(100), constant(1000)]),
query: Query
})
// Complex query
const search = {
from: 15,
size: 100,
query: {
bool: {
must: [{
bool: {
should: [
{ wildcard: { name: '*tablet*' }},
{ term: { category: 'Electronics/Tablets' }}]}}, {
bool: {
should: [{
bool: {
must: [
{ term: { manufacturer: 'Apple' }},
{ range: { price: { gte: 0, lte: 1000 }}}]}}, {
bool: {
must: [
{ term: { manufacturer: 'Samsung' }},
{ range: { price: { gte: 0, lte: 500 }}}]}}]}}]}}}
// Serialize object
const serializedString = Search.serialize(search)
//=> f$1323111*tablet*$$03Electronics%2FTablets$$$313204Apple$$20$1000$$$3204Samsung$$20$500
Search.deserialize(serializedString)
//=> equals to 'search'API
stringType([length])
Simple string. By default serializes strings with encodeURIComponent
lengthAccept only strings with providedlength
integerType([max])
Positive integer
maxAccept only integers between0andmax
numberType()
Simple number
constant(value)
valueAny javascript primitive type
objectOf(schema)
schemaObject withtokentype values
const Person = objectOf({
name: stringType(),
age: integerType(),
})mapOf(keyToken, valueToken)
Javascript object as map
keyTokenType of map keysvalueTokenType of map values
oneOfType(tokens)
Union type
tokensArray of possible types
const OptionalString = oneOfType([
constant(undefined),
constant(null),
stringType(),
])arrayOfType(token, [length])
tokenType of array itemlengthAccept only arrays with providedlength
const Matrix = arrayOfType(numberType(), 9)template(callback)
const nullableType = template(T => oneOfType([
constant(null),
T,
]))
const NullableString = nullableType(stringType())withCalculatedType(getType, callback)
Utility for deferred type calculation
