Skip to content

Commit e526ab5

Browse files
committed
Use papaParse, support multi-line paste
1 parent ee488eb commit e526ab5

8 files changed

Lines changed: 30 additions & 38 deletions

File tree

packages/components/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@labkey/components",
3-
"version": "7.23.3-fb-parseComma.0",
3+
"version": "7.23.3-fb-parseComma.1",
44
"description": "Components, models, actions, and utility functions for LabKey applications and pages",
55
"sideEffects": false,
66
"files": [

packages/components/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ import {
7070
makeCommaSeparatedString,
7171
parseScientificInt,
7272
quoteValueWithDelimiters,
73-
splitMultiValueForImport,
7473
setIsTestEnv,
74+
splitMultiValueForImport,
7575
uncapitalizeFirstChar,
7676
valueIsEmpty,
7777
withTransformedKeys,
@@ -1539,7 +1539,6 @@ export {
15391539
MAX_EDITABLE_GRID_ROWS,
15401540
MAX_SELECTION_ACTION_ROWS,
15411541
MEASUREMENT_UNITS,
1542-
UNITS_KIND,
15431542
MemberType,
15441543
MenuDivider,
15451544
MenuHeader,
@@ -1725,6 +1724,7 @@ export {
17251724
UnidentifiedPill,
17261725
UNIQUE_ID_FIND_FIELD,
17271726
UnitModel,
1727+
UNITS_KIND,
17281728
updateCellKeySampleIdMap,
17291729
updateCellValuesForSampleIds,
17301730
updateColumnLookup,

packages/components/src/internal/components/editable/actions.test.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,20 +1143,9 @@ describe('insertPastedData', () => {
11431143
selectedColIdx: 0,
11441144
selectedRowIdx: 2,
11451145
});
1146-
const changes = await validateAndInsertPastedData(
1147-
em,
1148-
'"line1\nline2"',
1149-
undefined,
1150-
true,
1151-
true,
1152-
undefined,
1153-
true
1154-
);
1146+
const changes = await validateAndInsertPastedData(em, '"line1\nline2"', undefined, true, true, undefined, true);
11551147
const cellValues = changes.cellValues;
1156-
expect(cellValues.get(genCellKey(fkOne, 2))).toEqual(
1157-
List([
1158-
{ display: 'line1\nline2', raw: 'line1\nline2' }
1159-
]));
1148+
expect(cellValues.get(genCellKey(fkOne, 2))).toEqual(List([{ display: 'line1\nline2', raw: 'line1\nline2' }]));
11601149

11611150
const cellMessages = changes.cellMessages;
11621151
expect(cellMessages.get(genCellKey(fkOne, 2))).toBeUndefined();
@@ -1311,7 +1300,7 @@ describe('insertPastedData', () => {
13111300
expect(changes.cellValues.get(genCellKey(mvtc, 0))).toEqual(
13121301
List([
13131302
{ display: 'A', raw: 'A' },
1314-
{ display: 'B', raw: 'B' }
1303+
{ display: 'B', raw: 'B' },
13151304
])
13161305
);
13171306
expect(changes.cellMessages.get(genCellKey(mvtc, 0))).toBeUndefined();

packages/components/src/internal/components/editable/actions.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,14 +1437,13 @@ function parsePaste(value: string): ParsePastePayload {
14371437
}
14381438

14391439
rows.forEach(row => {
1440-
const columns : List<string> = List(row)
1440+
const columns: List<string> = List(row);
14411441
if (numCols < columns.size) {
14421442
numCols = columns.size;
14431443
}
14441444
data = data.push(columns);
14451445
});
1446-
}
1447-
else {
1446+
} else {
14481447
// fall back to line by line processing without parsing, to preserver quotes
14491448
value.split('\n').forEach(rv => {
14501449
const columns = List(rv.split('\t'));
@@ -1455,7 +1454,6 @@ function parsePaste(value: string): ParsePastePayload {
14551454
});
14561455
}
14571456

1458-
14591457
// Normalize the number columns in each row in case a user pasted rows with different numbers of columns in them
14601458
data = data
14611459
.map(columns => {
@@ -1752,12 +1750,11 @@ export function pasteEvent(
17521750
function getCellCopyValue(valueDescriptors: List<ValueDescriptor>): string {
17531751
if (valueDescriptors && valueDescriptors.size > 0) {
17541752
const values = [];
1755-
valueDescriptors.forEach((vd) => {
1753+
valueDescriptors.forEach(vd => {
17561754
values.push(vd.display !== undefined ? vd.display.toString().trim() : '');
17571755
});
17581756

1759-
if (values.length > 0)
1760-
return joinMultiValueForExport(values);
1757+
if (values.length > 0) return joinMultiValueForExport(values);
17611758
}
17621759

17631760
return '';

packages/components/src/internal/components/forms/model.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ export function formatSavedResults(
129129

130130
const { key, orderedModels } = result;
131131
const models = fromJS(result.models[key]);
132-
const orderedResults = orderedModels[key]
133-
.reduce((ordered, k) => ordered.set(k, models.get(k)), OrderedMap<string, any>());
132+
const orderedResults = orderedModels[key].reduce(
133+
(ordered, k) => ordered.set(k, models.get(k)),
134+
OrderedMap<string, any>()
135+
);
134136

135137
return formatResults(model, orderedResults, token);
136138
}

packages/components/src/internal/util/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ import {
4646
isNonNegativeInteger,
4747
isQuotedWithDelimiters,
4848
isSameWithStringCompare,
49-
isSimpleQuotedMultiLine,
5049
isSetEqual,
50+
isSimpleQuotedMultiLine,
5151
joinMultiValueForExport,
5252
makeCommaSeparatedString,
5353
parseCsvString,

packages/components/src/internal/util/utils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -764,13 +764,11 @@ export function isSimpleQuotedMultiLine(value: any): boolean {
764764
return false;
765765
}
766766

767-
if (!NEWLINE_CHARS.find(char => value.indexOf(char) > -1))
768-
return false;
767+
if (!NEWLINE_CHARS.find(char => value.indexOf(char) > -1)) return false;
769768

770769
const strVal = value + '';
771770
if (strVal.length <= 2) return false; // need at least 2 characters to be quoted with something in between
772-
if (!strVal.startsWith('"') || !strVal.endsWith('"'))
773-
return false
771+
if (!strVal.startsWith('"') || !strVal.endsWith('"')) return false;
774772

775773
const innerValue = strVal.substring(1, strVal.length - 1);
776774
return innerValue.indexOf('"') === -1;
@@ -780,13 +778,19 @@ export function joinMultiValueForExport(values: string[]): string {
780778
return Papa.unparse([values], { delimiter: ',' });
781779
}
782780

783-
const processParsedResults = (results, removeEmpty: boolean = true, trimSpace?: boolean): string[] => {
784-
return results.data[0]?.map(value => (trimSpace && Utils.isString(value) ? value.trim() : value))
785-
.filter(value_ => removeEmpty ? value_ !== '' : true)
786-
}
781+
const processParsedResults = (results, removeEmpty = true, trimSpace?: boolean): string[] => {
782+
return results.data[0]
783+
?.map(value => (trimSpace && Utils.isString(value) ? value.trim() : value))
784+
.filter(value_ => (removeEmpty ? value_ !== '' : true));
785+
};
787786
// Port of Java PageFlowUtil.splitStringToValuesForImport — Google Sheets-compatible CSV parsing
788787
// for multi-value (multi-select) column values. Fixed comma delimiter, double-quote quoting.
789-
export function splitMultiValueForImport(str: string, delimiter: string = ',', removeEmpty: boolean = true, trimSpace?: boolean): string[] {
788+
export function splitMultiValueForImport(
789+
str: string,
790+
delimiter = ',',
791+
removeEmpty = true,
792+
trimSpace?: boolean
793+
): string[] {
790794
if (str === null) return null;
791795
if (str === undefined) return undefined;
792796
if (!str) {

0 commit comments

Comments
 (0)