@react-md/utils
Version:
General utils for react-md.
68 lines • 2.25 kB
JavaScript
/**
* This is a small util that is used to transform a search string with common
* patterns.
*
* @param value - The string to transform
* @param lowercase - Boolean if the value should be lowercased for
* case-insensitive searches
* @param trim - Boolean if the leading and trailing whitespace should be
* trimmed
* @param ignoreWhitespace - Boolean if all whitespace should be ignored. This
* will take precedence over the `trim` parameter if set to true.
* @returns The transformed search string
* @internal
*/
export function getSearchString(value, lowercase, trim, ignoreWhitespace) {
if (lowercase === void 0) { lowercase = false; }
if (trim === void 0) { trim = false; }
if (ignoreWhitespace === void 0) { ignoreWhitespace = false; }
if (lowercase) {
value = value.toLowerCase();
}
if (ignoreWhitespace) {
value = value.replace(/\s/g, "");
}
else if (trim) {
value = value.trim();
}
return value;
}
/**
* The default implementation of the getItemValue search option that will
* attempt to "stringify" any unknown item as a string.
*
* @param item - The current item to transform
* @param valueKey - The key to use that should hold the value if the item is an
* object
* @returns the item as a string
* @internal
*/
export function getItemValue(item, valueKey) {
if (valueKey === void 0) { valueKey = "value"; }
switch (typeof item) {
case "string":
return item;
case "number":
return Number.isNaN(item) ? "" : "".concat(item);
case "function":
return getItemValue(item(), valueKey);
case "object":
return item
? getItemValue(item[valueKey], valueKey)
: "";
default:
return "";
}
}
export var DEFAULT_GET_ITEM_VALUE = getItemValue;
export var DEFAULT_VALUE_KEY = "value";
export var DEFAULT_TRIM = true;
export var DEFAULT_IGNORE_WHITESPACE = false;
export var DEFAULT_SEARCH_RESET_TIME = 500;
export var DEFAULT_SEARCH_OPTIONS = {
getItemValue: DEFAULT_GET_ITEM_VALUE,
valueKey: DEFAULT_VALUE_KEY,
trim: DEFAULT_TRIM,
ignoreWhitespace: DEFAULT_IGNORE_WHITESPACE,
};
//# sourceMappingURL=utils.js.map