UNPKG

mobx-roof

Version:

Simple React data management by mobx.

139 lines (124 loc) 4.88 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.isRegExp = exports.isFunction = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; exports.autobind = autobind; exports.mapValues = mapValues; exports.toPromise = toPromise; exports.toObservableObj = toObservableObj; exports.addMobxContextToComponent = addMobxContextToComponent; exports.each = each; exports.compose = compose; exports.nameToUpperCase = nameToUpperCase; exports.inherits = inherits; exports.classCallCheck = classCallCheck; exports.possibleConstructorReturn = possibleConstructorReturn; var _mobx = require('mobx'); var _constants = require('./constants'); var _react = require('react'); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var toString = Object.prototype.toString; /** * @param {Object} target * @param {Array<String>} methods */ function autobind(target, methods) { var _this = this; methods.forEach(function (methodName) { if (!target[methodName]) { throw new Error('Undefined method "' + methodName + '"'); } target[methodName] = target[methodName].bind(_this); }); } /** * Applies a function to every key-value pair inside an object. * * @param {Object} obj - The source object. * @param {Function} fn - The mapper function that receives the value and the key. * @param {Object?} res - Result object * @returns {Object} A new object that contains the mapped values for the keys. */ function mapValues(obj, fn) { var res = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; return Object.keys(obj).reduce(function (result, key) { result[key] = fn(obj[key], key); return result; }, res); } /** * @param {*} val * @returns {Promise} */ function toPromise(val) { if (val && typeof val.then === 'function') { return val; } return Promise.resolve(val); } function toObservableObj() { var obj = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; return mapValues(obj, function (item) { return (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' ? (0, _mobx.asFlat)(item) : item; }); } /** * @param {React.Component} WrappedComponent */ function addMobxContextToComponent(WrappedComponent) { WrappedComponent.contextTypes = _extends(_defineProperty({}, _constants.CONTEXT_NAME, _react.PropTypes.object.isRequired), WrappedComponent.contextTypes); } function each() { var obj = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var fn = arguments[1]; Object.keys(obj).forEach(function (key) { fn(obj[key], key); }); } var isFunction = exports.isFunction = function isFunction(arg) { return toString.call(arg) === '[object Function]'; }; var isRegExp = exports.isRegExp = function isRegExp(arg) { return toString.call(arg) === '[object RegExp]'; }; function compose(arr, arg) { return arr.reduce(function (cur, fn) { return cur.then(function (res) { return fn(res); }); }, Promise.resolve(arg)); } function nameToUpperCase() { var name = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0]; return name[0].toUpperCase() + name.slice(1); } function inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + (typeof superClass === 'undefined' ? 'undefined' : _typeof(superClass))); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) { if (Object.setPrototypeOf) Object.setPrototypeOf(subClass, superClass);else subClass.__proto__ = superClass; } } function classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && ((typeof call === 'undefined' ? 'undefined' : _typeof(call)) === 'object' || typeof call === 'function') ? call : self; }