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-util
Advanced tools
Utility library to traverse parsed HTML (AST's) or anything nested (plain objects within arrays within plain objects)
npm i ast-monkey-util
Consume via a require()
:
const { pathNext, pathPrev, pathUp } = require("ast-monkey-util");
or as an ES Module:
import { pathNext, pathPrev, pathUp } from "ast-monkey-util";
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-util/dist/ast-monkey-util.umd.js"></script>
// in which case you get a global variable "astMonkeyUtil" which you consume like this:
const { pathNext, pathPrev, pathUp } = astMonkeyUtil;
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-util.cjs.js | 2 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . | module | dist/ast-monkey-util.esm.js | 1 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in | browser | dist/ast-monkey-util.umd.js | 1 KB |
codsen-parser
(npm/monorepo) and emlint
(npm/monorepo) both use object-path
notation. This utility program contains helper functions to traverse the paths.
Conceptually, we'd use ast-monkey-traverse
(npm/monorepo), identify the node we need, then get its path (from the same program, from callbacks), then amend that path (using this program), then use object-path
to get/set/delete that path.
pathNext
It takes (a string) path and increments the last digit:
console.log(pathNext("0"));
// => "1"
console.log(pathNext("9.children.3"));
// => "9.children.4"
console.log(pathNext("9.children.1.children.0"));
// => "9.children.1.children.1"
pathPrev
It takes (a string) path and decrements the last digit:
console.log(pathPrev("0"));
// => null
console.log(pathPrev("9.children.33"));
// => "9.children.32"
console.log(pathPrev("9.children.1.children.2"));
// => "9.children.1.children.1"
pathUp
It takes (a string) path and goes "one level" up:
console.log(pathUp("1"));
// => null
console.log(pathUp("9.children.3"));
// => "9"
console.log(pathUp("9.children.1.children.2"));
// => "9.children.1"
Practically, if you think, codsen-parser
(npm/monorepo) root is array. The first element thusly is always a number - that some object, a node, meaning tag or text or whatever.
In codsen-parser
(npm/monorepo) AST's, child nodes are nested within children
key - its value is array:
The following HTML:
<a>text</a>
Would yield AST (many keys omitted):
[
{
"type": "tag",
"start": 0,
"end": 3,
"value": "<a>",
"attribs": [],
"children": [
{
"type": "text",
"start": 3,
"end": 7,
"value": "text"
}
]
},
{
"type": "tag",
"start": 7,
"end": 11,
"value": "</a>",
"attribs": [],
"children": []
}
]
Thus, text node for value "text" is at the path 0.children.0
and "going up" would mean "0" - that's splitting by dot into an array and discarding the last two elements from that array, then joining it back with a dot.
0 . children . 0
^ ^
these two are removed during the "go up" action
object-path
notationThe notation used in this program is based on object-path
- an array elements are marked with dot - if object's key value is an array and we want to a path of the fourth element in there, it's key.3
, not key[3]
.
A drawback of this notation is that keys can't be numeric strings. But the advantage of this notation is that all children are now separated with a dot - you can split by dot String.split(".")
and quickly process the path elements, unlike JS notation with square brackets.
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"
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-2020 Roy Revelt and other contributors
FAQs
Utility library of AST helper functions
We found that ast-monkey-util 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.