UNPKG

morpha

Version:

A Web-IDE or Desktop-IDE creator.

116 lines (92 loc) 3.36 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _promise = require('babel-runtime/core-js/promise'); var _promise2 = _interopRequireDefault(_promise); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); exports.mapValues = mapValues; exports.each = each; exports.isPlainObject = isPlainObject; exports.toCamelCase = toCamelCase; exports.toSeparatorName = toSeparatorName; exports.toPromise = toPromise; exports.getFunctionParamKeys = getFunctionParamKeys; exports.safeRequire = safeRequire; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var fnToString = function fnToString(fn) { return Function.prototype.toString.call(fn); }; var objStringValue = fnToString(Object); /** * 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 (0, _keys2.default)(obj).reduce(function (result, key) { result[key] = fn(obj[key], key); return result; }, res); } function each() { var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var fn = arguments[1]; (0, _keys2.default)(obj).forEach(function (key) { fn(obj[key], key); }); } /** * @param {any} obj The object to inspect. * @returns {boolean} True if the argument appears to be a plain object. */ function isPlainObject(obj) { if (!obj || (typeof obj === 'undefined' ? 'undefined' : (0, _typeof3.default)(obj)) !== 'object') { return false; } var proto = typeof obj.constructor === 'function' ? (0, _getPrototypeOf2.default)(obj) : Object.prototype; if (!proto) { return true; } var constructor = proto.constructor; return typeof constructor === 'function' && constructor instanceof constructor && fnToString(constructor) === objStringValue; } function toCamelCase(name) { return name.split('-').map(function (str, index) { if (index === 0) return str; return str[0].toUpperCase() + str.slice(1); }).join(''); } function toSeparatorName(name) { return name.replace(/[A-Z]/g, function (match, pos) { if (pos === 0) return match.toLowerCase(); return '-' + match.toLowerCase(); }); } function toPromise(fn) { return function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return new _promise2.default(function (res) { res(fn.apply(undefined, args)); }); }; } function getFunctionParamKeys(func) { return func.toString().match(/^function\s*[^\(]+\((.*)\)|^\((.*)\)\s*=>/)[1].trim().split(/\s*,\s*/).filter(function (key) { return !!key; }); } function safeRequire(moduleName) { return require(moduleName); }