fixed-width-parser
Advanced tools
Comparing version 1.2.3 to 2.0.0
import { ParseConfigInput } from './interfaces/ParseConfig'; | ||
import { JsonObject } from './interfaces/json'; | ||
import { IUnparseOptions } from './interfaces/IUnparseOptions'; | ||
import { IParseOptions } from './interfaces/IParseOptions'; | ||
interface IFixedWidthParserOptions { | ||
/** | ||
* Will throw an error if config widths do not add up to this value. | ||
*/ | ||
expectedFullWidth?: number; | ||
} | ||
import { IFixedWidthParserOptions } from './interfaces/IFixedWidthParserOptions'; | ||
import { IDefaults } from './interfaces/IDefaults'; | ||
export declare class FixedWidthParser<T extends JsonObject = JsonObject> { | ||
@@ -15,2 +10,6 @@ private parseConfigMap; | ||
/** | ||
* Default values of parameters used during parse and unparse. | ||
*/ | ||
defaults: IDefaults; | ||
/** | ||
* @param parseConfigMap Array of parse configs | ||
@@ -21,9 +20,8 @@ * @param options Additional options | ||
parse(input: string, options?: Partial<IParseOptions>): T[]; | ||
unparse(input: unknown[], options?: Partial<IUnparseOptions>): string; | ||
unparse(input: unknown[]): string; | ||
private parseLine; | ||
private parseLineSegments; | ||
private parseSegment; | ||
private applyDefaults; | ||
private applyDefaultType; | ||
private validateParseConfigs; | ||
} | ||
export {}; |
@@ -26,2 +26,3 @@ "use strict"; | ||
var _this = this; | ||
var _a; | ||
this.parseLine = function (line, options) { | ||
@@ -90,3 +91,3 @@ return _this.parseConfigMap | ||
}; | ||
this.applyDefaults = function (parseConfig) { | ||
this.applyDefaultType = function (parseConfig) { | ||
if (!parseConfig.type) { | ||
@@ -154,3 +155,5 @@ parseConfig.type = 'string'; | ||
} | ||
this.parseConfigMap = parseConfigMap.sort(function (a, b) { return a.start - b.start; }).map(this.applyDefaults); | ||
this.parseConfigMap = parseConfigMap | ||
.sort(function (a, b) { return a.start - b.start; }) | ||
.map(this.applyDefaultType); | ||
var validationErrors = this.parseConfigMap | ||
@@ -166,2 +169,5 @@ .map(this.validateParseConfigs) | ||
} | ||
this.defaults = { | ||
truncate: (_a = options === null || options === void 0 ? void 0 : options.truncate) !== null && _a !== void 0 ? _a : true, | ||
}; | ||
} | ||
@@ -180,3 +186,3 @@ FixedWidthParser.prototype.parse = function (input, options) { | ||
}; | ||
FixedWidthParser.prototype.unparse = function (input, options) { | ||
FixedWidthParser.prototype.unparse = function (input) { | ||
var _this = this; | ||
@@ -187,6 +193,6 @@ if (!input || input.length <= 0) { | ||
var lines = input.map(function (record) { | ||
var _a, _b, _c, _d, _e, _f; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
var line = ''; | ||
for (var _i = 0, _g = _this.parseConfigMap; _i < _g.length; _i++) { | ||
var config = _g[_i]; | ||
for (var _i = 0, _h = _this.parseConfigMap; _i < _h.length; _i++) { | ||
var config = _h[_i]; | ||
var value = ''; | ||
@@ -249,3 +255,4 @@ if (record[config.name] === undefined || record[config.name] === null) { | ||
if (value.length > config.width) { | ||
if (!(options === null || options === void 0 ? void 0 : options.truncate)) { | ||
// Prioritize config-level option over parser-level options | ||
if (!((_g = config.truncate) !== null && _g !== void 0 ? _g : _this.defaults.truncate)) { | ||
throw new Error("Unable to parse value '" + value + "' into width of '" + config.width + "'!"); | ||
@@ -252,0 +259,0 @@ } |
@@ -12,2 +12,3 @@ export declare type ParseConfigInput = ParseConfig | IBaseParseConfig; | ||
default?: string | number; | ||
truncate?: boolean; | ||
} | ||
@@ -14,0 +15,0 @@ export interface ISkipParseConfig extends IBaseParseConfig { |
{ | ||
"name": "fixed-width-parser", | ||
"version": "1.2.3", | ||
"version": "2.0.0", | ||
"description": "A fixed width data parser", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -107,3 +107,3 @@ # fixed-width-parser | ||
}, | ||
] | ||
]; | ||
@@ -121,12 +121,2 @@ const result = fixedWidthParser.unparse(input); | ||
### Unparse Options | ||
```typescript | ||
interface IUnparseOptions { | ||
// Allows truncating values that are too long instead of throwing | ||
// default: false | ||
truncate: boolean; | ||
} | ||
``` | ||
## Config | ||
@@ -156,2 +146,4 @@ | ||
default?: string | number; | ||
// overrides FixedWidthParser.defaults.truncate | ||
truncate?: boolean; | ||
} | ||
@@ -162,3 +154,3 @@ ``` | ||
data types to use for values parsed from strings. Several of these data types require | ||
additional properties to be provided to fully define how parse/unparse values. | ||
additional properties to be provided to fully define how parse/unparse values. | ||
@@ -257,2 +249,18 @@ ```typescript | ||
## Parser Options | ||
```typescript | ||
interface IFixedWidthParserOptions { | ||
// Allows truncating values that are too long instead of throwing. | ||
// | ||
// This value can be overridden by the 'truncate' option in individual parse configs. | ||
// default: true | ||
truncate?: boolean; | ||
// If provided, enables an additional validation of the provided parse config | ||
// map. If sum of all `width` values in the parse config map do not match this | ||
// value, then the an error is thrown. | ||
expectedFullWidth?: number; | ||
} | ||
``` | ||
## Thanks | ||
@@ -264,1 +272,4 @@ | ||
API. | ||
Another huge thanks to @wk-davis for her help with concept discussions and ongoing | ||
development help! |
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
28860
25
447
270