-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add features * udpdate changeset * fix: test errors
- Loading branch information
Showing
23 changed files
with
623 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@tokens-studio/graph-engine": minor | ||
--- | ||
|
||
- Adds a parse Unit node. | ||
- Adds `align-items` to the exposed UI | ||
- Adds native supports for tokenSets in input | ||
- Adds Json node (alpha) |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Provides a defined constant for the graph | ||
* | ||
* @packageDocumentation | ||
*/ | ||
|
||
import { NodeDefinition, NodeTypes } from "../../types.js"; | ||
import Ajv, { DefinedError } from "ajv"; | ||
|
||
export class ValidationError extends Error { | ||
errors: DefinedError[] = []; | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "ValidationError"; | ||
} | ||
} | ||
|
||
export const type = NodeTypes.JSON; | ||
|
||
/** | ||
* Defines the starting state of the node | ||
*/ | ||
export const defaults = { | ||
input: "", | ||
schema: "", | ||
}; | ||
|
||
export type Input = { | ||
/** | ||
* The string representation of the JSON object | ||
*/ | ||
input: string; | ||
/** | ||
* The string representation of the JSON schema | ||
*/ | ||
schema: string; | ||
}; | ||
|
||
/** | ||
* Core logic for the node. Will only be called if all inputs are valid. | ||
* Return undefined if the node is not ready to execute. | ||
* Execution can also be optionally delayed by returning a promise. | ||
* @param input | ||
* @param state | ||
* @returns | ||
*/ | ||
export const process = (input: Input, state: Input) => { | ||
const final = { | ||
...state, | ||
...input, | ||
}; | ||
|
||
let inputObject: Record<string, any>; | ||
let schema: Record<string, any>; | ||
|
||
//Attempt to parse the input | ||
try { | ||
inputObject = JSON.parse(final.input); | ||
} catch (e) { | ||
throw new ValidationError("Invalid JSON for input"); | ||
} | ||
|
||
if (!final.schema) { | ||
return inputObject; | ||
} | ||
|
||
//Attempt to parse the schema | ||
try { | ||
schema = JSON.parse(final.schema); | ||
} catch (e) { | ||
throw new ValidationError("Invalid JSON for schema"); | ||
} | ||
|
||
// @ts-ignore This is a weird error with typing | ||
const ajv = new Ajv(); | ||
const validate = ajv.compile(schema); | ||
|
||
//Call the validation function | ||
validate(inputObject); | ||
|
||
if (validate.errors) { | ||
const error = new ValidationError("Validation errors"); | ||
console.log(error); | ||
error.errors = validate.errors as unknown as DefinedError[]; | ||
throw error; | ||
} | ||
|
||
return inputObject; | ||
}; | ||
|
||
export const node: NodeDefinition<Input, Input> = { | ||
type, | ||
defaults, | ||
process, | ||
}; |
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,4 @@ | ||
import { node as passUnit } from "./passUnit.js"; | ||
import { node as parseUnit } from "./parseUnit.js"; | ||
|
||
export const nodes = [passUnit]; | ||
export const nodes = [passUnit, parseUnit]; |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { NodeDefinition, NodeTypes } from "../../types.js"; | ||
|
||
import valueParser from "postcss-value-parser"; | ||
const type = NodeTypes.PARSE_UNIT; | ||
|
||
type Input = { | ||
input: string; | ||
}; | ||
type Output = { | ||
unit?: string; | ||
number?: string; | ||
}; | ||
|
||
/** | ||
* Core logic for the node. Will only be called if all inputs are valid. | ||
* Return undefined if the node is not ready to execute. | ||
* Execution can also be optionally delayed by returning a promise. | ||
* @param input | ||
* @param state | ||
* @returns | ||
*/ | ||
const process = (input: Input) => { | ||
const x = valueParser.unit("" + input.input); | ||
if (!x) { | ||
return {}; | ||
} | ||
|
||
return x; | ||
}; | ||
|
||
const mapOutput = (input: Input, state: any, output: Output) => { | ||
return output; | ||
}; | ||
|
||
export const node: NodeDefinition<Input, any, Output> = { | ||
type, | ||
mapOutput, | ||
process, | ||
}; |
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
Oops, something went wrong.