npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@leptonite/json-with-imports

v0.2.1

Published

json-with-imports =================

Downloads

2

Readme

json-with-imports

The function readJsonWithImports reads and parses JSON files and supports a special syntax for imports:

  • Use "${import:path/to/file.json}" to import another json file.
  • Use "${import-text:path/to/file.txt}" to import a text file as a single string.
  • Use "${import-first-line:path/to/file.txt}" to import the first line of a text file as a single string.

You can use import-text and import-first-line together with plain strings:

  • "the file path/to/file.txt contains this text:\n${import-text:path/to/file.txt}"
  • "the file path/to/file.txt starts with this line:\n${import-first-line:path/to/file.txt}"
  • "the file path/to/file.txt starts with this line:\n${import-first-line:path/to/file.txt}\nand contains this text:\n${import-text:path/to/file.txt}"

All files are expected to be UTF-8 encoded. Paths may be absolute or relative to the importing file.

Example:

const parsedData = await readJsonWithImports('path/to/file.json');

The given path may be absolute or relative to the current working directory.

Escape special character

If you want to use a literal $ in your strings or a literal } in your file name, you can escape it with another $:

"the file file-with-{$$trange}-name.txt contains this text:\n${import-text:file-with-{$$trange$}-name.txt}"

This expects a file named file-with-{$trange}-name.txt in the same directory as the importing file.

Custom special character

You may use any of #, $, %, & or @ as special character with the specialCharacter option. Default is $ as used in the examples above.

Example:

const parsedData = await readJsonWithImports('path/to/file.json', { specialCharacter: '%' });

This changes the syntax to "the file file-with-50%%-{braces}.txt contains this text:\n%{import-text:file-with-50%%-{braces%}.txt}" (expects a file named file-with-50%-{braces}.txt).

Limit import depth

You may limit import depth with the maxDepth option. Default is no limit.

Example:

const parsedData = await readJsonWithImports('path/to/file.json', { maxDepth: 3 });

maxDepth: 0 does not allow imports at all. maxDepth: 1 allows imports in the given file but not in imported files and so on. maxDepth only affects import but neither import-text nor import-first-line which are always allowed.

readJsonWithImports throws an Error if maxDepth is exceeded.

Custom parser

You may use a custom parser instead of JSON.parse with the jsonParser option.

This may be used to support JSON5 for example:

import * as JSON5 from 'json5';

[...]

const parsedData = await readJsonWithImports('path/to/file.json5', { jsonParser: JSON5.parse });

The optional reviver argument of JSON.parse is not supported.