Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
ast-monkey-traverse
Advanced tools
Utility library to traverse parsed HTML (AST's) or anything nested (plain objects within arrays within plain objects)
Utility library to traverse parsed HTML (AST's) or anything nested (plain objects within arrays within plain objects)
npm i ast-monkey-traverse
The default is exported, so instead of "traverse
" below, you can name the consumed function however you want.
Consume via a require()
:
const traverse = require("ast-monkey-traverse");
or as an ES Module:
import traverse from "ast-monkey-traverse";
or for web pages, as a production-ready minified script file (so-called "UMD build"), straight from CDN:
<script src="https://cdn.jsdelivr.net/npm/ast-monkey-traverse/dist/ast-monkey-traverse.umd.js"></script>
// in which case you get a global variable "astMonkeyTraverse" which you consume like this:
const traverse = astMonkeyTraverse;
This package has three builds in dist/
folder:
Type | Key in package.json | Path | Size |
---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports | main | dist/ast-monkey-traverse.cjs.js | 3 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . | module | dist/ast-monkey-traverse.esm.js | 2 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in | browser | dist/ast-monkey-traverse.umd.js | 10 KB |
Walk through every single element of an array or key of an object or every string in the given input, use familiar callback function interface (just like Array.forEach
or Array.map
).
traverse()
is an inner method meant to be used by other functions. It does the actual traversal of the AST tree (or whatever input you gave, from simplest string to most complex spaghetti of nested arrays and plain objects). This method function is used via a callback function, similarly to Array.forEach()
.
const traverse = require("ast-monkey-traverse");
var ast = [{ a: "a", b: "b" }];
ast = traverse(ast, function(key, val, innerObj) {
let current = val !== undefined ? val : key;
// if you are traversing and "stumbled" upon an object, it will have both "key" and "val"
// if you are traversing and "stumbled" upon an array, it will have only "key"
// you can detect either using the principle above.
// you can also now change "current" - what you return will be overwritten.
// return `NaN` to give instruction to delete currently traversed piece of AST.
return current; // #1 <------ it's obligatory to return it, unless you want to assign it to "undefined"
});
It's very important to return the value of the callback function (point marked #1
above) because otherwise whatever you return will be written over the current AST piece being iterated.
If you want to delete, return NaN
.
When you call traverse()
like this:
input = traverse(input, function (key, val, innerObj) {
...
})
you get three variables:
key
val
innerObj
If traverse()
is currently traversing a plain object, going each key/value pair, key
will be the object's current key and val
will be the value.
If traverse()
is currently traversing an array, going through all elements, a key
will be the current element and val
will be null
.
innerObj object's key | Type | Description |
---|---|---|
{ | ||
depth | Integer number | Zero is root, topmost level. Every level deeper increments depth by 1 . |
path | String | The path to the current value. The path uses exactly the same notation as the popular object-path package. For example, a.1.b would be: input object's key a > value is array, take 1 st index (second element in a row, since indexes start from zero) > value is object, take it's key b . |
topmostKey | String | When you are very deep, this is the topmost parent's key. |
parent | Type of the parent of current element being traversed | A whole parent (array or a plain object) which contains the current element. Its purpose is to allow you to query the siblings of the current element. |
parentType | String | Either array if parent is array or object if parent is a plain object (not the "object" type, which includes functions, arrays etc.). |
} |
In monorepo, npm libraries are located in packages/
folder. Inside, the source code is located either in src/
folder (normal npm library) or in the root, cli.js
(if it's a command-line application).
The npm script "dev
", the "dev": "rollup -c --dev --silent"
builds the development version retaining all console.log
s with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.log
s.
MIT License
Copyright (c) 2015-2019 Roy Revelt and other contributors
FAQs
Utility library to traverse AST
The npm package ast-monkey-traverse receives a total of 4,713 weekly downloads. As such, ast-monkey-traverse popularity was classified as popular.
We found that ast-monkey-traverse demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.