es-number-ext
Version:
Native number extensions for javascript and typescript.
28 lines (27 loc) • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = extendProperty;
function extendProperty(func, option) {
var _a, _b, _c;
const functionName = (_a = option === null || option === void 0 ? void 0 : option.functionName) !== null && _a !== void 0 ? _a : func.name;
if (Number.hasOwnProperty(functionName)) {
const errorIfCollided = (_b = option === null || option === void 0 ? void 0 : option.errorIfCollided) !== null && _b !== void 0 ? _b : true;
const warnIfCollided = (_c = option === null || option === void 0 ? void 0 : option.warnIfCollided) !== null && _c !== void 0 ? _c : true;
const defaultErrorMessage = `Number.${functionName} has been already defined. No overriding and ignore it. Please check if the behaviour is the equivalent.`;
// Skip defining the property
if (errorIfCollided) {
console.error(errorIfCollided === true ? defaultErrorMessage : errorIfCollided);
}
else if (warnIfCollided) {
console.warn(warnIfCollided === true ? defaultErrorMessage : warnIfCollided);
}
}
else {
Object.defineProperty(Number, functionName, {
value: func,
writable: false,
configurable: false,
enumerable: false
});
}
}