@browser.style/data-mapper
v1.0.3
Published
Data Mapper component for browser.style
Downloads
7
Maintainers
Readme
DataMapper
A web component for importing and transforming CSV/TSV text files with a visual mapping interface.
Installation
npm install @browser.style/data-mapperBasic Usage
import '@browser.style/data-mapper';<data-mapper>
<label part="row">
<span part="label">File<abbr title="required">*</abbr></span>
<input part="file" type="file" name="file" accept=".csv,.txt,.tsv">
</label>
<label part="row">
<input type="checkbox" part="firstrow" name="firstrow" checked>
<span part="label">First row contains headers</span>
</label>
</data-mapper>Attributes
accept: File types to accept (default:.txt)label: Label text for file input (default:Select file)lang: Language code for UI text (default:en)mapping: JSON string for predefined field mappingsnomount: Prevents automatic initializationrequired: Makes file input requiredseparator: Character to use when concatenating multiple fields (default:\n)
Methods
mount(): Initialize component manually (when usingnomount)process(): Process data with current mappingpreview(format): Generate preview in specified formatdownload(format): Download data in specified formatinitializeComponent(): Re-initialize the component
Properties
content: Get/set the raw content stringoutputData: Get/set processed data arraycustomMapping: Get/set field mappings arrayconverters: Get/set type converters objectformatters: Get/set text formatters object
Events
dm:processed: Fired when file processing is complete. Thedetailproperty contains the processed data array.dm:imported: Fired after successful import operation. Thedetailproperty contains the processed data.dm:error: Fired when an error occurs. Thedetailproperty contains the error message.dm:close: Can be dispatched to close the mapping interface.
Input Formats
- CSV (Comma Separated Values)
- JSON
- NDJSON (Newline Delimited JSON)
- TSV (Tab Separated Values)
- XML
- YAML/YML
Output Formats
Same as input formats. Convert between any supported format.
CSS Custom Properties
:host {
--accent-color: light-dark(hsl(211, 100%, 50%), hsl(211, 60%, 50%));
--accent-color-text: hsl(211, 100%, 95%);
--grey-light: #f3f3f3;
--grey-dark: #333;
--data-mapper-button-bg: light-dark(var(--grey-light), var(--grey-dark));
--data-mapper-input-bg: light-dark(var(--grey-light), var(--grey-dark));
--data-mapper-input-placeholder: light-dark(#CCC, #777);
--data-mapper-close-bg: light-dark(var(--grey-dark), var(--grey-light));
}Custom Mapping
dataMapper.customMapping = [
{
source: 'SOURCE_FIELD', // Source field name
target: 'target_field', // Target field name
order: 0, // Optional order for concatenated fields
type: 'int', // Optional data type converter
formatter: 'titleCase', // Optional text formatter
prefix: 'Prefix: ', // Optional prefix text
suffix: '.' // Optional suffix text
}
];Available Type Converters
boolean: Converts values to true/false (truthy: 'true', '1', 'yes', 'y'; falsy: 'false', '0', 'no', 'n')date: Converts dates from MM/DD/YYYY to YYYY-MM-DD formatfloat: Converts to floating point number (returns null if invalid)int: Converts to integer (returns null if invalid)number: Converts to number (returns null if invalid)
Built-in Formatters
capitalize: Capitalizes first lettercurrency: Formats as currency with 2 decimal placeslowercase: Converts to lowercasepercentage: Adds % symbolremoveSpaces: Removes all spacesslugify: Creates URL-friendly slug (lowercase, hyphens, no special chars)titleCase: Capitalizes Each Wordtrim: Removes leading/trailing spacestruncate: Limits text to 100 characters with ellipsisuppercase: Converts to uppercase
Custom Formatters
dataMapper.formatters = {
isbn: str => {
if (!str) return null;
const cleaned = str.replace(/\D/g, '');
return cleaned.length >= 13 ? cleaned.slice(0, 13) : cleaned;
}
};Styling Parts
button: Action buttonsclose: Close buttonfile: File inputfirstrow: First row checkboxicon: SVG iconsimport: Import buttonlabel: Labelsmapping: Mapping containermapping-content: Mapping fields containermapping-header: Column headersmapping-input: Mapping input fieldsmapping-nav: Navigation buttonsmapping-row: Mapping rowoutput: Preview/output areapreview: Preview buttonrow: Form row
