0% found this document useful (0 votes)
6 views2 pages

Flattening Arrays and Objects in JS

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Flattening Arrays and Objects in JS

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Flattened Array function flatten(value) {

if (typeof(value) !== 'object' || value == null){


return value;
}
if([Link](value)){
return flattenArray(value);
}
return flattenObj(value);
}
function flattenArray(array) {
return [Link]((acc, curr) => [Link](flatten(curr)),[]);
}
function flattenObj(object){
const flattenedObj = {}
for (const [key, value] of [Link](object)){
const valueIsObj = typeof value == 'object' && value !== null && ![Link](value);
const flattenedValue = flatten(value);
if(valueIsObj){
[Link](flattenedObj, flattenedValue);
}
else{
flattenedObj[key] = flattenedValue;
}
}
return flattenedObj;
}
[Link] = flatten;

Testing Framework function describe(testSuiteName, func) {


[Link](`beginning test suite ${testSuiteName}`);

try {
func();
[Link](`successfully completed test suite ${testSuiteName}`);
} catch (error) {
const { testCaseName, errorMessage } = error;
[Link](
`failed running test suite ${testSuiteName} on ` +
`test case ${testCaseName} with error message ${errorMessage}`
);
}
}

function it(testCaseName, func) {


[Link](`beginning test case ${testCaseName}`);

try {
func();
[Link](`successfully completed test case ${testCaseName}`);
} catch (errorMessage) {
throw { testCaseName, errorMessage };
}
}

function expect(actual) {
return new ExpectFunctions(actual);
}

class ExpectFunctions {
constructor(actual) {
[Link] = actual;
[Link] = [Link](actual);
}

toExist() {
if ([Link] == null) {
throw `expected value to exist but got ${[Link]}`;
}
}

toBe(expected) {
if ([Link] !== expected) {
throw `expected ${[Link]} to be ${[Link](
expected
)}`;
}
}

toBeType(type) {
if (typeof [Link] !== type) {
throw `expected ${
[Link]
} to be of type ${type} but got ${typeof [Link]}`;
}
}
}

// Do not edit the lines below.


[Link] = describe;
[Link] = it;
[Link] = expect;
Array Methods [Link] = function (callback) {
const output = [];
for(let i=0; i < [Link]; i++){
[Link](callback(this[i], i, this));
}
return output;
};

[Link] = function (callback) {


const output = [];
for(let i=0;i<[Link];i++){
if(callback(this[i], i, this) === true){
[Link](this[i])
}
}
return output;
};
[Link] = function (callback, initialValue) {
let accumulator = initialValue;
for(let i=0;i<[Link];i++){
if(i === 0 && initialValue === undefined){
accumulator = this[i];
}
else{
accumulator = callback(accumulator, this[i], i, this);
}
}
return accumulator;
};

Event Target class EventTarget {


constructor(){
[Link] = {}
}
addEventListener(name, callback) {
if(![Link](name)){
[Link][name] = new Set();
}
[Link][name].add(callback);
}
removeEventListener(name, callback) {
[Link][name]?.delete(callback);
}
dispatchEvent(name) {
[Link][name]?.forEach(callback => {
callback();
})
}
}
[Link] = EventTarget;

function debounce(callback, delay, immediate = false) {


let timerId;
return function(...args){
clearTimeout(timerId);
let shouldCallImmediately = timerId == null && immediate;
if(shouldCallImmediately){
[Link](this, args)
}
timerId = setTimeout(() => {
if(!immediate){
[Link](this, args);
}
timerId = null;
}, delay)
}
}
[Link] = debounce;
Debounce

You might also like