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.
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 }
exportfunctionpack(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 }
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 }
exportfunctionpack(template, ...values) { // 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 }
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.
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.