-
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.
* add pad node * format * add tests cases and only take char 0 for pad
- Loading branch information
Showing
4 changed files
with
150 additions
and
4 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 pad node that lets you fill a string to a certain lenght with a given character, like 25 to 025 |
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 |
---|---|---|
@@ -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 | ||
]; |
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,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
75
packages/graph-engine/tests/suites/nodes/string/pad.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,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'); | ||
}); | ||
}); |