Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions modules/3_objects/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
/**
* Array to List
*/
export const arrayToList = () => {};
export const listToArray = () => {};
export const arrayToList = (arr) => {
let parentNode = {};
let childNode = parentNode;
for (let i = 0; i < arr.length; i++) {
childNode.value = arr[i];
if (i+1 !== arr.length){
childNode.rest = {}
childNode = childNode.rest;
}else {
childNode.rest = null;
}

}
return parentNode;
};
export const listToArray = (arr) => {
return arr; // not clear what should be done here
};

/**
* Keys and values to list
*/
export const getKeyValuePairs = () => {};
export const getKeyValuePairs = (obj) => {
let result = [];
for (const key in obj) {
result.push([key, obj[key]]);
}
return result;
};

/**
* Invert keys and values
*/
export const invertKeyValue = () => {};
export const invertKeyValue = (obj) => {
let result = {};
for (const key in obj) {
result[obj[key]] = key;
}
return result;
};

/**
* Get all methods from object
*/
export const getAllMethodsFromObject = () => {};
export const getAllMethodsFromObject = (obj) => {
return Object.getOwnPropertyNames(obj).filter(function(property) {
return typeof obj[property] == 'function';
});
};

/**
* Clock
Expand All @@ -27,4 +59,29 @@ export class Clock {}
/**
* Groups
*/
export class Groups {}
export class Groups {
members = [];

has(val){
return this.members.includes(val);
}

add(val){
if(!this.has(val)){
this.members.push(val);
}
}

delete(val){
if(this.has(val)){
let idx = this.members.indexOf(val);
this.members.splice(idx, 1);
}
}

static from (arr) {
let instance = new Groups();
instance.members = arr;
return instance;
}
}