Skip to content

Commit

Permalink
add pad node (#478)
Browse files Browse the repository at this point in the history
* add pad node

* format

* add tests cases and only take char 0 for pad
  • Loading branch information
mck authored Aug 2, 2024
1 parent c406277 commit bc39aeb
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-jokes-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-engine": minor
---

Add pad node that lets you fill a string to a certain lenght with a given character, like 25 to 025
10 changes: 6 additions & 4 deletions packages/graph-engine/src/nodes/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import interpolate from './interpolate.js';
import join from './join.js';
import lowercase from './lowercase.js';
import pad from './pad.js';
import regex from './regex.js';
import split from './split.js';
import stringify from './stringify.js';
import uppercase from './uppercase.js';

export const nodes = [
interpolate,
lowercase,
join,
stringify,
lowercase,
pad,
regex,
uppercase,
split
split,
stringify,
uppercase
];
64 changes: 64 additions & 0 deletions packages/graph-engine/src/nodes/string/pad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { INodeDefinition, ToInput, ToOutput } from '../../index.js';
import { Node } from '../../programmatic/node.js';
import { NumberSchema, StringSchema } from '../../schemas/index.js';

export enum Position {
START = 'start',
END = 'end'
}

/**
* This node pads a string to a given length
*/
export default class NodeDefinition extends Node {
static title = 'Pad';
static type = 'studio.tokens.string.pad';
static description = 'Pads a string to a given length';

declare inputs: ToInput<{
string: string;
length: number;
character: string;
position: Position;
}>;
declare outputs: ToOutput<{
string: string;
}>;

constructor(props: INodeDefinition) {
super(props);
this.addInput('string', {
type: StringSchema
});
this.addInput('length', {
type: NumberSchema
});
this.addInput('character', {
type: StringSchema
});
this.addInput('position', {
type: {
...StringSchema,
enum: [Position.START, Position.END],
default: Position.START
}
});
this.addOutput('string', {
type: StringSchema
});
}

execute(): void | Promise<void> {
const { string, length, character, position } = this.getAllInputs();

let paddedString: string;

if (position === Position.START) {
paddedString = string.padStart(length, character[0]);
} else {
paddedString = string.padEnd(length, character[0]);
}

this.setOutput('string', paddedString);
}
}
75 changes: 75 additions & 0 deletions packages/graph-engine/tests/suites/nodes/string/pad.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Graph } from '../../../../src/graph/graph.js';
import { describe, expect, test } from 'vitest';
import Node, { Position } from '../../../../src/nodes/string/pad.js';

describe('string/pad', () => {
test('should pad start of string correctly', async () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.string.setValue('Hello');
node.inputs.length.setValue(10);
node.inputs.character.setValue('*');
node.inputs.position.setValue(Position.START);

await node.execute();

expect(node.outputs.string.value).to.equal('*****Hello');
});

test('should pad end of string correctly', async () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.string.setValue('World');
node.inputs.length.setValue(8);
node.inputs.character.setValue('-');
node.inputs.position.setValue(Position.END);

await node.execute();

expect(node.outputs.string.value).to.equal('World---');
});

test('should not pad if string length is already equal to or greater than specified length', async () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.string.setValue('LongString');
node.inputs.length.setValue(5);
node.inputs.character.setValue('0');
node.inputs.position.setValue(Position.START);

await node.execute();

expect(node.outputs.string.value).to.equal('LongString');
});

test('should use default position (start) if not specified', async () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.string.setValue('Test');
node.inputs.length.setValue(7);
node.inputs.character.setValue('_');
// Not setting position, should default to 'start'

await node.execute();

expect(node.outputs.string.value).to.equal('___Test');
});

test('should use first character if multiple characters are provided', async () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.string.setValue('ABC');
node.inputs.length.setValue(6);
node.inputs.character.setValue('XYZ');
node.inputs.position.setValue(Position.END);

await node.execute();

expect(node.outputs.string.value).to.equal('ABCXXX');
});
});

0 comments on commit bc39aeb

Please sign in to comment.