0.0.2 • Published 7 months ago

in-to-js v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

in-to-js Parser

Convert any .in/.txt files into a javascript/typescript object. Ideal for low-level js developers who frequently work with files or coding contest participants.

npm GitHub issues npm TypeScript

Demo

Features

  • āœ… Ready to use
  • āš™ Typescript support
  • šŸš€ No external libraries
  • šŸ’” Open for more. Request a feature in the issues tab.
  • šŸ“ MIT license

Install

# Via npm:
npm install in-to-js

# Via yarn:
yarn add in-to-js

Usage

import * as fs from 'fs';
import { InToJSParser } from 'in-to-js';

const s = fs.readFileSync('ghosts.in',{encoding: 'utf-8'}).split("\n");

const data = InToJSParser.create(s)
    .number("ghostCount")
    .arrayOfObject("ghosts", "ghostCount", P =>
        P
            .string('name')
            .numbers('x y')
            .number('walkingSpeed')
            .array('path')
    )
    .build();


/* Results:

    data.ghostCount = 5;
    data.ghosts = [{
            name: "Casper",
            x: 10,
            y: 2,
            walkingSpeed: 10,
            path: "UDLRUDL"
        },
        ...
    ];
*/

Functions

Number

number(name: string) Parses one number on that line

ParserLine .in fileResult
.number('n')50data.n = 50

Numbers

numbers(names: string, splitBy = ' ') |Parser| Line .in file | Result | |--|--| -- | | .number('x y') | 7 13 | data.x = 7, data.y = 13 | .number('x/y', '/') | 1/3 | data.x = 1, data.y = 3

Skip

skip(length: number | string = 1) |Parser| Line .in file | Result | |--|--| -- | | .skip() | ignore-me| Skips one line | .skip(3) | ... | Skips multiple lines | .number('n').skip('n') | ... | Skips n number of lines

String

string(name: string) |Parser| Line .in file | Result | |--|--| -- | | .string('s') | turn left | data.s = "turn left"

Array

array(name: string, splitBy = '', mapFunc?: (x: string) => any) |Parser| Line .in file | Result | |--|--| -- | | .array('arr') | LRLRLLRDUU | data.arr = ['L', 'R', 'L', 'R', 'L', 'L', 'R', 'D', 'U', 'U'] | .array('arr', ' ', x => x*10) | 1 2 3 4| data.arr = [10, 20, 30, 40]

Lines

lines(name: string, length: number | string, mapFunc?: (line: string) => any) |Parser| Lines .in file | Result | |--|--| -- | | .lines('commands', 'n') | LRLDLRDRU | data.commands = ["LRL","DLR", "DRU"] | .lines('commands', 2, line => line.toLowerCase()) | LRLDLRDRU | data.commands = ["lrl","dlr"]

Matrix

matrix(name: string, length: number | string, splitBy = '', mapFunc?: (line: string) => any) |Parser| Lines .in file | Result | |--|--| -- | | .matrix('matrix', 'n') | 123456789 | data.matrix = [[1,2,3], [4,5,6], [7,8,9]] | .matrix('matrix', 'n', " ", x => x*10) | 1 2 34 5 67 8 9 | data.matrix = [[10,20,30], [40,50,60], [70,80,90]]

Array of objects

arrayOfObject(name: string, length: number | string, p: (P: Parser) => any) |Parser| Lines .in file | Result | |--|--| -- | | .arrayOfObject("ghosts", 'ghostCount', (P) =>P.numbers('x y').number("stepCount").array("path")) | 10 1430 UUDULLRLLRRRDDUDDUUULLLUUDDRLR| data.ghosts = [ {x:10, y:14,stepCount: 30,path: ['U','U','D','U','L','L','R','L','L','R','R','R','D','D','U','D','D','U','U','U','L','L','L','U','U','D','D','R','L','R'] } ]

FAQ

ā“ Where can I find more examples?
šŸ’¬ Use the link below:

ā“ What node versions does this package support?
šŸ’¬ This project doesn't use external libraries or node functions. It uses only pure javascript, so it should work with any version. Let me know in the issues tab if you face any problems.

ā“ Are ideas welcome?
šŸ’¬ We value all ideas, improvements, suggestions and pull requests ā¤ļø.

0.0.2

7 months ago

0.0.1

7 months ago