Skip to content

Instantly share code, notes, and snippets.

@caiodanielnunessantos
Last active December 10, 2020 16:17
Show Gist options
  • Save caiodanielnunessantos/38b7da8136405e13afa4c227590ec3fd to your computer and use it in GitHub Desktop.
Save caiodanielnunessantos/38b7da8136405e13afa4c227590ec3fd to your computer and use it in GitHub Desktop.
const [dummyArray, dummyObject, dummyString, dummyNumber] = [Array, Object, String, Number];
dummyArray.prototype.merge = function (source) {
source.forEach((element) => {
if (!this.includes(element)) this.push(element);
});
return this;
};
dummyObject.prototype.deepMerge = function (source) {
if (Array.isArray(this)) {
this.merge(source);
return this;
}
Object.entries(source).forEach(([key, value]) => {
if (typeof this[key] === 'object') {
this[key].deepMerge(value);
} else {
this[key] = value;
}
});
return this;
};
dummyObject.prototype.transformIf = function (condition, lambda) {
if (condition) {
return lambda(this);
}
return this;
};
dummyObject.prototype.entries = function () {
return Object.entries(this);
};
dummyObject.prototype.theseValues = function (...keys) {
return keys.map(key => this[key]);
};
dummyArray.prototype.fromEntries = function () {
return Object.fromEntries(this);
};
dummyArray.prototype.max = function (compareFn) {
return this.reduce((acc, val) =>
((acc && (compareFn(val, acc) < 0)) ? acc : val), undefined);
};
dummyArray.prototype.selfMerge = function () {
return this.reduce((acc, val) =>
acc.deepMerge(val)
, {});
};
dummyString.prototype.transformIf = function (condition, lambda) {
return ((condition) ? lambda(this) : this).valueOf();
};
dummyNumber.prototype.transformIf = function (condition, lambda) {
return ((condition) ? lambda(this) : this).valueOf();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment