@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
79 lines • 2.6 kB
JavaScript
import { Node } from '../../programmatic/node.js';
import { StringSchema } from '../../schemas/index.js';
export var CaseType;
(function (CaseType) {
CaseType["CAMEL"] = "camel";
CaseType["SNAKE"] = "snake";
CaseType["KEBAB"] = "kebab";
CaseType["PASCAL"] = "pascal";
})(CaseType || (CaseType = {}));
/**
* This node converts strings between different case formats
*/
export default class NodeDefinition extends Node {
static title = 'Case Convert';
static type = 'studio.tokens.string.case';
static description = 'Converts strings between different case formats';
constructor(props) {
super(props);
this.addInput('string', {
type: StringSchema
});
this.addInput('type', {
type: {
...StringSchema,
enum: Object.values(CaseType),
default: CaseType.CAMEL
}
});
this.addInput('delimiters', {
type: {
...StringSchema,
default: '-_.'
}
});
this.addOutput('string', {
type: StringSchema
});
}
execute() {
const { string, type, delimiters } = this.getAllInputs();
// Replace each delimiter with a space
const processedString = delimiters
.split('')
.reduce((result, char) => result.replaceAll(char, ' '), string);
// First normalize the string by splitting on word boundaries
const words = processedString
// Add space before capitals in camelCase/PascalCase
.split(/([A-Z][a-z]+)/)
.join(' ')
// Remove extra spaces and convert to lowercase
.trim()
.toLowerCase()
// Split into words and remove empty strings
.split(/\s+/)
.filter(word => word.length > 0);
let result;
switch (type) {
case CaseType.CAMEL:
result = words[0] + words.slice(1).map(capitalize).join('');
break;
case CaseType.SNAKE:
result = words.join('_');
break;
case CaseType.KEBAB:
result = words.join('-');
break;
case CaseType.PASCAL:
result = words.map(capitalize).join('');
break;
default:
result = string;
}
this.outputs.string.set(result);
}
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
//# sourceMappingURL=case.js.map