Last active
November 10, 2017 02:14
-
-
Save mk-pmb/4d3ecf5182ad83b9baa2d56e9debd5ae to your computer and use it in GitHub Desktop.
movie sort thingie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ { "title": "Test 1", | |
"type": [ | |
{ "name": "Animal", "level": 1 }, | |
{ "name": "Food", "level": 1 }, | |
{ "name": "Chicken", "level": 3 } ] | |
}, | |
{ "title": "Test 2", | |
"type": [ { "name": "Foo", "level": 2 } ] | |
}, | |
{ "title": "Test 3", | |
"type": [ { "name": "Baz", "level": 2 } ] | |
}, | |
{ "title": "Test 4", | |
"type": [ | |
{ "name": "Animal", "level": 1 }, | |
{ "name": "Dog", "level": 2 } ] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ { "name": "Animal", | |
"typechild": [ | |
{ "name": "Chicken", "level": 3 }, | |
{ "name": "Dog", "level": 2 } ] | |
}, | |
{ "name": "Food", | |
"typechild": [ { "name": "Chicken", "level": 3 } ] | |
}, | |
{ "name": "No name", | |
"typechild": [ | |
{ "name": "Foo", "level": 2 }, | |
{ "name": "Baz", "level": 2 } ] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint indent: 2, maxlen: 80, node: true */ | |
/* -*- tab-width: 2 -*- */ | |
'use strict'; | |
var input = require('./movies.in.json'), want = require('./movies.out.json'), | |
equal = require('equal-pmb'), jsonify = require('ersatz-compactjson'), | |
ld = require('lodash'); | |
function unflattenTypes(orig) { | |
var subTypesByTopType = {}; | |
function learnTypes(movie) { | |
if (!movie.type) { return; } | |
var allTypes = movie.type, topTypes = [], subTypes = []; | |
function splitTypeLevels(typeSpec) { | |
var destList = (typeSpec.level > 1 ? subTypes : topTypes); | |
destList.push(typeSpec); | |
} | |
allTypes.forEach(splitTypeLevels); | |
if (!topTypes.length) { topTypes[0] = { name: 'No name' }; } | |
//console.log({ movieTitle: movie.title, topTypes: topTypes }); | |
topTypes.forEach(function (topType) { | |
var ttn = topType.name, oldSubs = subTypesByTopType[ttn]; | |
subTypesByTopType[ttn] = (oldSubs || []).concat(subTypes); | |
}); | |
} | |
orig.forEach(learnTypes); | |
//console.log('top->sub:', subTypesByTopType); | |
return subTypesByTopType; | |
} | |
function addTypechildClutter(subTypesByTopType) { | |
function remapKeys(subTypes, topTypeName) { | |
return { name: topTypeName, typechild: subTypes }; | |
} | |
return ld.map(subTypesByTopType, remapKeys); | |
} | |
(function () { | |
var top2sub = unflattenTypes(input), | |
output = addTypechildClutter(top2sub); | |
equal.lines(jsonify(output), jsonify(want)); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment