Typescript and ES6 Javascript class / file generator
JTGenerator supports only installation via composer. So first ensure your composer is installed, configured and ready to use.
$ composer require jtgenerator/jtgenerator
Define initialized constants:
$state = new Constant('TASK');
$state->setValue('data');
$state->setExport();
$file = new ScriptFile();
$file->addContent($state);
echo $file;
Results:
export const TASK = 'data';
Define constants as object:
$state = new Constant('state');
$state->setExport();
$file = new ScriptFile();
$file->addContent($state);
echo $file;
Results:
export const state = {
};
More complex:
$method = new Method('INCREMENT_ACTIVE');
$method->addParameter('state');
$method->setBody('state.active++');
$method2 = new Method('DECREMENT_ACTIVE');
$method2->addParameter('state');
$method2->setBody('if (state.active >= 1) { state.active-- }');
$s = new Constant('mutations');
$s->setExport();
$methods = [$method,$method2];
$s->setMethods(...$methods);
$file = new ScriptFile();
$file->addContent($s);
echo $file;
Results:
export const mutations = {
INCREMENT_ACTIVE(state) {
state.active++
}
DECREMENT_ACTIVE(state) {
if (state.active >= 1) { state.active-- }
}
};
Arrow Functions:
$s = new ArrowFunction();
$s->setExport();
$s->setDefault();
$properties = [
(new Property('[TASKS]'))->setType('[]'),
(new Property('[TOTAL]'))->setType('[]'),
(new Property('[PAGINATION]'))->setType(
[
'page' => 1,
'limit' => 10,
'totalPages' => 0,
'totalItems' => 0
]
)
];
$s->setProperties(...$properties);
$file = new ScriptFile();
$file->addContent($s);
echo $file;
Results:
export default () => ({
[TASKS]: [],
[TOTAL]: [],
[PAGINATION]: {
page: 1,
limit: 10,
totalPages: 0,
totalItems: 0
}
});