Skip to content

Commit

Permalink
Make custom object properties non-enumerable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Demonstrandum committed Jul 28, 2023
1 parent e68590c commit 0f8e216
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 59 deletions.
169 changes: 111 additions & 58 deletions lib/BasicCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ let already_patched = (typeof window !== 'undefined'
export const clone = obj => Object.assign(Object.create(Object.getPrototypeOf(obj)), obj);

if (!already_patched) {
Object.prototype.clone = function () {
return clone(this);
};
Object.defineProperty(Object.prototype, 'clone',{
value: function(){ return clone(this) },
writable: true,
configurable: true,
enumerable: false
});
}

export const type = element => (
Expand Down Expand Up @@ -72,115 +75,165 @@ if (!already_patched) {
Math.HALF_PI = Math.PI * 0.5;
Math.triangle = t => Math.abs(((t - 1) % 4) - 2) - 1;

Number.prototype.roundTo = function (dp) {
return parseFloat((this).toFixed(dp));
};
Number.prototype.times = function (fn) {
for (let i = 1; i <= this.valueOf(); ++i)
fn(i);
};
Object.defineProperty(Number.prototype, 'roundTo', {
value: function (dp) {
return parseFloat((this).toFixed(dp));
},
enumerable: false,
});
Object.defineProperty(Number.prototype, 'times', {
value: function (fn) {
for (let i = 1; i <= this.valueOf(); ++i) fn(i);
},
enumerable: false,
});

Array.prototype.each = Array.prototype.forEach;
Array.prototype.select = Array.prototype.filter;
Array.prototype.reject = function (lambda, array) {
return this.filter((e) => !lambda(e), array)
};
Array.prototype.mag = function () {
return Math.sqrt(this.reduce((i, j) => i + j ** 2, 0));
};
Array.prototype.normalize = function () {
if (this.every(e => e === 0)) {
return this;
}
return this.map(e => e / this.mag());
};
Array.prototype.rotate = function (theta, origin=[0,0]) {
return [ // Only 2D
origin[0] + (this[0] - origin[0]) * Math.cos(theta) - (this[1] - origin[1]) * Math.sin(theta),
origin[1] + (this[0] - origin[0]) * Math.sin(theta) + (this[1] - origin[1]) * Math.cos(theta)
];
};
Object.defineProperty(Array.prototype, 'each', {
value: Array.prototype.forEach,
enumerable: false,
});
Object.defineProperty(Array.prototype, 'select', {
value: Array.prototype.filter,
enumerable: false,
});
Object.defineProperty(Array.prototype, 'reject', {
value: function (lambda, array) {
return this.filter((e) => !lambda(e), array);
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'mag', {
value: function () {
return Math.sqrt(this.reduce((i, j) => i + j ** 2, 0));
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'normalize', {
value: function () {
if (this.every(e => e === 0)) {
return this;
}
return this.map(e => e / this.mag());
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'rotate', {
value: function (theta, origin=[0,0]) {
return [ // Only 2D
origin[0] + (this[0] - origin[0]) * Math.cos(theta) - (this[1] - origin[1]) * Math.sin(theta),
origin[1] + (this[0] - origin[0]) * Math.sin(theta) + (this[1] - origin[1]) * Math.cos(theta)
];
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'x', {
get: function x() {
return this[0];
},
set: function x(new_x) {
return this[0] = new_x;
}
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'y', {
get: function y() {
return this[1];
},
set: function y(new_y) {
return this[1] = new_y;
}
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'z', {
get: function y() {
return this[2];
},
set: function z(new_z) {
return this[2] = new_z;
}
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'first', {
get: function first() {
return this[0];
},
set: function first(other) {
return this[0] = other;
}
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'last', {
get: function last() {
return this[this.length - 1];
},
set: function last(other) {
return this[this.length - 1] = other;
}
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'tail', {
get: function tail() {
return this.slice(1);
}
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'point', {
get: function point() {
return Point(this[0], this[1]);
}
},
enumerable: false,
});
Object.defineProperty(Array.prototype, 'head', {
get: function first() {
return this[0];
},
set: function first(other) {
return this[0] = other;
},
enumerable: false,
});
Array.prototype.head = Array.prototype.first;

String.prototype.replaceAll = function (search, replacement) {
return this.replace(new RegExp(search, 'g'), replacement);
};
Object.defineProperty(String.prototype, 'replaceAll', {
value: function (search, replacement) {
return this.replace(new RegExp(search, 'g'), replacement);
},
enumerable: false,
});

HTMLElement.prototype.html = function (s, ...exps) {
const contain = document.createElement('del');
contain.style.textDecoration = 'none';
contain.innerHTML = String.raw(s, ...exps);
this.appendChild(contain);
};
Object.defineProperty(HTMLElement.prototype, 'html', {
value: function (s, ...exps) {
const contain = document.createElement('del');
contain.style.textDecoration = 'none';
contain.innerHTML = String.raw(s, ...exps);
this.appendChild(contain);
},
enumerable: false,
});

HTMLElement.prototype.css = function (properties) {
for (const property in properties) {
if (Object.prototype.hasOwnProperty.call(properties, property)) {
this.style[property] = properties[property];
Object.defineProperty(HTMLElement.prototype, 'css', {
value: function (properties) {
for (const property in properties) {
if (Object.prototype.hasOwnProperty.call(properties, property)) {
this.style[property] = properties[property];
}
}
}
};
},
enumerable: false,
});

Object.prototype.omap = function (lambda) {
return Object.assign({},
...Object.keys(this).map(k =>
({[k]: lambda(this[k])})));
};
Object.defineProperty(Object.prototype, 'omap',{
value: function (lambda) {
return Object.assign({},
...Object.keys(this).map(k => ({[k]: lambda(this[k])})));
},
enumerable: false
});

Object.defineProperty(HTMLElement.prototype, 'elem', {
get: function elem() {
return this;
},
enumerable: false,
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "basiccanvas",
"title": "BasicCanvas",
"description": "Simple JavaScript canvas abstractions.",
"version": "1.3.3",
"version": "1.4.0",
"main": "lib/BasicCanvas.js",
"homepage": "https://github.com/Demonstrandum/BasicCanvas/",
"author": {
Expand Down

1 comment on commit 0f8e216

@vercel
Copy link

@vercel vercel bot commented on 0f8e216 Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

BasicCanvas – ./

basiccanvas-git-master-demonstrandum.vercel.app
basiccanvas-demonstrandum.vercel.app
canvas.knutsen.co

Please sign in to comment.