MCPcopy
hub / github.com/Lulzx/tinypdf / serialize

Function serialize

src/index.ts:90–108  ·  view source on GitHub ↗

* Serialize value to PDF format

(val: PDFValue)

Source from the content-addressed store, hash-verified

88 * Serialize value to PDF format
89 */
90function serialize(val: PDFValue): string {
91 if (val === null || val === undefined) return 'null'
92 if (typeof val === 'boolean') return val ? 'true' : 'false'
93 if (typeof val === 'number') return Number.isInteger(val) ? String(val) : val.toFixed(4).replace(/\.?0+$/, '')
94 if (typeof val === 'string') {
95 if (val.startsWith('/')) return val // name
96 if (val.startsWith('(')) return val // already escaped string
97 return pdfString(val)
98 }
99 if (Array.isArray(val)) return '[' + val.map(serialize).join(' ') + ']'
100 if (val instanceof Ref) return `${val.id} 0 R`
101 if (typeof val === 'object') {
102 const pairs = Object.entries(val)
103 .filter(([_, v]) => v !== undefined)
104 .map(([k, v]) => `/${k} ${serialize(v as PDFValue)}`)
105 return '<<\n' + pairs.join('\n') + '\n>>'
106 }
107 return String(val)
108}
109
110/** PDF object reference */
111class Ref {

Callers 1

buildFunction · 0.85

Calls 1

pdfStringFunction · 0.85

Tested by

no test coverage detected