-
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.
* [TASK] add array sort * Update * Update to expose on UI --------- Co-authored-by: SorsOps <[email protected]>
- Loading branch information
Showing
13 changed files
with
227 additions
and
16 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 a sort array node |
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
107 changes: 107 additions & 0 deletions
107
packages/graph-editor/src/components/flow/nodes/array/sort.tsx
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,107 @@ | ||
import { Handle, HandleContainer } from '../../handles.tsx'; | ||
import { PreviewAny } from '../../preview/any.tsx'; | ||
import { PreviewArray } from '../../preview/array.tsx'; | ||
import { | ||
Button, | ||
DropdownMenu, | ||
Stack, | ||
Text, | ||
TextInput, | ||
} from '@tokens-studio/ui'; | ||
import { WrapNode, useNode } from '../../wrapper/nodeV2.tsx'; | ||
import { | ||
node, | ||
OrderMap, | ||
} from '@tokens-studio/graph-engine/nodes/array/sort.js'; | ||
import React, { useCallback } from 'react'; | ||
import { LabelNoWrap } from '#/components/label.tsx'; | ||
import { sentenceCase } from 'sentence-case'; | ||
|
||
const keys: string[] = Object.values(OrderMap); | ||
|
||
const SortArrayNode = () => { | ||
const { input, state, output, setState } = useNode(); | ||
const setValue = useCallback((ev) => { | ||
const value = ev.target.value; | ||
const key = ev.currentTarget.dataset.key; | ||
setState((state) => ({ | ||
...state, | ||
[key]: value, | ||
})); | ||
}, []); | ||
|
||
const setDropdownValue = useCallback((ev) => { | ||
const value = ev.currentTarget.dataset.value; | ||
const key = ev.currentTarget.dataset.key; | ||
setState((state) => ({ | ||
...state, | ||
[key]: value, | ||
})); | ||
}, []); | ||
|
||
return ( | ||
<Stack direction="row" gap={4}> | ||
<HandleContainer type="target"> | ||
<Handle id="array"> | ||
<LabelNoWrap>Array</LabelNoWrap> | ||
<PreviewAny value={input.array} /> | ||
</Handle> | ||
<Handle id="sortBy"> | ||
<LabelNoWrap>Sort By</LabelNoWrap> | ||
|
||
{input.sortBy !== undefined ? ( | ||
<PreviewAny value={input.sortBy} /> | ||
) : ( | ||
<TextInput | ||
onChange={setValue} | ||
value={state.sortBy} | ||
data-key="sortBy" | ||
/> | ||
)} | ||
</Handle> | ||
<Stack direction="row" justify="between" align="center" gap={3}> | ||
<LabelNoWrap>Order</LabelNoWrap> | ||
<DropdownMenu> | ||
<DropdownMenu.Trigger asChild> | ||
<Button variant="secondary" asDropdown size="small"> | ||
{sentenceCase(state.order || '')} | ||
</Button> | ||
</DropdownMenu.Trigger> | ||
|
||
<DropdownMenu.Portal> | ||
<DropdownMenu.Content> | ||
{keys.map((key, i) => { | ||
if (!key) { | ||
return <DropdownMenu.Separator key={i} />; | ||
} | ||
|
||
return ( | ||
<DropdownMenu.Item | ||
key={key} | ||
onClick={setDropdownValue} | ||
data-key="order" | ||
data-value={key} | ||
> | ||
{sentenceCase(key || '')} | ||
</DropdownMenu.Item> | ||
); | ||
})} | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Portal> | ||
</DropdownMenu> | ||
</Stack> | ||
</HandleContainer> | ||
<HandleContainer type="source"> | ||
<Handle id="output"> | ||
<LabelNoWrap>Output</LabelNoWrap> | ||
<PreviewArray value={output?.output} /> | ||
</Handle> | ||
</HandleContainer> | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default WrapNode(SortArrayNode, { | ||
...node, | ||
title: 'Sort 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
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,37 @@ | ||
/** | ||
* Sort a given array without mutating the original | ||
* | ||
* @packageDocumentation | ||
*/ | ||
|
||
import { NodeDefinition, NodeTypes } from "../../types.js"; | ||
import orderBy from "lodash.orderby"; | ||
|
||
const type = NodeTypes.SORT; | ||
|
||
export enum OrderMap { | ||
ASC = "asc", | ||
DESC = "desc", | ||
} | ||
|
||
export type NamedInput = { | ||
array: any[]; | ||
order?: OrderMap.ASC | OrderMap.DESC; // Optional parameter to specify the sort order | ||
sortBy?: string; // Optional parameter to specify the property to sort by | ||
}; | ||
|
||
export const defaults = { | ||
order: OrderMap.ASC, | ||
}; | ||
|
||
export const process = (input: NamedInput, state: NamedInput) => { | ||
const { array, order, sortBy } = { ...state, ...input }; | ||
|
||
return orderBy(array, [sortBy], order); | ||
}; | ||
|
||
export const node: NodeDefinition<NamedInput, NamedInput> = { | ||
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
37 changes: 37 additions & 0 deletions
37
packages/graph-engine/tests/suites/nodes/array/sort.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,37 @@ | ||
import { executeNode } from "#/core.js"; | ||
import { node } from "#/nodes/array/sort.js"; | ||
|
||
describe("array/sort", () => { | ||
it("sorts the values as expected", async () => { | ||
const output = await executeNode({ | ||
input: { | ||
array: [1, 2, 3, 4], | ||
order: "desc", | ||
}, | ||
node, | ||
state: {}, | ||
nodeId: "", | ||
}); | ||
|
||
expect(output).toStrictEqual({ | ||
output: [4, 3, 2, 1], | ||
}); | ||
}); | ||
|
||
it("sorts the values as expected", async () => { | ||
const output = await executeNode({ | ||
input: { | ||
array: [{ a: 3 }, { a: 2 }, { a: 4 }], | ||
order: "asc", | ||
sortBy: "a", | ||
}, | ||
node, | ||
state: {}, | ||
nodeId: "", | ||
}); | ||
|
||
expect(output).toStrictEqual({ | ||
output: [{ a: 2 }, { a: 3 }, { a: 4 }], | ||
}); | ||
}); | ||
}); |
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