PageSourceSearch

https://relays.app/lib/codegen.js

js relays.app collected 2026-10-02 00:14:28 UTC 2,518 bytes, 62 lines download raw bytes

1/*!
2 * nodality v1.3.12
3 * (c) 2026 Filip Vabrousek
4 * License: MIT
5 */
6
7/*!
8 * codegen.js — options object -> JavaScript source, for toCode().
9 *
10 * Every toCode() in the library used to hand-roll the same two steps:
11 *
12 *     JSON.stringify(obj, null, 4).replace(/"([^"]+)":/g, '$1:')
13 *
14 * The replace unquotes EVERY key. That is correct for `radius` and wrong
15 * for any key that is not a bare identifier — and the library emits those
16 * now: `vars: { --nod-split: 0.9 }` from the morph expansion, and content
17 * slots like `"main.hero.title"`. Neither parses as JavaScript, so the
18 * emitted code could not be run, pasted, or ejected. Fourteen copies of
19 * the bad spelling had drifted across twelve files, while the correct one
20 * (`/"(\w+)"\s*:/g`) already sat in image.js, table.js, button.js and
21 * side-nav-bar.js — the same rule, written both ways, in one codebase.
22 *
23 * This module is the one place that rule lives now. It is pure: no DOM, no
24 * imports, safe to load anywhere (see __tests__/unit/import-inert).
25 *
26 *   toObjectSource(obj, 4)   // an object -> "{\n    radius: \"2px\"\n}"
27 *   unquoteKeys(jsonString)  // already have the JSON? just fix the keys
28 *   IDENT_KEY                // the pattern, for call sites that chain
29 *                            // further replaces onto the same string
30 */
31
32// A quoted JSON key whose bare form is a valid JS identifier. Anything
33// else — "--nod-split", "main.hero.title", "kebab-case", "0" — is left
34// quoted, because unquoting it would produce a syntax error.
35//
36// Not global-flagged at the source: a /g regex carries mutable lastIndex,
37// and a module-level constant shared by a dozen callers must not. Each
38// consumer gets a fresh copy via keyPattern().
39const IDENT_KEY_SOURCE = '"([A-Za-z_$][A-Za-z0-9_$]*)"\\s*:';
40
41/** A fresh global regex matching unquotable keys. */
42function keyPattern() {
43	return new RegExp(IDENT_KEY_SOURCE, "g");
44}
45
46/** Unquote the identifier-shaped keys of an already-stringified object. */
47function unquoteKeys(jsonString) {
48	if (typeof jsonString !== "string") return jsonString;
49	return jsonString.replace(keyPattern(), "$1:");
50}
51
52/**
53 * Serialize an options object as JavaScript source.
54 *
55 * `indent` matches JSON.stringify's third argument; call sites historically
56 * used 2 or 4 and both are kept so emitted code keeps its existing shape.
57 */
58function toObjectSource(value, indent = 4) {
59	return unquoteKeys(JSON.stringify(value, null, indent));
60}
61
62export { toObjectSource, unquoteKeys, keyPattern };

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.