GithubHelp home page GithubHelp logo

waldyrious / record-jar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alhadis/record-jar

0.0 1.0 0.0 19 KB

Self-contained JavaScript class for reading and writing data from the "record-jar" metaformat.

Home Page: http://www.catb.org/esr/writings/taoup/html/ch05s02.html#id2906931

License: ISC License

JavaScript 100.00%

record-jar's Introduction

Record-Jar

A self-contained JavaScript class for parsing and packing data in the so-called "record-jar" metaformat.

The what?

This is a "record-jar":

Planet: Mercury
Orbital-Radius: 57,910,000 km
Diameter: 4,880 km
%%
Planet: Venus
Orbital-Radius: 108,200,000 km
Diameter: 12,103.6 km
%%
Planet: Earth
Orbital-Radius: 149,600,000 km
Mass: 5.972e24 kg
Moons: Luna

You might've already seen this format if you've perused the IANA subtag registry. The registry lists every IETF language tag available for identifying the language of web documents, and is perhaps the most frequently-referenced example of a record-jar file.

If the term's unfamiliar to you, it probably brought to mind some arcane compression format, or something to do with Java executables (my-sick-program.jar). The term was actually coined by Eric S. Raymond in his excellent book, The Art of UNIX Programming (p.116) whilst describing various data file metaformats. The name "Record-Jar" was derived from the "Cookie-Jar" metaformat, which listed quotes that were selected at random by the fortune program. Each quote was delimited by a newline-separated %% or %, which left the data it sandwiched easy to parse:

"Simple cookie-jar format is appropriate for pieces of text that have no
natural ordering, distinguishable structure above word level, or search keys
other than their text context."
	-- Eric S. Raymond, "The Art of UNIX Programming", pg 116
%%
"A data file metaformat is a set of syntactic and lexical conventions that is
either formally standardized or sufficiently well established by practice that
there are standard service libraries to handle marshaling and unmarshaling it."
	-- Eric S. Raymond, "The Art of UNIX Programming", pg 112
%%
"lu2 is very common Sumerian texts in its full meaning as "man". It is also
frequent as a head noun in relative clauses modifying animate antecedents;
clause such as that occurring in lines 4-5 can be understood and translated as
"the man who built", "the one who built", "he who built", and so on. Akkadian
scribes themselves sometimes translated lu2 as ša, the Akkadian relative
pronoun."
	-- John L. Hayes, "A Manual of Sumerian Grammar and Texts", pg 106

You get it.

When the hell would I use this?

Probably for the exact same reason I wrote it: for parsing the IANA subtag registry. Furthermore, I don't expect this script will invoke a loadbalancer-shattering level of demand, but it's nice knowing it might benefit somebody out there one day needing to build a list of BCP 47 tags.

Usage

Create a new RecordJar instance by passing it a block of text as its first argument.

var data = load("languages.txt");
var jar  = new RecordJar(data);

Then loop over its .records property, reading or matching whatever you need to:

for(var i = 0; i < jar.records.length; ++i){
	console.log( jar.records[i] );
}

When adding new data, you needn't access the array methods of .records directly: RecordJar instances offer their own .push method for your convenience:

jar.push({
	Name: "Waryfish",
	Kingdom: "Animalia",
	Genera: [
		"Ahliesaurus",
		"Luciosudis",
		"Scopelosaurus"
	]
});

Finally, when typecast to a string, RecordJars will produce compiled, serialisable versions of their contents in the same format they were designed to read:

/** Either of the following two lines will work. You know the drill. */
var str = jar.toString();
var str = jar + "";

console.log(str);

/** Example output: **/
`Planet: Earth
Orbital-Radius: 149,600,000 km
Diameter: 12,756.3 km
Mass: 5.972e24 kg
Moons: Luna
%%
Name: Waryfish
Kingdom: Animalia
Genera: Ahliesaurus
Genera: Luciosudis
Genera: Scopelosaurus`

record-jar's People

Contributors

alhadis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.