UNPKG

es-promise-ext

Version:

Native promise extensions for javascript and typescript.

53 lines (52 loc) 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extendProperty = extendProperty; exports.extendPrototype = extendPrototype; 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 (Promise.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 = `Promise.${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(Promise, functionName, { value: func, writable: false, configurable: false, enumerable: false }); } } function extendPrototype(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 (Promise.prototype.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 = `Promise.${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(Promise.prototype, functionName, { value: func, writable: false, configurable: false, enumerable: false }); } }