-
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.
- Loading branch information
Showing
4 changed files
with
56 additions
and
0 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,5 @@ | ||
--- | ||
"@tokens-studio/graph-engine": minor | ||
--- | ||
|
||
An Array Length node was added to get the length of an array. |
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,33 @@ | ||
import { AnyArraySchema, NumberSchema } from '../../schemas/index.js'; | ||
import { INodeDefinition, ToInput, ToOutput } from '../../index.js'; | ||
import { Node } from '../../programmatic/node.js'; | ||
|
||
export default class NodeDefinition<T> extends Node { | ||
static title = 'Array Length'; | ||
static type = 'studio.tokens.array.length'; | ||
static description = 'Determines the length of an array.'; | ||
|
||
declare inputs: ToInput<{ | ||
array: T[]; | ||
}>; | ||
|
||
declare outputs: ToOutput<{ | ||
length: number; | ||
}>; | ||
|
||
constructor(props: INodeDefinition) { | ||
super(props); | ||
this.addInput('array', { | ||
type: AnyArraySchema | ||
}); | ||
this.addOutput('length', { | ||
type: NumberSchema | ||
}); | ||
} | ||
|
||
execute(): void | Promise<void> { | ||
const { array } = this.getAllInputs(); | ||
|
||
this.outputs.length.set(array.length); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/graph-engine/tests/suites/nodes/array/length.test.ts
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,16 @@ | ||
import { Graph } from '../../../../src/graph/graph.js'; | ||
import { describe, expect, test } from 'vitest'; | ||
import Node from '../../../../src/nodes/array/length.js'; | ||
|
||
describe('array/length', () => { | ||
test('return length of array', async () => { | ||
const graph = new Graph(); | ||
const node = new Node({ graph }); | ||
|
||
node.inputs.array.setValue([4, 5, 6]); | ||
|
||
await node.execute(); | ||
|
||
expect(node.outputs.length.value).to.equal(3); | ||
}); | ||
}); |