Array#pack & String#unpack?

I was browsing through current/closed PRs and issues on the tc39 repo and it doesn't look like anyone has ever proposed bringing Ruby's [].pack and "".unpack (or Python's struct.pack) to JavaScript.

Of all tools I miss from other languages, this is probably one of the big ones - it's hard to recreate how much this tool can do in so few bytes. Has anyone ever brought up proposing to make it part of the language?

I see some existing NPM packages intended to backfill (for example this one mimics Python's: python-struct - npm). Is that reason enough not to ask for it to be included in the standard lib?

I've never used these in Ruby. What are your use cases for it in JS?

Good question, I think a lot of them fall into the "generic utility" category but here's where I'd typically use it in Ruby:

(1) Small inline conversions from one format to another.

For example, converting some RGB values into a hex string.

[29, 142, 33].pack('c*').unpack('H*')   # => '1d8e21'

Or converting a long binary sequence into bytes:

['011001100110111101101111'].pack('B*')   # => 'foo'

(2) Parsing generic binary formats

It's increasingly rare in the modern web era but it still exists (especially if you're writing web software backends that interact with systems like home automation, car starters, lower level mobile data, etc.).

If the spec says something like "each record contains a 16 bit id, followed by an 8 bit node count, followed by that many 16 bit node ids", that becomes as simple as:

record = '\xBB6\u0003\xBF6\u001E7W/'
id, count, nodes = record.unpack('Sca*')   # => [14011, 3, '\xBF6\x1E7W/']
nodes.unpack("S#{count}")                  # => [14015, 14110, 12119]

Are there any popular libraries that provide this functionality on npm? If so, folks would likely have to be persuaded of the value of adding it to the language versus the library. If not, folks would likely think the api is too niche to be worth adding, and thus it might be good to start by creating such a library.

1 Like

@elliot-nelson, I am a little curious when a binary sequence shows up as a literal string in the first place?

I have had similar use cases what you are describing, however, then the data has been Uint8Array and I have used DataView to read from them.

Thank you,
Have a great day