UNPKG

@teamsnap/teamsnap-ui

Version:

a CSS component library for TeamSnap

166 lines (165 loc) 5.09 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getClassName = exports.stringifyArray = exports.parseNumericString = exports.parseAlphaString = exports.setUniqueId = exports.generateUniqueId = exports.capitalize = void 0; var uniqueId_1 = __importDefault(require("lodash/uniqueId")); var capitalize_1 = __importDefault(require("lodash/capitalize")); /** * @name capitalize * * @description * This simple method returns a capitalized version of the provided string. * Currently it is a 'wrapper' around the lodash _capitalize method. * There are other utilities we can look into as well, such as `shortid` * * @example * import { capitalize } from 'utils/helpers * * capitalize('name') * */ var capitalize = function (string) { return (0, capitalize_1.default)(string); }; exports.capitalize = capitalize; /** * @name generateUniqueId * * @description * This simple method returns a string representing a uniqueID. * Currently it is a 'wrapper' around the lodash uniqueId method. * There are other utilities we can look into as well, such as `shortid` * * @example * import { generateUniqueId } from 'utils/helpers * * generateUniqueId() * */ var generateUniqueId = function (prefix) { if (prefix === void 0) { prefix = ''; } return (0, uniqueId_1.default)(prefix); }; exports.generateUniqueId = generateUniqueId; /** * @name setUniqueId * * @description * Set a uniqueID on an object if no id is provided. * Accepts an array of objects, the property name to create * * @example * import { setUniqueId } from 'utils/helpers * * setUniqueId([{name: 'item one'}], 'uuid') * */ var setUniqueId = function (items, property) { if (property === void 0) { property = 'id'; } var updatedItems = null; // Assume if first row has an id, they all do and just return items if (items.length && items[0].hasOwnProperty(property)) { updatedItems = items; } else { updatedItems = items.map(function (item) { var _a; return (__assign((_a = {}, _a[property] = (0, exports.generateUniqueId)('item-'), _a), item)); }); } return updatedItems; }; exports.setUniqueId = setUniqueId; /** * @name parseAlphaString * * @description * Parse alpha characters from a provided string. * * @example * import { parseAlphaString } from 'utils/helpers * * parseAlphaString('abcd 1234 dcba') * */ var parseAlphaString = function (value) { return value.toString().replace(/[^a-zA-Z]/g, "").toLowerCase(); }; exports.parseAlphaString = parseAlphaString; /** * @name parseNumericString * * @description * Parse Numeric characters, periods and negation from a provided string. * * @example * import { parseNumericString } from 'utils/helpers * * parseNumericString('abcd 1234 dcba') * */ var parseNumericString = function (value) { return parseFloat(value.toString().replace(/[^0-9.-]/g, "")).toFixed(2); }; exports.parseNumericString = parseNumericString; /** * @name stringifyArray * * @description * Used to quickly filter out and join an array by 'truthy' values. * * @example * import { stringifyArray } from 'utils/helpers * * stringifyArray(['Simpson', 'Homer'], ', ') * */ var stringifyArray = function (array, joinBy) { if (joinBy === void 0) { joinBy = ' '; } return array.filter(Boolean).join(joinBy); }; exports.stringifyArray = stringifyArray; /** * @name getClassName * * @description * Takes a base className and calls stringifyArray to merge 'truthy' options into a single string it returns * * @example * import { getClassName } from 'utils/helpers * * getClassName( * 'BaseClassName', * isInline && 'InlineModifier', * isActive ? 'ActiveModifier' : 'InactiveModifier' * 'some-utility-modifier' * ) * */ var getClassName = function (className) { var classModifiers = []; for (var _i = 1; _i < arguments.length; _i++) { classModifiers[_i - 1] = arguments[_i]; } var classes = __spreadArray([ className ], classModifiers, true); return (0, exports.stringifyArray)(classes); }; exports.getClassName = getClassName;