57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
import {deepStrictEqual} from "assert";
|
|
|
|
/**
|
|
* Parses a PB file as found in the [Google Fonts Repository](https://github.com/google/fonts/blob/main/ofl/wixmadefordisplay/METADATA.pb)
|
|
* @param{string} rinput
|
|
* @return{Object}
|
|
*/
|
|
export function parse(rinput) {
|
|
let input = rinput.split("\n").map(v => v.trim()).filter(v => v.length > 0)
|
|
let object = {}
|
|
let object_key = ""
|
|
let object_buffer = ""
|
|
let object_open = false
|
|
|
|
for (let line of input) {
|
|
if (line.endsWith("{")) {
|
|
object_key = line.replaceAll("{", "").trim()
|
|
object[object_key] = {}
|
|
object_buffer = ""
|
|
object_open = true
|
|
} else if (line.endsWith("}")) {
|
|
object_open = false
|
|
} else {
|
|
let { key, value } = parse_value(line)
|
|
if (object_open === true) {
|
|
object[object_key][key] = value
|
|
} else {
|
|
object[key] = value
|
|
}
|
|
}
|
|
}
|
|
|
|
return object
|
|
}
|
|
|
|
/**
|
|
* Parses a single value
|
|
* @param{string} input
|
|
* @return{{key: string, value: string|number}}
|
|
*/
|
|
export function parse_value(input) {
|
|
let [key, ...rvalues] = input.split(":")
|
|
let value = rvalues.join("").trim()
|
|
|
|
if (value.startsWith("\"") && value.endsWith("\"")) {
|
|
return {
|
|
key,
|
|
value: value.slice(1, -1).replaceAll("\\", ""),
|
|
}
|
|
} else {
|
|
return {
|
|
key,
|
|
value: Number(value)
|
|
}
|
|
}
|
|
}
|