Skip to content

Instantly share code, notes, and snippets.

@KaySackey
Last active September 19, 2016 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaySackey/078c6f069fadae81ca553d4b71085706 to your computer and use it in GitHub Desktop.
Save KaySackey/078c6f069fadae81ca553d4b71085706 to your computer and use it in GitHub Desktop.
Creating Typed Arrays with Serialzr
import {
//// isPropSchema,
//// isAliasedPropSchema,
// invariant
// isMapLike,
// isModelSchema
list,
map,
createSimpleSchema,
serializable,
} from "serializr";
function isPropSchema(thing) {
return thing && thing.serializer && thing.deserializer
}
function isAliasedPropSchema(propSchema) {
return typeof propSchema === "object" && !!propSchema.jsonname
}
function isModelSchema(thing) {
return thing && thing.factory && thing.props
}
function isMapLike(thing) {
return thing && typeof thing.keys === "function" && typeof thing.clear === "function"
}
function isConstructor(f){
return typeof f === 'function' && f.prototype.constructor === f;
}
/**
* Similar to the base map() provided by serializr but accepts a second parameter of mapping
* which is used to map key names to specific types.
*
* This can be both plain objects (default) or ES6 Map like structures.
* This will be inferred from the initial value of the targeted attribute.
*
* @example
*
* export class TagFacet {
* @serializable name;
* @serializable text;
* @serializable count;
* constructor({name, text, count}){
* Object.assign(this, {name, text, count});
* }
* }
*
* let mapping = {
* tags : TagFacet,
* default: Object
* };
*
* let facetSchema = createSimpleSchema({
* name: true, text: true, count: true
* });
*
* class FacetContainer{
* @serializable(typedMap(list(object(facetSchema)), mapping)) facets;
* }
*
* @param {PropSchema} propSchema - The schema to be used to deserialize the initial values.
* If not given; then mapping.default will be used if it is a PropSchema.
* @param {Object.<string, Object>} mapping - A simple object that that maps keys to values, as values are types to
* be deserialized to for that key.
*
* @returns {{serializer: function, deserializer: function}}
*/
function typedMap(propSchema, mapping) {
mapping.default = mapping.default || Object;
// invariant(isPropSchema(propSchema), "expected prop schema as second argument");
// invariant(!isAliasedPropSchema(propSchema), "provided prop is aliased, please put aliases first");
if ( !propSchema && isModelSchema(mapping.default) ) {
propSchema = mapping.default;
}
return {
serializer : map(propSchema).serializer,
deserializer: function (jsonObject, done, context, oldValue) {
var keys = Object.keys(jsonObject);
map(propSchema).deserializer(jsonObject, function (err, newValue) {
if ( err ) {
return void done(err)
}
var isMap = isMapLike(newValue);
for (var i = 0, l = keys.length; i < l; i++) {
var clazz = mapping[keys[i]] || mapping.default;
// invariant(typeof clazz === "function", "expected constructor function");
var key = keys[i];
var raw_value = isMap ? newValue.get(key) : newValue[key];
var obj;
if(Array.isArray(raw_value)){
for(var ii = 0, ll = raw_value.length; ii < ll; ii++){
raw_value[ii] = isConstructor(clazz) ? new clazz(raw_value[ii]) :clazz(raw_value[ii]);
}
obj = raw_value;
}else{
obj = isConstructor(clazz) ? new clazz(raw_value) : clazz(raw_value);
}
if ( isMap ) {
newValue.set(key, obj);
}
else {
newValue[key] = obj;
}
}
return void done(null, newValue)
},
context,
oldValue
);
}
}
}
/** Expected
FacetContainer {
facets:
{ tags: [ TagFacet { name: 'hello_world', text: 'Hello World', count: 'world' } ],
other: [ { name: 'hello_world', text: 'Hello World', count: 'world' } ] } }
*/
/*
Licence: MIT
Copyright (c) 2016 - Kay Sackey
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Relies on code from Serialzr.
MIT Copyright (c) 2016 Michel Weststrate
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment