Skip to content

Instantly share code, notes, and snippets.

@gbozee
Last active February 23, 2018 08:03
Show Gist options
  • Save gbozee/3c3943bc99014ef755024add8f5ce912 to your computer and use it in GitHub Desktop.
Save gbozee/3c3943bc99014ef755024add8f5ce912 to your computer and use it in GitHub Desktop.
test("When there are two different keys and one needs updating", () => {
const page = [
{
Awards: {
awards: {
heading: "Awards & Certifications",
body: [
{
title: "Shell Ecomarathon Award",
company: "University of Lagos",
date: "2014",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 0
},
{
title: "MTN Foundation Scholarship",
company: "MTN Nigeria",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 1
},
{
title: "Inbound Marketing Certification",
company: "Hubspot",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 2
},
]
}
},
IndustrySkills: {
industry_skills: {
heading: "Special Skills",
body: [
{
title: "UX Design",
description:
"Creating User Interfaces for applications in a way that meets the users needs and business goals",
index: 0
},
]
}
}
},
{
IndustrySkills: {
industry_skills: {
body: [
{
title: "UI Design",
description:
"Creating visually compelling websites and apps that feels coherent with the brand's visual identity",
index: 1
}
]
}
}
}
];
expect(updatePages(page,
[
0,
"Awards",
{
awards: {
heading: "Awards & Certifications",
body: [
{
title: "Shell Ecomarathon Award",
company: "University of Lagos",
date: "2014",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 0
},
{
title: "MTN Foundation Scholarship",
company: "MTN Nigeria",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 1
}
]
}
},
{
awards: {
body: [
{
title: "Inbound Marketing Certification",
company: "Hubspot",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 2
}
]
}
},
false
])).toEqual([
{
Awards: {
awards: {
heading: "Awards & Certifications",
body: [
{
title: "Shell Ecomarathon Award",
company: "University of Lagos",
date: "2014",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 0
},
{
title: "MTN Foundation Scholarship",
company: "MTN Nigeria",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 1
}
]
}
},
IndustrySkills: {
industry_skills: {
heading: "Special Skills",
body: [
{
title: "UX Design",
description:
"Creating User Interfaces for applications in a way that meets the users needs and business goals",
index: 0
},
]
}
}
},
{
Awards: {
awards:{
body: [
{
title: "Inbound Marketing Certification",
company: "Hubspot",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 2
}
]
}
},
IndustrySkills: {
industry_skills: {
body: [
{
title: "UI Design",
description:
"Creating visually compelling websites and apps that feels coherent with the brand's visual identity",
index: 1
}
]
}
}
}
]);
});
// The original array I am working with
const pages = [
{
Awards: {
awards: {
heading: "Awards & Certifications",
body: [
{
title: "Shell Ecomarathon Award",
company: "University of Lagos",
date: "2014",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 0
},
{
title: "MTN Foundation Scholarship",
company: "MTN Nigeria",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 1
},
{
title: "Inbound Marketing Certification",
company: "Hubspot",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 2
}
]
}
}
}
];
/* The function to be implemented should use a particular configuration which is also an array but consist of the following items
1. The index of the item to remove data from
2. The key of the object to remove e.g "Awards"
3. The object structure to replace the index passed with
4. The object structure which should be added to the next item in the array.
*/
// A sample usage would look like this
let newState = updatePages(pages, [
0,
"Awards",
{
awards: {
heading: "Awards & Certifications",
body: [
{
title: "Shell Ecomarathon Award",
company: "University of Lagos",
date: "2014",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 0
},
{
title: "MTN Foundation Scholarship",
company: "MTN Nigeria",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 1
}
]
}
},
{
awards: {
body: [
{
title: "Inbound Marketing Certification",
company: "Hubspot",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 2
}
]
}
}
])
// The expected Result was to be of this form
expect(newState).toEqual([
{
Awards: {
awards: {
heading: "Awards & Certifications",
body: [
{
title: "Shell Ecomarathon Award",
company: "University of Lagos",
date: "2014",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 0
},
{
title: "MTN Foundation Scholarship",
company: "MTN Nigeria",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 1
}
]
}
}
},
{
Awards: {
awards: {
body: [
{
title: "Inbound Marketing Certification",
company: "Hubspot",
date: "2017",
description:
"Awarded the Best Creative Arts Students from a class of 54, 2017",
image_url: "http://place_hold.it/120x60",
index: 2
}
]
}
}
}
])
// This function takes two objects and does a deep merge of the object.
/*
It accounts for the following scenarios
1. When the value of any of the keys is a string, the value of the key for the first object is used as the default.
2. When the value of any of the keys is an array, the values from both object keys are merged
*/
let merger = (a, b) => {
let c = {};
Object.keys(a).forEach(u => {
if (typeof a[u] === "string") {
c[u] = a[u];
}
if (Array.isArray(a[u])) {
let accountForUndefined = !!b ? b : { [u]: [] };
c[u] = [...a[u], ...accountForUndefined[u]];
}
if (!Array.isArray(a[u]) && typeof a[u] === "object") {
let result = merger(a[u], b[u]);
c[u] = result;
}
});
return c;
};
// This function kinda does the reverse of merger. It returns just the items in `a` that do not exist in `b`
const excluder = (a, b, keys) => {
let ouv = uu => (!!keys ? uu[keys] : uu);
let c = {};
Object.keys(a).forEach(u => {
if (typeof a[u] === "string") {
let ro = a[u] === b[u];
if (!!ro === false) {
c[u] = a[u];
}
}
if (Array.isArray(a[u])) {
let mm = [];
let indexs = a[u].map(x => ouv(x));
console.log(indexs);
let vvv = new Set([...(b[u] || []).map(x => ouv(x))]);
console.log(vvv);
let ddd = [...new Set([...indexs].filter(x => !vvv.has(x)))];
c[u] = ddd.map(x => {
let inst = a[u].find(u => ouv(u) === x);
return inst;
});
}
if (!Array.isArray(a[u]) && typeof a[u] === "object") {
let result = excluder(a[u], b[u], keys);
c[u] = result;
}
});
return c;
};
// The actual logic for the function to be implemented.
export function updatePages(pages, params) {
let newPages = [...pages];
let found = pages[params[0]];
let existing = excluder(found[params[1]], params[2], "index");
found = { ...found, [params[1]]: params[2] };
newPages[params[0]] = found;
let newArray = [];
if (pages.length - 1 > params[0]) {
let working = { ...pages[params[0] + 1][params[1]] };
working = merger(existing, working);
Object.keys(params[3]).forEach(o => {
if (Array.isArray(working[o])) {
working[o] = [...params[3], ...working[o]];
}
});
newPages[params[0] + 1][params[1]] = working;
}
if (pages.length - 1 === params[0]) {
newPages.push({ [params[1]]: params[3] });
}
return newPages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment