-
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
6 changed files
with
159 additions
and
5 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 | ||
--- | ||
|
||
Add Inject Item into Array node, this allows you to add a new item to the array at a given index, also allowing negative index to go last to first |
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,58 @@ | ||
import { | ||
AnyArraySchema, | ||
AnySchema, | ||
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 = 'Inject Item'; | ||
static type = 'studio.tokens.array.inject'; | ||
static description = 'Injects an item into an array at a specified index.'; | ||
|
||
declare inputs: ToInput<{ | ||
array: T[]; | ||
item: T; | ||
index: number; | ||
}>; | ||
|
||
declare outputs: ToOutput<{ | ||
value: T[]; | ||
}>; | ||
|
||
constructor(props: INodeDefinition) { | ||
super(props); | ||
this.addInput('array', { | ||
type: AnyArraySchema | ||
}); | ||
this.addInput('item', { | ||
type: AnySchema | ||
}); | ||
this.addInput('index', { | ||
type: NumberSchema | ||
}); | ||
this.addOutput('array', { | ||
type: AnyArraySchema | ||
}); | ||
} | ||
|
||
execute(): void | Promise<void> { | ||
const { item, index } = this.getAllInputs(); | ||
const array = this.getRawInput('array'); | ||
this.inputs.item.setType(array.type.items); | ||
|
||
// Create a copy of the array value | ||
const result = [...array.value]; | ||
|
||
if (index >= 0) { | ||
result.splice(index, 0, item); | ||
} else { | ||
const insertIndex = Math.max(0, result.length + index + 1); | ||
result.splice(insertIndex, 0, item); | ||
} | ||
|
||
// Set the output using the modified result and the original array type | ||
this.setOutput('array', result, array.type); | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
packages/graph-engine/tests/suites/nodes/array/inject.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,84 @@ | ||
import { Graph } from '../../../../src/graph/graph.js'; | ||
import { describe, expect, test } from 'vitest'; | ||
import Node from '../../../../src/nodes/array/inject.js'; | ||
|
||
describe('array/inject', () => { | ||
test('injects an item at a positive index', async () => { | ||
const graph = new Graph(); | ||
const node = new Node({ graph }); | ||
|
||
node.inputs.array.setValue([1, 2, 3]); | ||
node.inputs.item.setValue(4); | ||
node.inputs.index.setValue(1); | ||
|
||
await node.execute(); | ||
|
||
expect(node.outputs.array.value).to.eql([1, 4, 2, 3]); | ||
}); | ||
|
||
test('injects an item at the beginning with index 0', async () => { | ||
const graph = new Graph(); | ||
const node = new Node({ graph }); | ||
|
||
node.inputs.array.setValue([1, 2, 3]); | ||
node.inputs.item.setValue(0); | ||
node.inputs.index.setValue(0); | ||
|
||
await node.execute(); | ||
|
||
expect(node.outputs.array.value).to.eql([0, 1, 2, 3]); | ||
}); | ||
|
||
test('injects an item at the end with index equal to array length', async () => { | ||
const graph = new Graph(); | ||
const node = new Node({ graph }); | ||
|
||
node.inputs.array.setValue([1, 2, 3]); | ||
node.inputs.item.setValue(4); | ||
node.inputs.index.setValue(3); | ||
|
||
await node.execute(); | ||
|
||
expect(node.outputs.array.value).to.eql([1, 2, 3, 4]); | ||
}); | ||
|
||
test('injects an item at a negative index', async () => { | ||
const graph = new Graph(); | ||
const node = new Node({ graph }); | ||
|
||
node.inputs.array.setValue([1, 2, 3]); | ||
node.inputs.item.setValue(4); | ||
node.inputs.index.setValue(-2); | ||
|
||
await node.execute(); | ||
|
||
expect(node.outputs.array.value).to.eql([1, 2, 4, 3]); | ||
}); | ||
|
||
test('injects an item at the beginning with a large negative index', async () => { | ||
const graph = new Graph(); | ||
const node = new Node({ graph }); | ||
|
||
node.inputs.array.setValue([1, 2, 3]); | ||
node.inputs.item.setValue(0); | ||
node.inputs.index.setValue(-10); | ||
|
||
await node.execute(); | ||
|
||
expect(node.outputs.array.value).to.eql([0, 1, 2, 3]); | ||
}); | ||
|
||
test('does not mutate the original array', async () => { | ||
const graph = new Graph(); | ||
const node = new Node({ graph }); | ||
|
||
const originalArray = [1, 2, 3]; | ||
node.inputs.array.setValue(originalArray); | ||
node.inputs.item.setValue(4); | ||
node.inputs.index.setValue(1); | ||
|
||
await node.execute(); | ||
|
||
expect(originalArray).to.eql([1, 2, 3]); | ||
}); | ||
}); |