-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API documentation for parser-utils
- Loading branch information
Showing
10 changed files
with
309 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,78 @@ | ||
# Design Token Parser Utilities | ||
|
||
Low-level logic and utilities for parsing DTCG and DTCG-like files. Used by `@udt/dtcg-parser` to parse DTCG to TOM, but also suitable for building libraries or tools that parse DTCG(-like) token files to other representations, or need to traverse tokens and/or groups. | ||
Low-level logic and utilities to help build parsers for DTCG and DTCG-like files. Will be used by [`@udt/dtcg-parser`](../dtcg-parser/) to parse DTCG to a Token Object Model (TOM), but also suitable for building libraries or tools that parse DTCG(-like) files to other representations, or need to traverse tokens and/or groups. | ||
|
||
## Usage | ||
|
||
```js | ||
import { parseData } from "@udt/parser-utils"; | ||
|
||
// Given a nested object structure representing groups | ||
// and design tokens (e.g. the result of reading a DTCG file | ||
// and running it through JSON.parse())... | ||
const fileData = { /* ... */ }; | ||
|
||
// ...parseData() will traverse through it and pass the | ||
// relevant properties of each group and design token | ||
// object it encounters to the functions you provide in | ||
// the config: | ||
const parsedData = parseData(fileData, { | ||
/* Parser config */ | ||
|
||
// A function that checks whether an object is | ||
// a design token or not (if not, it is assumed | ||
// to be a group). | ||
// | ||
// E.g. for DTCG data, this could check for the | ||
// presence of a $value property. | ||
isDesignTokenData: (data) => { | ||
/* ... */ | ||
return /* true for tokens, false otherwise */; | ||
}, | ||
|
||
// Array of strings and/or RegExp which match | ||
// properties of group objects that are NOT | ||
// names of child design tokens or groups. | ||
// | ||
// E.g. for DTCG data, this could be a RegEx | ||
// like /^$/ which would match all $-prefixed | ||
// format properties | ||
groupPropsToExtract: [ /* ... */ ]; | ||
|
||
// Function which is called for each group data object | ||
// that is encountered. | ||
// | ||
// Is given the extracted properties of that group and its | ||
// path, and should parse that data into whatever structure | ||
// is desired. | ||
parseGroupData: (data, path, contextFromParent) => { | ||
/* ... */ | ||
return { | ||
group: parsedGroup, | ||
|
||
// optional: | ||
addChild: (childName, childGroupOrToken) => { /*... */ }, | ||
|
||
// optional: | ||
contextForChildren: /* anything you like */, | ||
} | ||
}, | ||
|
||
// Function which is called for each design token | ||
// data object that is encountered. | ||
// | ||
// Is given the design token data and its path, and | ||
// should parse that data into whatever structure is | ||
// desired. | ||
parseDesignTokenData: (data, path, contextFromParent) => { | ||
/* ... */ | ||
return parsedDesignToken; | ||
}, | ||
}); | ||
``` | ||
|
||
Note, this package contains TypeScript typings, which are annotated with doc blocks. Please refer to those for more | ||
detail about the parameters and return values of the config | ||
functions. Many popular IDEs (e.g. VSCode) will surface that | ||
info via auto-completions and tooltips as you code, even if | ||
you're writing plain JavaScript. |
This file contains 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 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 |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { isJsonObject } from "./isJsonObject.js"; | ||
import { isPlainObject } from "./isJsonObject.js"; | ||
|
||
describe("isJsonObject()", () => { | ||
describe("isPlainObject()", () => { | ||
it("Returns true for a genuine object", () => { | ||
expect(isJsonObject({ foo: "bar" })).toBe(true); | ||
expect(isPlainObject({ foo: "bar" })).toBe(true); | ||
}); | ||
|
||
it("Returns false for null", () => { | ||
expect(isJsonObject(null)).toBe(false); | ||
expect(isPlainObject(null)).toBe(false); | ||
}); | ||
|
||
it("Returns false for an array", () => { | ||
expect(isJsonObject([1, 2, 3])).toBe(false); | ||
expect(isPlainObject([1, 2, 3])).toBe(false); | ||
}); | ||
|
||
it("Returns false for undefined", () => { | ||
expect(isJsonObject(undefined)).toBe(false); | ||
expect(isPlainObject(undefined)).toBe(false); | ||
}); | ||
|
||
it("Returns false for a function", () => { | ||
expect(isJsonObject(function () {})).toBe(false); | ||
expect(isPlainObject(function () {})).toBe(false); | ||
}); | ||
|
||
it("Returns false for a boolean", () => { | ||
expect(isJsonObject(true)).toBe(false); | ||
expect(isPlainObject(true)).toBe(false); | ||
}); | ||
|
||
it("Returns false for a number", () => { | ||
expect(isJsonObject(42)).toBe(false); | ||
expect(isPlainObject(42)).toBe(false); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
@@ -1,5 +1,19 @@ | ||
export type JsonObject = Record<string, unknown>; | ||
/** | ||
* A plain object. | ||
* | ||
* I.e. `{ ... }`, and not an array or `null`, which | ||
* JavaScript's `typeof` operator would also return | ||
* `"object"` for. | ||
*/ | ||
export type PlainObject = Record<string, unknown>; | ||
|
||
export function isJsonObject(data: unknown): data is JsonObject { | ||
return typeof data === "object" && data !== null && !Array.isArray(data); | ||
/** | ||
* Checks whether a value is a plain object. | ||
* | ||
* @param value The value to check. | ||
* | ||
* @returns `true` if it is a plain object, `false` otherwise. | ||
*/ | ||
export function isPlainObject(value: unknown): value is PlainObject { | ||
return typeof value === "object" && value !== null && !Array.isArray(value); | ||
} |
Oops, something went wrong.