kickass-utilities
Version:
My Kick Ass Utilities
152 lines (129 loc) • 6.04 kB
JavaScript
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import * as R from 'ramda';
var getDisplayName = function getDisplayName(ComposedComponent) {
return ComposedComponent.displayName || ComposedComponent.name || 'Component';
};
var getIdsObject = R.pipe(R.map(function (_ref) {
var name = _ref.name,
value = _ref.value;
return _defineProperty({}, name, value);
}), R.mergeAll);
var getQueriesObject = R.pipe(R.map(function (_ref3) {
var name = _ref3.name,
value = _ref3.value;
return _defineProperty({}, name, value);
}), R.mergeAll);
var getQueriesString = R.pipe(R.filter(function (_ref5) {
var value = _ref5.value;
return value;
}), R.map(function (_ref6) {
var name = _ref6.name,
value = _ref6.value;
return "".concat(name, "=").concat(value);
}), R.join('&'), R.unless(R.isEmpty(), R.concat('?')));
var formatBytes = function formatBytes(bytes) {
var decimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
if (bytes <= 0) return '0 Bytes';
var k = 1024;
var sizes = ['Byte(s)', 'KB(s)', 'MB(s)', 'GB(s)', 'TB(s)', 'PB(s)', 'EB(s)', 'ZB(s)', 'YB(s)'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return "".concat(parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)), " ").concat(sizes[i]);
};
var round = function round(number, precision) {
var shift = function shift(num, exponent) {
var numArray = "".concat(num).split('e');
return +"".concat(numArray[0], "e").concat(numArray[1] ? +numArray[1] + exponent : exponent);
};
return shift(Math.round(shift(number, +precision)), -precision);
};
var memoize = function memoize(method) {
var _ref7 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref7$path = _ref7.path,
path = _ref7$path === void 0 ? [] : _ref7$path,
_ref7$timeout = _ref7.timeout,
timeout = _ref7$timeout === void 0 ? Infinity : _ref7$timeout;
var cache = {};
var fresh = {};
/* eslint-disable */
return function () {
// NOTE: arguments is an Array-like object
var renew = false;
var args = R.when( // ASSUME: Last element of arguments is a potential option object for memoize
function (argsList) {
return R.length(argsList) > 0 && R.both(R.is(Object), R.has('memoize'))(R.last(argsList));
}, function (argsList) {
var _R$last;
return (_R$last = R.last(argsList), renew = _R$last.memoize.renew, _R$last) && R.init(argsList);
})(Array.from(arguments));
var cachedArgs = JSON.stringify(R.path(path, args));
var invoke = renew || !cache[cachedArgs] || !fresh[cachedArgs]; // NOTE: We can cache Promise as well (including rejected Promise)!!!
cache[cachedArgs] = invoke ? method.apply(this, args) : cache[cachedArgs];
invoke && (fresh[cachedArgs] = true, timeout !== Infinity && setTimeout(function () {
return fresh[cachedArgs] = false;
}, timeout));
return cache[cachedArgs];
};
/* eslint-enable */
}; // NOTE: Order of comparison is important when their set of props are different
var shallowDiff = R.curry(function (a, b) {
var diffProps = '';
R.forEachObjIndexed(function (v, k) {
v !== b[k] && (diffProps += "".concat(k, ", ")); // eslint-disable-line
})(a);
return R.dropLast(2, diffProps);
});
var shallowEqual = function shallowEqual(objA, objB) {
var hasOwn = Object.prototype.hasOwnProperty;
var is = function is(x, y) {
return x === y ? x !== 0 || y !== 0 || 1 / x === 1 / y : x !== x && y !== y;
}; // eslint-disable-line
if (is(objA, objB)) return true;
if (_typeof(objA) !== 'object' || objA === null || _typeof(objB) !== 'object' || objB === null) {
return false;
}
var keysA = Object.keys(objA);
var keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false; // eslint-disable-next-line
for (var i = 0; i < keysA.length; i++) {
if (!hasOwn.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
};
var findByProp = R.curry(function (name, value, arrOrObj) {
return R.find(R.propEq(name, value), R.has('length', arrOrObj) ? arrOrObj : R.values(arrOrObj));
});
var findByPath = R.curry(function (path, value, arrOrObj) {
return R.find(R.pathEq(path, value), R.has('length', arrOrObj) ? arrOrObj : R.values(arrOrObj));
});
var findIndexBy = R.curry(function (name, value, arr) {
return R.findIndex(R.propEq(name, value), arr);
});
var findIndexByPath = R.curry(function (path, value, arr) {
return R.findIndex(R.pathEq(path, value), arr);
});
var switchProp = R.curry(function (toProp, fromProp, fromValue, arr) {
return R.pipe(R.find(R.propEq(fromProp, fromValue)), R.unless(R.isNil, R.prop(toProp)))(arr);
});
var switchPath = R.curry(function (toPath, fromPath, fromValue, arr) {
return R.pipe(R.find(R.pathEq(fromPath, fromValue)), R.unless(R.isNil, R.path(toPath)))(arr);
});
var toObjBy = R.curry(function (name, arr) {
return R.pipe(R.groupBy(R.prop(name)), R.map(R.prop(0)), R.omit(['undefined']))(arr);
});
var removeBy = R.curry(function (name, value, arr) {
return R.reject(R.propEq(name, value))(arr);
});
var removeByPath = R.curry(function (path, value, arr) {
return R.reject(R.pathEq(path, value))(arr);
});
var initialsOf = function initialsOf(name) {
var noLetters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
var delimiter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ' ';
var binder = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
return R.pipe(R.split(delimiter), R.map(R.head), R.join(binder), R.take(noLetters))(name);
};
export { getDisplayName, getIdsObject, getQueriesObject, getQueriesString, formatBytes, round, memoize, shallowEqual, shallowDiff, findByProp, findByPath, findIndexBy, findIndexByPath, switchProp, switchPath, toObjBy, removeBy, removeByPath, initialsOf };
export { default as geolocation } from './geolocation';