Coder Social home page Coder Social logo

serjant / bloks-parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fordi/bloks-parser

0.0 0.0 0.0 70 KB

This is a small parser library for parsing the `blok_payload` field of Instagram / Threads API responses.

JavaScript 33.31% PEG.js 66.69%

bloks-parser's Introduction

Blok Parser

This is a small parser library for parsing the blok_payload field of Instagram / Threads API responses. This parser has no API details about IG/Threads APIs; it is only for being able to pick apart the payloads in code.

Usage

import createBlokParser from '@fordi-org/bloks-parser';

const bloks_payload = `
  (bk.action.map.Make,
    (bk.action.array.Make, "login_type", "login_source"),
    (bk.action.array.Make, "Password", "Login")
  )
`;
const parseBloks = createBlokParser();
const bloks = parseBloks(bloks_payload);

and the result will be:

["bk.action.map.Make",
  ["bk.action.map.Make", "login_type", "login_source"],
  ["bk.action.map.Make", "Password", "Login"]
]

The parser is also designed so you can create your own handlers for each blok type when generating the parser, e.g.,

import createBlokParser from '@fordi-org/bloks-parser';

const parse = createBlokParser({
  "bk.action.array.Make": (_, entries) => entries,
  "bk.action.i32.Const": (_, [value]) => parseInt(value),
  "bk.action.bool.Const": (_, [value]) => !!value,
});
const payload = '(bk.action.array.Make, (bk.action.i32.Const, 42069), "nice", (bk.action.bool.Const, true))';
console.log(JSON.stringify(parse(payload)));
[42069, "nice", true]

The special @ processor will allow you to have more custom behavior than just (name, args) => [name, ...args], which is the default. This is useful in that you could have something like,

/*...*/
"@": (name, args) => {
  console.warn(`Unknown blok: ${name} (\n ${JSON.stringify(args, null, 2).split('\n').join('\n  ')}\n)`);
  return [name, ...args];
},
/*...*/

So, like, for debugging, that'll report when it comes across a block type you haven't handled yet.

Contributing

To get the repo working:

npm ci
npm build:peg

If you make changes to src/bloks.pegjs, you must run npm build:peg again.

Of note, there's no unit tests as yet. I'll make a few sometime this week.

bloks-parser's People

Contributors

fordi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.