@aricma/itemids
Version:
Be faster in creating and updating react state, with the ItemIds object.
60 lines (49 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports._with = _with;
exports._can = _can;
var ERRORS = _interopRequireWildcard(require("../errors"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* function factories
*/
// errors
/**
*
* @param object
* @param key
* @param value
* @return {*}
* @private
*/
function _with(object, key, value) {
const gotKey = !!key;
const valueIsFunction = typeof value === "function";
if (valueIsFunction) throw Error(ERRORS.utils._with.gotFunction);
if (gotKey) {
Object.defineProperty(object, key, {
enumerable: false,
configurable: false,
writable: false,
value
});
}
return object;
}
function _can(object, key, func) {
const gotKey = !!key;
const gotNoFunction = !!func && typeof func !== "function";
if (gotNoFunction) throw Error(ERRORS.utils._can.gotNoFunction);
if (gotKey) {
Object.defineProperty(object, key, {
enumerable: false,
configurable: false,
writable: false,
value: func
});
}
return object;
}