Skip to content

Instantly share code, notes, and snippets.

@minachuong
Created August 18, 2020 06:36
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 minachuong/f5955a23b937dde1dd6d2644f9652608 to your computer and use it in GitHub Desktop.
Save minachuong/f5955a23b937dde1dd6d2644f9652608 to your computer and use it in GitHub Desktop.
// key-value data structure seems redundant in that the id is provided twice
// when a property is not present, the serialization seems to require
// that the property is present but with a 'None' value
let scientists = {
"e3d46de8c7194cb1a32275195c15dc07": {
"id": "e3d46de8c7194cb1a32275195c15dc07",
"name": "Niels Bohr",
"specialization": "Quantum Mechanics",
"known_for": ["Wave/Particle Duality", "Uncertainty"],
},
"7064c3f9c99743b2838bbd8eacafe0d6": {
"id": "7064c3f9c99743b2838bbd8eacafe0d6",
"name": "Max Planck",
"known_for": "Planck's constant",
},
"b19e575a0d3f4151a1391452d8a47a44": {
"id": "b19e575a0d3f4151a1391452d8a47a44",
"name": "Jane Goodall",
"specialization": "Apes",
},
"17d9d0908f454253b5337e8c1ef4b564": {
"id": "17d9d0908f454253b5337e8c1ef4b564",
"name": "Caroline Herschel",
"specialization": "Stars",
}
};
// Do whitespace and line breaks need to be preserved?
// Guessing they do because Python is whitespace-conscious
// I'll follow whitespace and linebreak patterns provided in the output from the Python methods
function getSerializedScientistInfoById(scientistId) {
const scientistList = Object.values(scientists);
const scientist = scientistList.find(scientist => scientist["id"] === scientistId);
return serializeScientistInfo(scientist);
}
function serializeScientistInfo(scientistInfo, isNested = false) {
const scientist = scientistInfo;
// to ensure each scientist has all properties
const requiredKeysStingValues = ['id', 'name', 'specialization', 'known_for'];
const fourSpaces = ' ';
const keyIndentation = isNested ? fourSpaces.repeat(2) : fourSpaces;
const bracketIndentation = isNested ? fourSpaces : '';
const linebreak = '\n';
let serialization = '';
requiredKeysStingValues.forEach((key) => {
if (!scientist[key]) {
scientist[key] = 'None';
}
if (key == 'known_for' && Array.isArray(scientist[key])) {
scientist[key] = escapeArrayValues(scientist[key]);
}
serialization = `${serialization}${keyIndentation}'${key}': '${scientist[key]}',${linebreak}`;
});
return `${bracketIndentation}{${linebreak}${serialization}${bracketIndentation}}`;
}
function escapeArrayValues(arrayValues) {
let serialization = '';
arrayValues.forEach((value) => {
if (serialization == '') {
serialization = `'${value}'`;
} else {
serialization = `${serialization}, '${value}'`;
}
});
return `[${serialization}]`;
}
// method for getting a serialization of a dump of scientist information returning
// an object with a property of "scientists" seems redundant
// because consumers of the object will always need to access the property
// to do anything meaningful with the object
// I've decided to return just a list of info objects for more declarative use
function getSerializedScientistInfos(scientists) {
const scientistList = Object.values(scientists);
const lineBreak = '\n';
let serialization = '';
scientistList.forEach((scientist) => {
serialization = `${serialization}${serializeScientistInfo(scientist, true)},${lineBreak}`;
});
return `[${lineBreak}${serialization}]`;
}
// need a bit more time for refactoring... -_-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment