This is a "record-jar":
Planet: MercuryOrbital-Radius: 57,910,000 kmDiameter: 4,880 km%%Planet: VenusOrbital-Radius: 108,200,000 kmDiameter: 12,103.6 km%%Planet: EarthOrbital-Radius: 149,600,000 kmMass: 5.972e24 kgMoons: 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
%, which left the data it sandwiched easy to parse:
"Simple cookie-jar format is appropriate for pieces of text that have nonatural ordering, distinguishable structure above word level, or search keysother 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 iseither formally standardized or sufficiently well established by practice thatthere 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 alsofrequent 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. Akkadianscribes themselves sometimes translated lu2 as ša, the Akkadian relativepronoun."-- 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.
Create a new
RecordJar instance by passing it a block of text as its first argument.
var data = ;var jar = data;
Then loop over its
.records property, reading or matching whatever you need to:
forvar i = 0; i < jarrecordslength; ++iconsole;
When adding new data, you needn't access the array methods of
.records directly: RecordJar instances offer their own
.push method for your convenience:
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;var str = jar + "";console;/** Example output: **/`Planet: EarthOrbital-Radius: 149,600,000 kmDiameter: 12,756.3 kmMass: 5.972e24 kgMoons: Luna%%Name: WaryfishKingdom: AnimaliaGenera: AhliesaurusGenera: LuciosudisGenera: Scopelosaurus`