GitXplorerGitXplorer
a

javascript-data-files

public
0 stars
0 forks
3 issues

Commits

List of commits on branch main.
Verified
49b72b29049e7bae003d591c983b4b0071c4844b

Merge pull request #26 from alexwlchan/more-compact-encoding

aalexwlchan committed 4 days ago
Unverified
8bcdaa937835264aed3a38bfe86851c99a87976a

Add tests for encoding short lists of strings

aalexwlchan committed 4 days ago
Unverified
679ae0f7550aee65979500b3ee85fe496fd239b7

Restore the tests for consistent round-tripping

aalexwlchan committed 4 days ago
Verified
88561f5258827382de600641d8660152f0636b8c

Merge pull request #25 from alexwlchan/write-lists-compact

aalexwlchan committed 8 days ago
Unverified
50aba03ff8251ab050f685574c0a0c65c979f40b

Add a more compact and human-readable encoding

aalexwlchan committed 8 days ago
Unverified
61dfab07378ad046a7106d27c4763bbed9c21e93

Break out the encoder as a separate file

aalexwlchan committed 8 days ago

README

The README file for this repository.

javascript-data-files

This is a collection of Python functions for manipulating JavaScript "data files" -- that is, JavaScript files that define a single variable with a JSON value.

This is an example of a JavaScript data file:

const shape = { "sides": 5, "colour": "red" };

Think of this module as the JSON module, but for JavaScript files.

These data files are meant to be both human- and machine-readable.

Usage

  • You can read a JavaScript file with read_js(path, varname)
  • You can write a JavaScript file with write_js(path, value, varname)
  • You can append an item to a JavaScript array with append_to_js_array(path, value)
  • You can append a key-value pair to a JavaScript object with append_to_js_object(path, key, value)

Installation

You have two options:

  1. Copy the file src/javascript folder into your project. You probably want to copy the tests as well.

  2. Install the package using pip:

    $ pip install javascript-data-files

Why not use JSON files?

If you've opening an HTML file from disk, you can load data from a local JavaScript file, for example:

<script src="file://users/alexwlchan/repos/javascript-data-files/data.js"></script>

This is the only way to load data from an external file from an HTML file you've opened locally -- you can't do this with a JSON file, for example.

I have a lot of HTML files and local sites I build with an HTML viewer and metadata in a JavaScript file. The convenience of this approach outweighs the mild annoyance of having to store data in JavaScript, not JSON.

License

MIT.