wishthis/node_modules/d/auto-bind.js
2022-01-21 09:28:41 +01:00

33 lines
1.2 KiB
JavaScript

"use strict";
var isValue = require("type/value/is")
, ensureValue = require("type/value/ensure")
, ensurePlainFunction = require("type/plain-function/ensure")
, copy = require("es5-ext/object/copy")
, normalizeOptions = require("es5-ext/object/normalize-options")
, map = require("es5-ext/object/map");
var bind = Function.prototype.bind
, defineProperty = Object.defineProperty
, hasOwnProperty = Object.prototype.hasOwnProperty
, define;
define = function (name, desc, options) {
var value = ensureValue(desc) && ensurePlainFunction(desc.value), dgs;
dgs = copy(desc);
delete dgs.writable;
delete dgs.value;
dgs.get = function () {
if (!options.overwriteDefinition && hasOwnProperty.call(this, name)) return value;
desc.value = bind.call(value, options.resolveContext ? options.resolveContext(this) : this);
defineProperty(this, name, desc);
return this[name];
};
return dgs;
};
module.exports = function (props/*, options*/) {
var options = normalizeOptions(arguments[1]);
if (isValue(options.resolveContext)) ensurePlainFunction(options.resolveContext);
return map(props, function (desc, name) { return define(name, desc, options); });
};