You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

array-of-arrays-into-ast

Package Overview
Dependencies
Maintainers
1
Versions
171
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-of-arrays-into-ast

turns an array of arrays of data into a nested tree of plain objects

1.4.2
Source
npmnpm
Version published
Weekly downloads
5
-80%
Maintainers
1
Weekly downloads
 
Created
Source

array-of-arrays-into-ast

turns an array of arrays of data into a nested tree of plain objects

Minimum Node version required Repository is on BitBucket Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

Install

npm i array-of-arrays-into-ast
// consume as CommonJS require():
const generateAst = require("array-of-arrays-into-ast");
// or as ES Module:
import generateAst from "array-of-arrays-into-ast";

Here's what you'll get:

TypeKey in package.jsonPathSize
Main export - CommonJS version, transpiled to ES5, contains require and module.exportsmaindist/array-of-arrays-into-ast.cjs.js2 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export.moduledist/array-of-arrays-into-ast.esm.js1 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-inbrowserdist/array-of-arrays-into-ast.umd.js41 KB

⬆ back to top

Table of Contents

What it does

It consumes array of arrays and produces a trie-like AST from them:

Input:

[[1, 2, 3], [1, 2], [5]];

Output:

{
  1: [
    {
      2: [
        {
          3: [null]
        },
        null
      ]
    }
  ],
  5: [null]
}

This library is a piece of a breakthrough code generator I'm producing.

⬆ back to top

API

generateAst (input, [opts])

API - Input

Input argumentTypeObligatory?Description
inputArray of zero or more arraysyesSource of data to put into an AST
otpsPlain objectnoAn Optional Options Object. See its API below.

⬆ back to top

An Optional Options Object

Type: object - an Optional Options Object.

options object's keyTypeDefaultDescription
{
dedupeBooleantrueSkip duplicates
}

Here are all defaults in one place for copying:

{
  dedupe: true,
}

When unused, Optional Options Object can also be passed as a null or undefined value.

⬆ back to top

API - Output

TypeDescription
Plain objectAST of the input

opts.dedupe

If you generate the AST with default settings, dedupe setting will be active and duplicate paths won't be created:

import generateAst from "array-of-arrays-into-ast";
const res = generateAst([[1], [1], [1]]);
console.log(
  `${`\u001b[${33}m${`res`}\u001b[${39}m`} = ${JSON.stringify(res, null, 4)}`
);
// res = {
//   1: [null]
// }

Now, see what happens when you turn off opts.dedupe:

import generateAst from "array-of-arrays-into-ast";
const res = generateAst([[1], [1], [1]], { dedupe: false });
console.log(
  `${`\u001b[${33}m${`res`}\u001b[${39}m`} = ${JSON.stringify(res, null, 4)}`
);
// res = {
//   1: [null, null, null]
// }
}

Notice how entries for each branch were created.

Generally, I don't see the reason why you'd want duplicates, but the setting is there if you ever need it. 👍🏻

⬆ back to top

Principles

Every object's key will have a value of array.

  • null inside that array means it's the tip of the branch.

  • An object inside that array means the branch continues.

Simples.

Compared vs. datastructures-js

There are libraries that produce and manage trie data structures, for example, datastructures-js. In particular case, the problem is, the data structure is abstracted behind the let trie = ds.trie(); and you can't access it directly, traversing the nested tree of arrays and objects.

datastructures-js trie would limit to search(), traverse() and count() methods. However, we need to recursively traverse every node and look up and down, what's around it.

Here's where this library comes in. It doesn't abstract the data it's producing - you get a nested plain object which you can traverse and further process any way you like, using a vast ocean of object- processing libraries.

⬆ back to top

Contributing

  • If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.

  • If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.

  • If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.

⬆ back to top

Licence

MIT License (MIT)

Copyright © 2018 Codsen Ltd, Roy Revelt

Keywords

array

FAQs

Package last updated on 27 Dec 2018

Did you know?

Socket

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.

Install

Related posts