fixed-width-parser
Advanced tools
Comparing version 2.1.2 to 2.2.0
import { ParseConfigInput } from './interfaces/ParseConfig'; | ||
import { JsonObject } from './interfaces/json'; | ||
import { ILogger } from './interfaces/ILogger'; | ||
import { IParseOptions } from './interfaces/IParseOptions'; | ||
@@ -7,2 +8,3 @@ import { IFixedWidthParserOptions } from './interfaces/IFixedWidthParserOptions'; | ||
export declare class FixedWidthParser<T extends JsonObject = JsonObject> { | ||
private logger; | ||
private parseConfigMap; | ||
@@ -23,3 +25,3 @@ private fullWidth; | ||
*/ | ||
constructor(parseConfigMap: ParseConfigInput[], options?: IFixedWidthParserOptions); | ||
constructor(parseConfigMap: ParseConfigInput[], options?: IFixedWidthParserOptions, logger?: ILogger); | ||
parse(input: string, options?: Partial<IParseOptions>): T[]; | ||
@@ -26,0 +28,0 @@ unparse(input: unknown[]): string; |
@@ -25,5 +25,7 @@ "use strict"; | ||
*/ | ||
function FixedWidthParser(parseConfigMap, options) { | ||
function FixedWidthParser(parseConfigMap, options, logger) { | ||
var _this = this; | ||
if (logger === void 0) { logger = console; } | ||
var _a; | ||
this.logger = logger; | ||
this.parseLine = function (line, options) { | ||
@@ -70,3 +72,3 @@ return _this.parseConfigMap | ||
} | ||
console.warn("Failed to parse to boolean value. Falling back to " + options.falsyFallback + "."); | ||
_this.logger.warn("Failed to parse to boolean value. Falling back to " + options.falsyFallback + "."); | ||
return handleFalsyFallback_1.handleFalsyFallback(false, options.falsyFallback); | ||
@@ -80,3 +82,3 @@ } | ||
var failValue = handleFalsyFallback_1.handleFalsyFallback(null, options.falsyFallback); | ||
console.warn("Failed to parse to date value. Falling back to " + failValue + "."); | ||
_this.logger.warn("Failed to parse to date value. Falling back to " + failValue + "."); | ||
return failValue; | ||
@@ -259,3 +261,3 @@ } | ||
} | ||
console.warn("Truncating value '" + value + "' to '" + value.slice(0, config.width) + "' to fit in '" + config.name + "' width of '" + config.width + "'."); | ||
_this.logger.warn("Truncating value '" + value + "' to '" + value.slice(0, config.width) + "' to fit in '" + config.name + "' width of '" + config.width + "'."); | ||
value = value.slice(0, config.width); | ||
@@ -262,0 +264,0 @@ } |
@@ -5,1 +5,2 @@ import { FixedWidthParser } from './FixedWidthParser'; | ||
export * from './interfaces/ParseConfig'; | ||
export * from './interfaces/ILogger'; |
@@ -18,1 +18,2 @@ "use strict"; | ||
__exportStar(require("./interfaces/ParseConfig"), exports); | ||
__exportStar(require("./interfaces/ILogger"), exports); |
{ | ||
"name": "fixed-width-parser", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "A fixed width data parser", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -293,2 +293,33 @@ # fixed-width-parser | ||
## Parser Logger | ||
By default any logs from the parser will be handled by the built in `console` logger. | ||
You can optionally provide your own `ILogger` compatible logger to process logs. | ||
```typescript | ||
const { FixedWidthParser, ILogger } = require('fixed-width-parser'); | ||
const myLogger: ILogger = { | ||
warn: (...data: any[]) => console.warn(data); | ||
} | ||
const fixedWidthParser = new FixedWidthParser( | ||
[ | ||
{ | ||
type: 'int', | ||
name: 'age', | ||
start: 0, | ||
width: 2, | ||
}, | ||
{ | ||
name: 'name', | ||
start: 2, | ||
width: 12, | ||
}, | ||
], | ||
undefined, // options | ||
myLogger, // Default: console | ||
); | ||
``` | ||
## Thanks | ||
@@ -295,0 +326,0 @@ |
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
36380
31
589
333