Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→array-of-arrays-into-astnext

array-of-arrays-into-ast

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

Downloads per monthMIT Licenselibera manifesto
  • the top
  • PURPOSE
  • PRINCIPLES
  • API — GENERATEAST()
  • API — DEFAULTS
  • API — VERSION
  • OPTS — DEDUPE
  • COMPARED VS.…

Purpose

It consumes array of arrays and produces a trieopens in a new tab-like AST from them. This library was a piece of a one experimental code generator of ours.

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.

API — generateAst()

The main function generateAst() is imported like this:

It is a function which takes two arguments: an array and an optional options object:

Input argumentTypeObligatoryDescription
input
Type: Array of zero or more arrays
Obligatory: yes
inputArray of zero or more arraysyesSource of data to put into an AST
opts
Type: Plain object
Obligatory: no
optsPlain objectnoAn Optional Options Object. See its API below.

The optional options object has the following shape:

KeyTypeDefaultDescription
dedupe
Type: Boolean
Default: true
dedupeBooleantrueSkip duplicates

Here are all defaults in one place for copying:

The function returns a plain object, the assembled AST.

API — defaults

You can import defaults:

It's a plain object:

The main function calculates the options to be used by merging the options you passed with these defaults.

API — version

You can import version:

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, we don’t see the reason why you’d want duplicates, but the setting is there if you ever need it.

Compared vs. datastructures-js

There are libraries that produce and manage trie data structures, for example, datastructures-jsopens in a new tab. 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-jsopens in a new tab 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
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab