Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
fixed-width-parser
Advanced tools
fixed-width-parser
is a node module for parsing data to and from fixed width string
formats.
npm install fixed-width-parser
const { FixedWidthParser } = require('fixed-width-parser');
const fixedWidthParser = new FixedWidthParser([
{
type: 'int',
name: 'age',
width: 2,
},
{
name: 'name',
width: 12,
},
]);
const input = `42 bob
21 alice
33 jeff`;
const result = fixedWidthParser.parse(input);
result
is an array with the following content:
[
{
"age": 42,
"name": "bob"
},
{
"age": 21,
"name": "alice"
},
{
"age": 33,
"name": "jeff"
}
]
const { FixedWidthParser } = require('fixed-width-parser');
const fixedWidthParser = new FixedWidthParser([
{
type: 'int',
name: 'age',
width: 2,
},
{
name: 'name',
width: 12,
},
]);
const input = [
{
age: 42,
name: 'bob',
},
{
age: 21,
name: 'alice',
},
{
age: 33,
name: 'jeff',
},
]
const result = fixedWidthParser.unparse(input);
result
is a string with the following content:
42 bob
21 alice
33 jeff
interface IUnparseOptions {
// Allows truncating values that are too long instead of throwing
// default: false
truncate: boolean;
}
When initializing a new instance of the FixedWidthParser
class, you must provide
an array of parse configs. These configs define how to convert between lines of
text and json objects by providing a mapping between segments of text and object keys.
All parse configs share the following properties:
interface IBaseParseConfig {
// default: 'string'
type?: string;
// number of characters used in fixed width string for this field
// required
width: number;
// zero-based index of starting character in fixed width string for this field
// required
start: number;
// default: 'start'
padPosition?: 'start' | 'end';
// default: ' '
padString?: string;
// value to use when unparsing if field in input is undefined
default?: string | number;
}
An explicit type
property can be provided in each parse config to specify what
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.
// Default config type
interface IStringParseConfig {
type: 'string';
// key name in JSON input/output
// required
name: string;
}
// Will parse using parseInt
interface IIntegerParseConfig {
type: 'int';
// key name in JSON input/output
// required
name: string;
// integer between 2 and 36 that represents the radix of the string
// default: 10
radix?: number;
}
// Will parse using Number() after injecting a decimal if necessary
interface IFloatParseConfig {
type: 'float';
// key name in JSON input/output
// required
name: string;
// whether or not to insert a decimal in formatted string
// default: true
insertDecimal?: boolean;
// number of decimal places to assume if no decimal is present
// default: 2
decimalCount?: number;
}
interface IDateParseConfig {
type: 'date';
// key name in JSON input/output
// required
name: string;
// required
// unicode date field symbol pattern
fixedWidthFormat: string;
// required
// unicode date field symbol pattern
jsonFormat: string;
}
interface IBooleanParseConfig {
type: 'bool';
// key name in JSON input/output
// required
name: string;
// required
// fixed width string value to parse as true
trueValue: string;
// required
// fixed width string value to parse as false
falseValue: string;
}
interface ISkipParseConfig {
type: 'skip';
}
When constructing a new FixedWidthParser
instance the provided parse config will
be validated. If any errors are detected an array of validation errors will be thrown
to help you find and correct the invalid configs.
interface IParseConfigValidationError {
// Index of the invalid parse config
index: number;
// The invalid parse config
config: ParseConfig;
// List of issues detected in the parse config
errors: string[];
}
A huge thanks to @SteveyPugs for his work on fixy
which served as inspiration
for fixed-width-parser
! fixed-width-parser
started out as a fork of fixy
and
evolved into its own library when I got carried away and implemented a new high-level
API.
FAQs
A fixed width data parser
We found that fixed-width-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.