craydent-date
Version:
Node module to extend fs/file system functionality and simplify code for maintainability and readability
27 lines (26 loc) • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = __defineFunction;
const _getFuncArgs_1 = require("../protected/_getFuncArgs");
const craydent_error_1 = require("craydent.error");
const __common_1 = require("../private/__common");
function __defineFunction(name, func /* , override?: boolean */) {
try {
const args = (0, _getFuncArgs_1.default)(func), fstr = func.toString().replace(/this/g, 'craydent_ctx'),
// extra code to account for when this == global
extra_code = "if(arguments.length == 0 && this == $c){return;}", fnew = args.length === 0 ?
fstr.toString().replace(/(\(\s*?\)\s*?\{)/, ` (craydent_ctx){${extra_code}`) :
`(${fstr.toString().replace(/\((.*?)\)\s*?\{/, '(craydent_ctx,$1){' + extra_code)})`;
// if (!override && scope.eval(`typeof(${name})`) !== "undefined") {
// return scope.eval(`$c.${name} = ${fnew}`);
// }
if (global.$c && !global.$c.hasOwnProperty(name)) {
return __common_1.scope.eval(`global.$c.${name} = $c.${name} = ${fnew}`);
}
return __common_1.scope.eval(`$c.${name} = ${fnew}`);
}
catch (e) /* istanbul ignore next */ {
craydent_error_1.default && (0, craydent_error_1.default)("__defineFunction", e);
return null;
}
}