@ntragas/pouncejstest
Version:
A collection of UI components from Panther labs
179 lines (138 loc) • 4.64 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.addOpacity = addOpacity;
exports.lightenDarkenColor = lightenDarkenColor;
exports.slugify = slugify;
exports.getDates = exports.now = exports.dateToDayjs = exports.isEmptyValue = exports.noop = exports.isBrowser = exports.typedMemo = exports.__DEV__ = void 0;
var _react = _interopRequireDefault(require("react"));
var _dayjs = _interopRequireDefault(require("dayjs"));
var _utc = _interopRequireDefault(require("dayjs/plugin/utc"));
_dayjs.default.extend(_utc.default);
/** A boolean denoting whether we are in a development environment */
var __DEV__ = process.env.NODE_ENV !== 'production';
/**
* A function that implements the typical React.memo, but properly forwards TS generics
*/
exports.__DEV__ = __DEV__;
var typedMemo = _react.default.memo;
/**
*
* @param color A theme color
* @param opacity a value between [0,1]
* @returns A new color with opacity added to it
*/
exports.typedMemo = typedMemo;
function addOpacity(color, opacity) {
var hexWithoutHash = color.replace('#', '');
var r = parseInt(hexWithoutHash.substring(0, 2), 16);
var g = parseInt(hexWithoutHash.substring(2, 4), 16);
var b = parseInt(hexWithoutHash.substring(4, 6), 16);
return "rgba(" + r + "," + g + "," + b + "," + opacity + ")";
}
/**
* A function that takes a color and a positive or negative [0,1] amount and lightens it or darkens
* it by this amount. Positive numbers lighten the color, while negatives darken i
*
* @param color A color
* @param percentage A percentage by which to lighten/darken
* @returns A new color with opacity added to it
*/
function lightenDarkenColor(color, percentage) {
var getHue = function getHue(color) {
return parseInt(color, 16);
};
var restrictHue = function restrictHue(hue) {
return Math.max(Math.min(hue, 255), 0);
};
return '#' + color.replace(/^#/, '').replace(/../g, function (color) {
return ('0' + restrictHue(getHue(color) + percentage).toString(16)).substr(-2);
});
}
/**
* A function that takes a text and returns a valid slug for it. Useful for filename and url
* creation
*
* @param {String} text A string to slugify
* @returns {String} A slugified string
*/
function slugify(text) {
return text.toString().toLowerCase().replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w-]+/g, '') // Remove all non-word chars
.replace(/--+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
/**
* @returns True if current environment is a browser, false in any other case
*/
var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
exports.isBrowser = isBrowser;
var noop = function noop() {};
exports.noop = noop;
var isEmptyValue = function isEmptyValue(value) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string' && value === '') {
return true;
}
if (Array.isArray(value) && value.length === 0) {
return true;
}
if (typeof value === 'object' && Object.keys(value).length === 0) {
return true;
}
return false;
};
/**
* Converts the provided date to a Dayjs object
*/
exports.isEmptyValue = isEmptyValue;
var dateToDayjs = function dateToDayjs(value, timezone) {
if (timezone === void 0) {
timezone = 'local';
}
if (!value) {
return undefined;
}
return timezone === 'local' ? (0, _dayjs.default)(value) : (0, _dayjs.default)(value).utc();
};
/**
* Get the current date time.
* @returns {Dayjs} a Dayjs object
*/
exports.dateToDayjs = dateToDayjs;
var now = function now(timezone) {
if (timezone === void 0) {
timezone = 'local';
}
return timezone === 'local' ? (0, _dayjs.default)() : (0, _dayjs.default)().utc();
};
/**
* A function that generates a preset with dynamic dates.
* @returns {Object} An object with Dayjs presets
*/
exports.now = now;
var getDates = function getDates(timezone) {
if (timezone === void 0) {
timezone = 'local';
}
var dateNow = now(timezone);
var lastDay = dateNow.subtract(1, 'day');
var lastWeek = dateNow.subtract(1, 'week');
var lastMonth = dateNow.subtract(1, 'month');
var nextMonth = dateNow.subtract(1, 'month');
var lastThreeMonths = dateNow.subtract(3, 'month');
var lastSixMonths = dateNow.subtract(6, 'month');
return {
now: dateNow,
nextMonth,
lastDay,
lastWeek,
lastMonth,
lastThreeMonths,
lastSixMonths
};
};
exports.getDates = getDates;