Last active
October 15, 2018 15:59
-
-
Save MeyCry/3f3b773920651140fd88b2798d7205c6 to your computer and use it in GitHub Desktop.
ObjectArray =))
This file contains hidden or 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
class ObjectArray extends Object { | |
/** | |
* @param {Object} obj | |
*/ | |
constructor(obj = {}) { | |
super(); | |
this.length = 0; | |
const keys = Object.keys(obj); | |
keys.forEach((key) => { | |
this.length += 1; | |
this[key] = obj[key]; | |
}); | |
} | |
/** | |
* Send to cb each key and value in this object | |
* @param {function(string, value): void} cb - callback function | |
* @example objArr.forEach((key, value) => log(key + value)) | |
* @return {void} | |
*/ | |
forEach(cb) { | |
const keys = Object.keys(this).filter((itemKey) => itemKey !== "length"); | |
keys.forEach((itemKey) => { | |
cb(itemKey, this[itemKey]); | |
}); | |
} | |
/** | |
* Callback must return object! Method return new ObjectArray with data what will be return in cb | |
* @param {function(string, value): Object} cb - callback function | |
* @return {ObjectArray} - new instance of ObjectArray | |
*/ | |
map(cb) { | |
const keys = Object.keys(this).filter((itemKey) => itemKey !== "length"); | |
const result = new ObjectArray(); | |
keys.forEach((itemKey) => { | |
result.push(cb(itemKey, this[itemKey])); | |
}); | |
return result; | |
} | |
/** | |
* | |
* @param {function(string, value): boolean} cb must return bool | |
* @return {ObjectArray} | |
*/ | |
filter(cb) { | |
const keys = Object.keys(this).filter((itemKey) => itemKey !== "length"); | |
const result = new ObjectArray(); | |
keys.forEach((itemKey) => { | |
if (cb(itemKey, this[itemKey])) { | |
let _tempObj = {}; | |
_tempObj[itemKey] = this[itemKey]; | |
result.push(_tempObj); | |
} | |
}); | |
return result; | |
} | |
/** | |
* Mutation. added in to ObjectArray keys with values. | |
* @param {Object} obj | |
* @return {ObjectArray} - current instance of ObjectArray | |
* @example objArr | |
* .push({myKey: myVal, secondKey: secVal}) // => ObjectArray{myKey: myVal, secondKey: secVal, length: 2} | |
*/ | |
push(obj) { | |
if (!obj && typeof obj !== "object") { | |
return this; | |
} | |
const keys = Object.keys(obj); | |
keys.forEach((key) => { | |
this.length += 1; | |
this[key] = obj[key]; | |
}); | |
return this; | |
} | |
/** | |
* Delete by keys or key fields and return object with deleted fields. | |
* @param {Array|String} keys can be | |
* @return {Object} - object of deleted keys with value. | |
* @example objArr.delete("myKey") // => {myKey: myValue} | |
* @example objArr.delete(["myKey", "myKoy"]) // => {myKey: myValue, myKoy: mySecondVal} | |
*/ | |
delete(keys) { | |
var _keys = keys; | |
const deleted = {}; | |
if (!Array.isArray(keys)) { | |
_keys = [keys]; | |
} | |
_keys.forEach((itemKey) => { | |
if (this.hasOwnProperty(itemKey) && itemKey !== "length") { | |
this.length -= 1; | |
deleted[itemKey] = this[itemKey]; | |
delete this[itemKey]; | |
} | |
}); | |
return deleted; | |
} | |
/** | |
* | |
* @param {...ObjectArray} args | |
* @return {ObjectArray} new instance | |
*/ | |
concat(...args) { | |
const result = new ObjectArray(); | |
this.forEach((key, val) => { | |
const _tempObj = {}; | |
_tempObj[key] = val; | |
result.push(_tempObj); | |
}); | |
args.forEach((itemObjArr) => { | |
itemObjArr.forEach((key, val) => { | |
const _tempObj = {}; | |
_tempObj[key] = val; | |
result.push(_tempObj); | |
}); | |
}); | |
return result; | |
} | |
} | |
ObjectArray.prototype[Symbol.iterator] = function() { | |
const keys = Object.keys(this).filter((itemKey) => itemKey !== "length"); | |
const self = this; | |
var current = 0; | |
var last = keys.length; | |
return { | |
next() { | |
if (current < last) { | |
return { | |
done: false, | |
value: { | |
key: keys[current], | |
val: self[keys[current++]] | |
} | |
}; | |
} | |
return { | |
done: true | |
}; | |
} | |
}; | |
}; | |
var test = new ObjectArray({a: 1, b: 2, c: 3}); | |
console.log(test); // ObjectArray {length: 3, a: 1, b: 2, c: 3 } | |
test.delete("a"); | |
console.log(test); // ObjectArray { length: 2, b: 2, c: 3 } | |
test.delete(["b", "c", "z", "length"]); | |
console.log(test); // ObjectArray { length: 0 } | |
var test1 = new ObjectArray({a: 1}); | |
var test2 = new ObjectArray({z: 4, x: 5, v: 6}); | |
test1.push({b: 2, c: 3}); | |
console.log(test1); // ObjectArray { length: 3, a: 1, b: 2, c: 3 } | |
var test3 = test1.concat(test2); | |
console.log(test3); // ObjectArray { length: 6, a: 1, b: 2, c: 3, z: 4, x: 5, v: 6 } | |
test3.forEach(function (key, val) { | |
console.log(key, val); | |
/* | |
a 1 | |
b 2 | |
c 3 | |
z 4 | |
x 5 | |
v 6 | |
*/ | |
}); | |
var test4 = test3.map(function (key, val) { | |
const _tmpObj = {}; | |
_tmpObj[key + " added"] = val + 1; | |
return _tmpObj | |
}); | |
test4.forEach(function (key, val) { | |
console.log(key, val); | |
/* | |
a added 2 | |
b added 3 | |
c added 4 | |
z added 5 | |
x added 6 | |
v added 7 | |
*/ | |
}); | |
var test5 = test4.filter(function (key, val) { | |
return val % 2 === 0; | |
}); | |
console.log(test5); // ObjectArray { length: 3, 'a added': 2, 'c added': 4, 'x added': 6 } | |
for (let {key, val} of test5) { | |
console.log("super key", key, "super val", val); | |
/* | |
super key a added super val 2 | |
super key c added super val 4 | |
super key x added super val 6 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment