Comparing version 0.6.0 to 0.7.0
@@ -20,2 +20,6 @@ #! /usr/bin/env node | ||
.options({ | ||
'date-format': { | ||
describe: 'Overwrite detected date format', | ||
type: 'string', | ||
}, | ||
'encoding': { | ||
@@ -38,2 +42,3 @@ describe: 'Overwrite detected input encoding', | ||
.example('cat input.csv | csvnorm > normalized.csv', 'Pipe and normalize a CSV file') | ||
.example('csvnorm --date-format "dd/mm/yyyy" i.csv', 'Normalize a CSV file with an unusual date format') | ||
.version() | ||
@@ -43,3 +48,3 @@ .help() | ||
if (options._.length === 0) { | ||
if (options.inPlace) { | ||
if (options['in-place']) { | ||
console.error('Error: --in-place has no effect with input from stdin'); | ||
@@ -55,2 +60,3 @@ } | ||
index_1.default({ | ||
dateFormat: options['date-format'], | ||
encoding: options.encoding, | ||
@@ -69,2 +75,3 @@ readableStream: stdin, | ||
index_1.default({ | ||
dateFormat: options['date-format'], | ||
encoding: options.encoding, | ||
@@ -71,0 +78,0 @@ filePath: path.resolve(csvFilePath), |
@@ -1,2 +0,2 @@ | ||
declare const _default: (value: string) => string | undefined; | ||
declare const _default: (value: string, dateFormat?: string | undefined) => string | undefined; | ||
export default _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = (value) => { | ||
const formatToPattern = new Map([ | ||
['mm/dd/yyyy', { | ||
regex: /^([01][0-9])\/([0-3][0-9])\/([0-9]{4})$/, | ||
replacement: '$3-$1-$2', | ||
}], | ||
['mm/dd/yy', { | ||
regex: /^([01][0-9])\/([0-3][0-9])\/([0-9]{2})$/, | ||
replacement: '20$3-$1-$2', | ||
}], | ||
['dd.mm.yyyy', { | ||
regex: /^([0-3][0-9])\.([01][0-9])\.([0-9]{4})$/, | ||
replacement: '$3-$2-$1', | ||
}], | ||
['dd.mm.yy', { | ||
regex: /^([0-3][0-9])\.([01][0-9])\.([0-9]{2})$/, | ||
replacement: '20$3-$2-$1', | ||
}], | ||
['dd/mm/yyyy', { | ||
regex: /^([0-3][0-9])\/([01][0-9])\/([0-9]{4})$/, | ||
replacement: '$3-$2-$1', | ||
}], | ||
['dd/mm/yy', { | ||
regex: /^([0-3][0-9])\/([01][0-9])\/([0-9]{2})$/, | ||
replacement: '20$3-$2-$1', | ||
}], | ||
]); | ||
exports.default = (value, dateFormat) => { | ||
if (typeof value !== 'string') { | ||
return undefined; | ||
} | ||
const mmddyyyy = /^([01][0-9])\/([0-3][0-9])\/([0-9]{4})$/; | ||
if (mmddyyyy.test(value)) { | ||
return value.replace(mmddyyyy, '$3-$1-$2'); | ||
const emptyPattern = { regex: / /, replacement: '' }; | ||
let pattern; | ||
if (dateFormat) { | ||
if (!formatToPattern.has(dateFormat)) { | ||
throw new Error('The specified date format is not yet supported'); | ||
} | ||
pattern = formatToPattern.get(dateFormat) || emptyPattern; | ||
if (pattern.regex.test(value)) { | ||
return value.replace(pattern.regex, pattern.replacement); | ||
} | ||
} | ||
const ddmmyyyy = /^([0-3][0-9])\.([01][0-9])\.([0-9]{4})$/; | ||
if (ddmmyyyy.test(value)) { | ||
return value.replace(ddmmyyyy, '$3-$2-$1'); | ||
pattern = formatToPattern.get('mm/dd/yyyy') || emptyPattern; | ||
if (pattern.regex.test(value)) { | ||
return value.replace(pattern.regex, pattern.replacement); | ||
} | ||
const ddmmyy = /^([0-3][0-9])\.([01][0-9])\.([0-9]{2})$/; | ||
if (ddmmyy.test(value)) { | ||
return value.replace(ddmmyy, '20$3-$2-$1'); | ||
pattern = formatToPattern.get('dd.mm.yyyy') || emptyPattern; | ||
if (pattern.regex.test(value)) { | ||
return value.replace(pattern.regex, pattern.replacement); | ||
} | ||
pattern = formatToPattern.get('dd.mm.yy') || emptyPattern; | ||
if (pattern.regex.test(value)) { | ||
return value.replace(pattern.regex, pattern.replacement); | ||
} | ||
return undefined; | ||
}; |
@@ -5,4 +5,6 @@ /// <reference types="node" /> | ||
objectMode?: boolean; | ||
dateFormat?: string; | ||
} | ||
export default class Formatter extends stream.Transform { | ||
private dateFormat?; | ||
constructor(opts: FormatterOptions); | ||
@@ -9,0 +11,0 @@ _transform(row: string[], _1: string, chunkIsProcessedCb: () => void): void; |
@@ -11,2 +11,3 @@ "use strict"; | ||
super(opts); | ||
this.dateFormat = opts.dateFormat; | ||
} | ||
@@ -16,3 +17,3 @@ _transform(row, _1, chunkIsProcessedCb) { | ||
.map((cell) => { | ||
const date = formatDate_1.default(cell); | ||
const date = formatDate_1.default(cell, this.dateFormat); | ||
if (date) { | ||
@@ -19,0 +20,0 @@ return date; |
/// <reference types="node" /> | ||
import * as stream from 'stream'; | ||
interface MainOptions { | ||
dateFormat?: string; | ||
encoding?: string; | ||
@@ -5,0 +6,0 @@ filePath?: string; |
@@ -13,6 +13,6 @@ "use strict"; | ||
function printCsv(options) { | ||
const { configGenerator, encoding, skipLinesStart = 0, | ||
const { configGenerator, dateFormat, encoding, skipLinesStart = 0, | ||
// skipLinesEnd = 0, | ||
inputFilePath, writableStream, } = options; | ||
const formatter = new Formatter_1.default({}); | ||
const formatter = new Formatter_1.default({ dateFormat }); | ||
const parser = csvParse({ | ||
@@ -32,3 +32,2 @@ delimiter: configGenerator.mostFrequentDelimter, | ||
.pipe(writableStream || process.stdout); | ||
// .pipe(process.stdout) | ||
} | ||
@@ -64,3 +63,3 @@ class ConfigGenerator extends stream.Writable { | ||
exports.default = (options) => { | ||
const { encoding, filePath, inPlace, readableStream, | ||
const { dateFormat, encoding, filePath, inPlace, readableStream, | ||
// skipLinesEnd = 0, | ||
@@ -85,2 +84,3 @@ skipLinesStart = 0, } = options; | ||
configGenerator, | ||
dateFormat, | ||
encoding, | ||
@@ -105,2 +105,3 @@ inputFilePath: filePath, | ||
configGenerator, | ||
dateFormat, | ||
encoding, | ||
@@ -107,0 +108,0 @@ inputFilePath: temporaryFilePath, |
@@ -12,3 +12,3 @@ "use strict"; | ||
}); | ||
process.stdout.write('Format banking CSV file with emtpy lines'); | ||
process.stdout.write('Format banking CSV file with empty lines'); | ||
index_1.default({ | ||
@@ -15,0 +15,0 @@ readableStream: fs.createReadStream(path.join(testsDir, 'banking/input-utf-16-le-empty-line.csv')), |
@@ -21,2 +21,3 @@ "use strict"; | ||
'2018-02-22,Ben 🤠,166 $', | ||
'2019-06-24,Marc,274 €', | ||
'', | ||
@@ -23,0 +24,0 @@ ].join('\n')); |
{ | ||
"name": "csvnorm", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Command line tool to normalize CSV and *SV files.", | ||
@@ -5,0 +5,0 @@ "main": "binary/source/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37
595
25621
8