mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-10-09 15:59:29 +02:00
fix examples
This commit is contained in:
@@ -1,137 +1,141 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { InitialValuesType } from './types';
|
||||||
import { csvColumnsSwap } from './service';
|
import { csvColumnsSwap } from './service';
|
||||||
|
|
||||||
describe('csvColumnsSwap', () => {
|
describe('csvColumnsSwap', () => {
|
||||||
it('should swap columns by position', () => {
|
it('should swap columns by position', () => {
|
||||||
const input = 'A,B,C\n1,2,3\n4,5,6';
|
const input = 'A,B,C\n1,2,3\n4,5,6';
|
||||||
const result = csvColumnsSwap(
|
|
||||||
input,
|
const options: InitialValuesType = {
|
||||||
true, // fromPositionStatus
|
fromPositionStatus: true, // fromPositionStatus
|
||||||
'1', // fromPosition
|
fromPosition: '1', // fromPosition
|
||||||
true, // toPositionStatus
|
toPositionStatus: true, // toPositionStatus
|
||||||
'3', // toPosition
|
toPosition: '3', // toPosition
|
||||||
'', // fromHeader
|
fromHeader: '', // fromHeader
|
||||||
'', // toHeader
|
toHeader: '', // toHeader
|
||||||
true, // dataCompletion
|
emptyValuesFilling: true, // dataCompletion
|
||||||
'', // customFiller
|
customFiller: '', // customFiller
|
||||||
false, // deleteComment
|
deleteComment: false, // deleteComment
|
||||||
'#', // commentCharacter
|
commentCharacter: '#', // commentCharacter
|
||||||
true // emptyLines
|
emptyLines: true // emptyLines
|
||||||
);
|
};
|
||||||
|
|
||||||
|
const result = csvColumnsSwap(input, options);
|
||||||
expect(result).toBe('C,B,A\n3,2,1\n6,5,4');
|
expect(result).toBe('C,B,A\n3,2,1\n6,5,4');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should swap columns by header', () => {
|
it('should swap columns by header', () => {
|
||||||
const input = 'A,B,C\n1,2,3\n4,5,6';
|
const input = 'A,B,C\n1,2,3\n4,5,6';
|
||||||
const result = csvColumnsSwap(
|
const options: InitialValuesType = {
|
||||||
input,
|
fromPositionStatus: false, // fromPositionStatus
|
||||||
false, // fromPositionStatus
|
fromPosition: '', // fromPosition
|
||||||
'', // fromPosition
|
toPositionStatus: false, // toPositionStatus
|
||||||
false, // toPositionStatus
|
toPosition: '', // toPosition
|
||||||
'', // toPosition
|
fromHeader: 'A', // fromHeader
|
||||||
'A', // fromHeader
|
toHeader: 'C', // toHeader
|
||||||
'C', // toHeader
|
emptyValuesFilling: true, // dataCompletion
|
||||||
true, // dataCompletion
|
customFiller: '', // customFiller
|
||||||
'', // customFiller
|
deleteComment: false, // deleteComment
|
||||||
false, // deleteComment
|
commentCharacter: '#', // commentCharacter
|
||||||
'#', // commentCharacter
|
emptyLines: true // emptyLines
|
||||||
true // emptyLines
|
};
|
||||||
);
|
const result = csvColumnsSwap(input, options);
|
||||||
expect(result).toBe('C,B,A\n3,2,1\n6,5,4');
|
expect(result).toBe('C,B,A\n3,2,1\n6,5,4');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fill missing values with custom filler', () => {
|
it('should fill missing values with custom filler', () => {
|
||||||
const input = 'A,B,C\n1,2\n4';
|
const input = 'A,B,C\n1,2\n4';
|
||||||
const result = csvColumnsSwap(
|
const options: InitialValuesType = {
|
||||||
input,
|
fromPositionStatus: true, // fromPositionStatus
|
||||||
true, // fromPositionStatus
|
fromPosition: '1', // fromPosition
|
||||||
'1', // fromPosition
|
toPositionStatus: true, // toPositionStatus
|
||||||
true, // toPositionStatus
|
toPosition: '3', // toPosition
|
||||||
'3', // toPosition
|
fromHeader: '', // fromHeader
|
||||||
'', // fromHeader
|
toHeader: '', // toHeader
|
||||||
'', // toHeader
|
emptyValuesFilling: false, // dataCompletion
|
||||||
false, // dataCompletion
|
customFiller: 'X', // customFiller
|
||||||
'X', // customFiller
|
deleteComment: false, // deleteComment
|
||||||
false, // deleteComment
|
commentCharacter: '#', // commentCharacter
|
||||||
'#', // commentCharacter
|
emptyLines: true // emptyLines
|
||||||
true // emptyLines
|
};
|
||||||
);
|
const result = csvColumnsSwap(input, options);
|
||||||
expect(result).toBe('C,B,A\nX,2,1\nX,X,4');
|
expect(result).toBe('C,B,A\nX,2,1\nX,X,4');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should skip filling missing values', () => {
|
it('should skip filling missing values', () => {
|
||||||
const input = 'A,B,C\n1,2\n4';
|
const input = 'A,B,C\n1,2\n4';
|
||||||
const result = csvColumnsSwap(
|
const options: InitialValuesType = {
|
||||||
input,
|
fromPositionStatus: true, // fromPositionStatus
|
||||||
true, // fromPositionStatus
|
fromPosition: '1', // fromPosition
|
||||||
'1', // fromPosition
|
toPositionStatus: true, // toPositionStatus
|
||||||
true, // toPositionStatus
|
toPosition: '3', // toPosition
|
||||||
'3', // toPosition
|
fromHeader: '', // fromHeader
|
||||||
'', // fromHeader
|
toHeader: '', // toHeader
|
||||||
'', // toHeader
|
emptyValuesFilling: true, // dataCompletion
|
||||||
true, // dataCompletion
|
customFiller: '', // customFiller
|
||||||
'', // customFiller
|
deleteComment: false, // deleteComment
|
||||||
false, // deleteComment
|
commentCharacter: '#', // commentCharacter
|
||||||
'#', // commentCharacter
|
emptyLines: true // emptyLines
|
||||||
true // emptyLines
|
};
|
||||||
);
|
const result = csvColumnsSwap(input, options);
|
||||||
expect(result).toBe('C,B,A\n,2,1\n,,4');
|
expect(result).toBe('C,B,A\n,2,1\n,,4');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error for invalid column positions', () => {
|
it('should throw an error for invalid column positions', () => {
|
||||||
const input = 'A,B,C\n1,2,3\n4,5,6';
|
const input = 'A,B,C\n1,2,3\n4,5,6';
|
||||||
expect(() =>
|
const options: InitialValuesType = {
|
||||||
csvColumnsSwap(
|
fromPositionStatus: true, // fromPositionStatus
|
||||||
input,
|
fromPosition: '0', // fromPosition
|
||||||
true, // fromPositionStatus
|
toPositionStatus: true, // toPositionStatus
|
||||||
'0', // fromPosition
|
toPosition: '3', // toPosition
|
||||||
true, // toPositionStatus
|
fromHeader: '', // fromHeader
|
||||||
'3', // toPosition
|
toHeader: '', // toHeader
|
||||||
'', // fromHeader
|
emptyValuesFilling: true, // dataCompletion
|
||||||
'', // toHeader
|
customFiller: '', // customFiller
|
||||||
true, // dataCompletion
|
deleteComment: false, // deleteComment
|
||||||
'', // customFiller
|
commentCharacter: '#', // commentCharacter
|
||||||
false, // deleteComment
|
emptyLines: true // emptyLines
|
||||||
'#', // commentCharacter
|
};
|
||||||
true // emptyLines
|
expect(() => csvColumnsSwap(input, options)).toThrow(
|
||||||
)
|
'Invalid column positions. Check headers or positions.'
|
||||||
).toThrow('Invalid column positions. Check headers or positions.');
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle empty input gracefully', () => {
|
it('should handle empty input gracefully', () => {
|
||||||
const input = '';
|
const input = '';
|
||||||
const result = csvColumnsSwap(
|
const options: InitialValuesType = {
|
||||||
input,
|
fromPositionStatus: true, // fromPositionStatus
|
||||||
true, // fromPositionStatus
|
fromPosition: '1', // fromPosition
|
||||||
'1', // fromPosition
|
toPositionStatus: true, // toPositionStatus
|
||||||
true, // toPositionStatus
|
toPosition: '3', // toPosition
|
||||||
'3', // toPosition
|
fromHeader: '', // fromHeader
|
||||||
'', // fromHeader
|
toHeader: '', // toHeader
|
||||||
'', // toHeader
|
emptyValuesFilling: true, // dataCompletion
|
||||||
true, // dataCompletion
|
customFiller: '', // customFiller
|
||||||
'', // customFiller
|
deleteComment: false, // deleteComment
|
||||||
false, // deleteComment
|
commentCharacter: '#', // commentCharacter
|
||||||
'#', // commentCharacter
|
emptyLines: true // emptyLines
|
||||||
true // emptyLines
|
};
|
||||||
);
|
const result = csvColumnsSwap(input, options);
|
||||||
expect(result).toBe('');
|
expect(result).toBe('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should remove comments if deleteComment is true', () => {
|
it('should remove comments if deleteComment is true', () => {
|
||||||
const input = '# Comment\nA,B,C\n1,2,3\n4,5,6';
|
const input = '# Comment\nA,B,C\n1,2,3\n4,5,6';
|
||||||
const result = csvColumnsSwap(
|
const options: InitialValuesType = {
|
||||||
input,
|
fromPositionStatus: true, // fromPositionStatus
|
||||||
true, // fromPositionStatus
|
fromPosition: '1', // fromPosition
|
||||||
'1', // fromPosition
|
toPositionStatus: true, // toPositionStatus
|
||||||
true, // toPositionStatus
|
toPosition: '3', // toPosition
|
||||||
'3', // toPosition
|
fromHeader: '', // fromHeader
|
||||||
'', // fromHeader
|
toHeader: '', // toHeader
|
||||||
'', // toHeader
|
emptyValuesFilling: true, // dataCompletion
|
||||||
true, // dataCompletion
|
customFiller: '', // customFiller
|
||||||
'', // customFiller
|
deleteComment: true, // deleteComment
|
||||||
true, // deleteComment
|
commentCharacter: '#', // commentCharacter
|
||||||
'#', // commentCharacter
|
emptyLines: true // emptyLines
|
||||||
true // emptyLines
|
};
|
||||||
);
|
const result = csvColumnsSwap(input, options);
|
||||||
expect(result).toBe('C,B,A\n3,2,1\n6,5,4');
|
expect(result).toBe('C,B,A\n3,2,1\n6,5,4');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user