react-mapfilter
Version:
These components are designed for viewing data in Mapeo. They share a common interface:
80 lines (71 loc) • 2.59 kB
JavaScript
import "core-js/modules/es.array.iterator";
import "core-js/modules/es.string.replace";
import "core-js/modules/web.dom-collections.iterator";
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
// @flow
/*:: import type { Field, Key } from '../types'*/
/**
* Either returns the translated user-defined label for a field, or creates a
* label from the field key by replacing _ and - with spaces and formatting in
* title case
*/
export function getLocalizedFieldProp(field
/*: Field*/
, prop
/*: 'label' | 'placeholder'*/
, languageTag
/*: string*/
= 'en')
/*: string*/
{
// two-letter or three-letter ISO language code
const languageCode = languageTag.split('-')[0]; // choose most specific label translation available e.g. language tag with
// country code first, then just language code, then label without language
// specified
const label = field[prop + ':' + languageTag] || field[prop + ':' + languageCode] || field[prop];
return label;
}
export function primitiveToString(value
/*: string | boolean | number | null*/
)
/*: string*/
{
if (typeof value === 'string') return value; // TODO: Create translatable strings
if (typeof value === 'boolean') return value ? 'True' : 'False';
if (typeof value === 'number') return value.toString(); // TODO: what to show (translated) for "null" field (vs. undefined)
if (value === null) return 'No Value';
return '';
}
export function fieldKeyToLabel(key
/*: Key*/
)
/*: string | string[]*/
{
const fieldkey = typeof key === 'string' ? [key] : [...key];
const labelArray = _mapInstanceProperty(fieldkey).call(fieldkey, s => titleCase(s + ''));
return labelArray.length === 1 ? labelArray[0] : labelArray;
}
export function sentenceCase(str
/*: string*/
= '') {
// Matches the first letter in the string and the first letter that follows a
// period (and 1 or more spaces) and transforms that letter to uppercase.
return str.replace(/(^[a-z])|(\.\s*[a-z])/g, str => str.toUpperCase());
}
/**
* For a string written in camel_case or snake-case, or space-separated, return
* string formatted in title case
*/
export function titleCase(str
/*: string*/
) {
var _context;
return _mapInstanceProperty(_context = str.toLowerCase().split(/\s|_|-/)).call(_context, word => capitalize(word)).join(' ');
}
export function capitalize(str
/*: string*/
) {
return str.charAt(0).toUpperCase() + _sliceInstanceProperty(str).call(str, 1);
}
//# sourceMappingURL=strings.js.map