Repository navigation
feat(parser): ~DY+^XG upload/recall round-trip for graphics #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { parseStoragePath, formatStoragePath } from "./storagePath"; | ||
|
|
||
| describe("storagePath", () => { | ||
| it("parses device:name without extension", () => { | ||
| expect(parseStoragePath("R:LOGO")).toEqual({ device: "R", name: "LOGO" }); | ||
| }); | ||
|
|
||
| it("parses device:name.ext and drops the extension", () => { | ||
| expect(parseStoragePath("R:LOGO.GRF")).toEqual({ device: "R", name: "LOGO" }); | ||
| }); | ||
|
|
||
| it("returns null for paths without a colon", () => { | ||
| expect(parseStoragePath("LOGO.GRF")).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for paths with empty stem", () => { | ||
| expect(parseStoragePath("R:")).toBeNull(); | ||
| expect(parseStoragePath("R:.GRF")).toBeNull(); | ||
| }); | ||
|
|
||
| it("formats with and without the .GRF extension", () => { | ||
| const path = { device: "E", name: "LABEL" }; | ||
| expect(formatStoragePath(path, true)).toBe("E:LABEL.GRF"); | ||
| expect(formatStoragePath(path, false)).toBe("E:LABEL"); | ||
| }); | ||
|
|
||
| it("round-trips parse → format(true) for a path with extension", () => { | ||
| const original = "R:LOGO.GRF"; | ||
| const parsed = parseStoragePath(original); | ||
| expect(parsed).not.toBeNull(); | ||
| if (parsed) expect(formatStoragePath(parsed, true)).toBe(original); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * Helpers for Zebra storage paths: `device:name` (no extension) and | ||
| * `device:name.ext` (with extension). The two forms appear in different | ||
| * ZPL commands — `~DY` headers use the bare form (extension is encoded | ||
| * in a separate param), `^XG` references use the dot-suffixed form. | ||
| * | ||
| * Keeping the parse/format pair in one place stops the two forms from | ||
| * drifting apart across the parser, emitter, and image registry. | ||
| */ | ||
|
|
||
| export interface StoragePath { | ||
| /** Storage device prefix without trailing colon: "R", "E", "B", "A". */ | ||
| device: string; | ||
| /** Filename stem (no extension). */ | ||
| name: string; | ||
| } | ||
|
|
||
| /** Extension paired with `^GF`-shaped graphic uploads. Zebra firmware | ||
| * persists `~DY{path},*,G,...` as `{path}.GRF` on the device. */ | ||
| export const GRAPHIC_EXT = "GRF"; | ||
|
|
||
| /** | ||
| * Parse a `device:name` or `device:name.ext` storage path into structured | ||
| * parts. The extension (if any) is dropped — callers re-attach via | ||
| * `formatStoragePath` when emitting. Returns null when the input lacks a | ||
| * `:` separator, signalling a malformed path. | ||
| */ | ||
| export function parseStoragePath(raw: string): StoragePath | null { | ||
| const colonAt = raw.indexOf(":"); | ||
| if (colonAt <= 0) return null; | ||
| const device = raw.slice(0, colonAt); | ||
| const stemWithExt = raw.slice(colonAt + 1); | ||
| // Drop everything from the last `.` onwards. dotAt === 0 means the | ||
| // stem starts with a dot (only an extension) — treat as malformed via | ||
| // the empty-name guard below. | ||
| const dotAt = stemWithExt.lastIndexOf("."); | ||
| const name = dotAt === -1 ? stemWithExt : stemWithExt.slice(0, dotAt); | ||
| if (!name) return null; | ||
| return { device, name }; | ||
| } | ||
|
|
||
| /** | ||
| * Render a storage path back to its ZPL form. `withExt: true` produces | ||
| * `device:name.GRF` (for `^XG` recalls); `withExt: false` produces the | ||
| * bare `device:name` (for `~DY` headers, where the extension is encoded | ||
| * in the next param instead). | ||
| */ | ||
| export function formatStoragePath(p: StoragePath, withExt: boolean): string { | ||
| return withExt ? `${p.device}:${p.name}.${GRAPHIC_EXT}` : `${p.device}:${p.name}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.