Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @visualblocks/utility-nodes package #75

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions custom_nodes/.changeset/neat-melons-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@visualblocks/utility-nodes': minor
---

Add the @visualblocks/utility-nodes package with 'To Array' and 'From Array'
1 change: 1 addition & 0 deletions custom_nodes/packages/utility-nodes/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from '@visualblocks/eslint-config';
35 changes: 35 additions & 0 deletions custom_nodes/packages/utility-nodes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@visualblocks/utility-nodes",
"version": "0.0.0",
"description": "Utility nodes for Visual Blocks",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"files": [
"dist/**"
],
"author": "",
"license": "Apache-2.0",
"scripts": {
"build": "tsup src/index.ts --format esm --dts --sourcemap --metafile && npm run bundle",
"bundle": "esbuild --bundle src/index.ts --outfile=dist/bundle.js --format=esm --sourcemap --metafile=dist/bundle-metafile.json",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"lint": "eslint .",
"typecheck": "tsc",
"upload-to-gcp": "upload-to-gcp"
},
"devDependencies": {
"@visualblocks/custom-node-types": "*",
"@visualblocks/node-utils": "*",
"@visualblocks/tsconfig": "*",
"@visualblocks/eslint-config": "*",
"@visualblocks/scripts": "*",
"esbuild": "^0.23.0",
"eslint": "^9.8.0",
"tsup": "^8.2.3",
"typescript": "^5.5.4"
},
"dependencies": {
"lit": "^3.1.4"
}
}
111 changes: 111 additions & 0 deletions custom_nodes/packages/utility-nodes/src/from_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @license
* Copyright 2024 Google LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {
Category,
CustomNodeInfo,
DataType,
EditorType,
InputType,
NodeSpec,
OutputSpec,
OutputType,
} from '@visualblocks/custom-node-types';
import {PureFunctionNode} from '@visualblocks/node-utils';

const INPUT_SPECS = [
{
name: 'array',
type: DataType.ANY,
},
] as const;

const PROPERTY_SPECS = [
{
name: 'outputCount',
displayLabel: 'Output Count',
type: DataType.NUMBER,
defaultValue: 1,
editorSpec: {
type: EditorType.NUMBER,
integers: true,
min: 1,
max: 16,
step: 1,
},
},
] as const;

const NODE_SPEC = {
id: 'from-array',
name: 'From Array',
description: 'Convert an array into multiple outputs',
category: Category.PROCESSOR,
inputSpecs: INPUT_SPECS,
propertySpecs: PROPERTY_SPECS,
outputSpecs: [
{
name: 'output0',
displayLabel: 'Output 0',
type: DataType.ANY,
},
] as const,
dynamicIoGeneratorFn: ({outputCount}) => {
if (!(typeof outputCount === 'number')) {
outputCount = 1;
}

const outputSpecs: OutputSpec[] = [];
for (let i = 0; i < outputCount; i++) {
outputSpecs.push({
name: `output${i}`,
displayLabel: `Output ${i}`,
type: DataType.ANY,
});
}

return {
inputSpecs: INPUT_SPECS,
propertySpecs: PROPERTY_SPECS,
outputSpecs,
};
},
} satisfies NodeSpec;

type Inputs = InputType<typeof NODE_SPEC>;
type Outputs = OutputType<typeof NODE_SPEC>;

export class FromArray extends PureFunctionNode<Inputs, Outputs> {
override async run({array, outputCount}: Inputs): Promise<Outputs> {
if (!(array instanceof Array)) {
throw new Error(`Expected type 'Array' but got '${typeof array}'`);
}

const outputs: Record<string, unknown> = {};

for (let i = 0; i < (outputCount ?? 1); i++) {
outputs[`output${i}`] = array[i];
}

return outputs;
}
}

export const FromArrayInfo = {
nodeSpec: NODE_SPEC,
nodeImpl: FromArray,
} satisfies CustomNodeInfo;
26 changes: 26 additions & 0 deletions custom_nodes/packages/utility-nodes/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @license
* Copyright 2024 Google LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {CustomNodeLibrary} from '@visualblocks/custom-node-types';
import {ToArrayInfo} from './to_array';
import {FromArrayInfo} from './from_array';

export default {
registerCustomNodes: register => {
register([ToArrayInfo, FromArrayInfo]);
},
} satisfies CustomNodeLibrary;
62 changes: 62 additions & 0 deletions custom_nodes/packages/utility-nodes/src/to_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license
* Copyright 2024 Google LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {
Category,
CustomNodeInfo,
DataType,
InputType,
NodeSpec,
OutputType,
} from '@visualblocks/custom-node-types';
import {PureFunctionNode} from '@visualblocks/node-utils';

const NODE_SPEC = {
id: 'to-array',
name: 'To Array',
description: 'Convert multiple inputs into an array.',
category: Category.PROCESSOR,
inputSpecs: [
{
name: 'inputs',
type: DataType.ANY,
multiple: true,
},
] as const,
outputSpecs: [
{
name: 'array',
type: DataType.ANY,
},
] as const,
} satisfies NodeSpec;

type Inputs = InputType<typeof NODE_SPEC>;
type Outputs = OutputType<typeof NODE_SPEC>;

export class ToArray extends PureFunctionNode<Inputs, Outputs> {
override async run({inputs}: Inputs): Promise<Outputs> {
return {
array: inputs ?? [],
};
}
}

export const ToArrayInfo = {
nodeSpec: NODE_SPEC,
nodeImpl: ToArray,
} satisfies CustomNodeInfo;
4 changes: 4 additions & 0 deletions custom_nodes/packages/utility-nodes/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@visualblocks/tsconfig/tsconfig.json",
"include": ["*.ts", "src/**/*.ts"]
}