
Since these things are so, we must agree that that which keeps its own form unchangingly, which has not been brought into being and is not destroyed, which neither receives into itself anything else from anywhere else, nor itself enters into anything anywhere, is one thing
— Plato, Timaeus
Decomposes JavaScript files into their four fundamental components: shebang, header, body, and footer.
- Each component is an OOP object that can easily be worked with.
- Header and Footer are subdivided further into comment blocks, comment lines or empty lines.
I have made the functionality as robust as I could currently foresee to accomplish the goals I had for this package.
- JavaScript document structure is complex, and it’s likely I didn’t consider all fringe cases and uses.
- The abstract nature of how documents are decomposed could lead to a rapidly evolving API that may not be compatible with current versions.
- I will release a new major version for each breaking change, following a period of evaluation on the current
1.0.0
version.
Note: Use in production environments with caution due to potential structural changes in future major versions
npm install shatterjs
Or with Yarn:
yarn add shatterjs
import { Doc } from 'shatterjs';
const source = `
#!/usr/bin/env node
// This is a header comment
/* Multi-line
block comment */
const hello = 'world';
console.log(hello);
// Footer note
`;
const doc = new Doc();
doc.from(source);
console.log('Shebang:', doc.shebang.content);
console.log('Header:', doc.head.content);
console.log('Body:', doc.body.content);
console.log('Footer:', doc.footer.content);
Detects the shebang line (if present).
Captures decorative comments before code starts.
Framed from the start of valid code to the end of valid code.
All content after the last line of valid code.
Single-line comments (e.g., // comment
).
Block comments spanning multiple lines /* comment */
A line filled only with whitespace or a single line break \n
.
I use GitHub as the primary public development spot: https://github.com/alexstevovich/shatterjs
I created ShatterJS
for my licensing preamble package LawfulGood to identify whether a preamble already exists and where to insert a block of licensing code at the start of a document.
I modularized the functionality as I can see other use cases for this beyond script licensing.
Licensed under the Apache License 2.0.

Leave a Reply