Perl's core::pack in TypeScript

Rosetta Stone: php/pack

How to use

Install via yarn add locutus and import: import { pack } from 'locutus/perl/core/pack'.

Or with CommonJS: const { pack } = require('locutus/perl/core/pack')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1pack('A5', 'hi')'hi '
2pack('C*', 72, 105, 33)'Hi!'
3pack('n', 258).split('').map((ch) => ch.charCodeAt(0))[1, 2]

Notes

  • Implements a practical subset: A, a, C, n, N, v, V, H, h with optional counts and *.

  • Output is a binary JavaScript string (char codes 0-255).

Here's what our current TypeScript equivalent to Perl's core::pack looks like.

const toByte = (value: number): number => ((Math.trunc(value) % 256) + 256) % 256
const toChar = (value: number): string => String.fromCharCode(toByte(value))

function writeUInt16BE(value: number): string {
const n = Math.trunc(value) >>> 0
return toChar((n >>> 8) & 0xff) + toChar(n & 0xff)
}

function writeUInt16LE(value: number): string {
const n = Math.trunc(value) >>> 0
return toChar(n & 0xff) + toChar((n >>> 8) & 0xff)
}

function writeUInt32BE(value: number): string {
const n = Math.trunc(value) >>> 0
return toChar((n >>> 24) & 0xff) + toChar((n >>> 16) & 0xff) + toChar((n >>> 8) & 0xff) + toChar(n & 0xff)
}

function writeUInt32LE(value: number): string {
const n = Math.trunc(value) >>> 0
return toChar(n & 0xff) + toChar((n >>> 8) & 0xff) + toChar((n >>> 16) & 0xff) + toChar((n >>> 24) & 0xff)
}

function nibble(char: string): number {
const lower = char.toLowerCase()
if (lower >= '0' && lower <= '9') {
return lower.charCodeAt(0) - 48
}
if (lower >= 'a' && lower <= 'f') {
return lower.charCodeAt(0) - 87
}
return 0
}

function packHex(hex: string, highFirst: boolean, nybbles: number): string {
const normalized = hex.replace(/[^0-9a-fA-F]/g, '')
const take = nybbles === -1 ? normalized.length : Math.max(0, nybbles)
const input = normalized.slice(0, take)

let out = ''
for (let i = 0; i < input.length; i += 2) {
const a = nibble(input[i] ?? '0')
const b = nibble(input[i + 1] ?? '0')
const byte = highFirst ? (a << 4) | b : (b << 4) | a
out += toChar(byte)
}

if (input.length % 2 === 1) {
const last = nibble(input.at(-1) ?? '0')
out += toChar(highFirst ? last << 4 : last)
}

return out
}

export function pack(template: string, ...values: unknown[]): string {
// discuss at: https://locutus.io/perl/pack/
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Implements a practical subset: A, a, C, n, N, v, V, H, h with optional counts and *.
// note 2: Output is a binary JavaScript string (char codes 0-255).
// example 1: pack('A5', 'hi')
// returns 1: 'hi '
// example 2: pack('C*', 72, 105, 33)
// returns 2: 'Hi!'
// example 3: pack('n', 258).split('').map((ch) => ch.charCodeAt(0))
// returns 3: [1, 2]

const format = String(template).replace(/\s+/g, '')
let cursor = 0
let argIndex = 0
let out = ''

while (cursor < format.length) {
const directive = format[cursor]
if (!directive) {
break
}
cursor += 1

let countToken = ''
while (cursor < format.length && /[0-9*]/.test(format[cursor] ?? '')) {
countToken += format[cursor]
cursor += 1
}

const count = countToken === '' ? 1 : countToken === '*' ? -1 : Number.parseInt(countToken, 10)

const nextString = (): string => String(values[argIndex++] ?? '')
const nextNumber = (): number => Number(values[argIndex++] ?? 0)

switch (directive) {
case 'A':
case 'a': {
const value = nextString()
const width = count === -1 ? value.length : Math.max(0, count)
const padded = directive === 'A' ? value.padEnd(width, ' ') : value.padEnd(width, '\0')
out += padded.slice(0, width)
break
}

case 'C': {
const repeat = count === -1 ? values.length - argIndex : Math.max(0, count)
for (let i = 0; i < repeat; i++) {
out += toChar(nextNumber())
}
break
}

case 'n': {
const repeat = count === -1 ? values.length - argIndex : Math.max(0, count)
for (let i = 0; i < repeat; i++) {
out += writeUInt16BE(nextNumber())
}
break
}

case 'v': {
const repeat = count === -1 ? values.length - argIndex : Math.max(0, count)
for (let i = 0; i < repeat; i++) {
out += writeUInt16LE(nextNumber())
}
break
}

case 'N': {
const repeat = count === -1 ? values.length - argIndex : Math.max(0, count)
for (let i = 0; i < repeat; i++) {
out += writeUInt32BE(nextNumber())
}
break
}

case 'V': {
const repeat = count === -1 ? values.length - argIndex : Math.max(0, count)
for (let i = 0; i < repeat; i++) {
out += writeUInt32LE(nextNumber())
}
break
}

case 'H':
case 'h': {
const value = nextString()
out += packHex(value, directive === 'H', count)
break
}

default:
break
}
}

return out
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


We have 22 Perl functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

  • Get inspiration from the Perl documentation.
  • Click "New file" in the appropriate folder on GitHub. This will fork the project to your account, directly add the file to it, and send a Pull Request to us.

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.

« More Perl core functions


Star