@ntragas/pouncejstest
Version:
A collection of UI components from Panther labs
142 lines (123 loc) • 3.91 kB
JavaScript
import React from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
/** A boolean denoting whether we are in a development environment */
export var __DEV__ = process.env.NODE_ENV !== 'production';
/**
* A function that implements the typical React.memo, but properly forwards TS generics
*/
export var typedMemo = React.memo;
/**
*
* @param color A theme color
* @param opacity a value between [0,1]
* @returns A new color with opacity added to it
*/
export 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
*/
export 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
*/
export 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
*/
export var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
export var noop = function noop() {};
export 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
*/
export var dateToDayjs = function dateToDayjs(value, timezone) {
if (timezone === void 0) {
timezone = 'local';
}
if (!value) {
return undefined;
}
return timezone === 'local' ? dayjs(value) : dayjs(value).utc();
};
/**
* Get the current date time.
* @returns {Dayjs} a Dayjs object
*/
export var now = function now(timezone) {
if (timezone === void 0) {
timezone = 'local';
}
return timezone === 'local' ? dayjs() : dayjs().utc();
};
/**
* A function that generates a preset with dynamic dates.
* @returns {Object} An object with Dayjs presets
*/
export 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
};
};