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.
- ✅ Ready to use
- ⚙ Typescript support
- 🚀 No external libraries
- 💡 Open for more. Request a feature in the issues tab.
- 📝 MIT license
# Via npm:
npm install in-to-js
# Via yarn:
yarn add in-to-js
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"
},
...
];
*/
number(name: string) Parses one number on that line
Parser | Line .in file | Result |
---|---|---|
.number('n') |
50 | data.n = 50 |
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(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(name: string)
Parser | Line .in file | Result |
---|---|---|
.string('s') |
turn left | data.s = "turn left" |
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(name: string, length: number | string, mapFunc?: (line: string) => any)
Parser | Lines .in file | Result |
---|---|---|
.lines('commands', 'n') |
LRL DLR DRU |
data.commands = ["LRL","DLR", "DRU"] |
.lines('commands', 2, line => line.toLowerCase()) |
LRL DLR DRU |
data.commands = ["lrl","dlr"] |
matrix(name: string, length: number | string, splitBy = '', mapFunc?: (line: string) => any)
Parser | Lines .in file | Result |
---|---|---|
.matrix('matrix', 'n') |
123 456 789 |
data.matrix = [[1,2,3], [4,5,6], [7,8,9]] |
.matrix('matrix', 'n', " ", x => x*10) |
1 2 3 4 5 6 7 8 9 |
data.matrix = [[10,20,30], [40,50,60], [70,80,90]] |
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 14 30 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'] } ] |
❓ 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 ❤️.