@org-formation/tombok
Version:
Lombok for TypeScript
52 lines (51 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.allArgsConstructor = void 0;
/**
* This decorator generates an all-args constructor for the target class that is
* annotated with `@allArgsConstructor`.
*
* An all-args constructor requires one argument for every field or a object containing
* all fields in the class.
*
* Example:
* ```typescript
* new Person({
* name: 'Adam Savage',
* city: 'San Francisco',
* job: 'Mythbusters',
* });
* ```
*
* Or:
* ```typescript
* new Person('Adam Savage', 'San Francisco', 'Mythbusters');
* ```
*
* @param <T> Type of the base class that must contain a constructor
* @param {T} target Base class that we are going to mutate
*/
function allArgsConstructor(target) {
// todo: get property key, type, and value if available from Base
return class TAllArgsConstructor extends target {
// todo: set constructor arguments to typed arguments matching target properties, in the same order declared
constructor(...args) {
super();
// todo: assign each argument to its matching property
let built = new Map();
if (args.length === 1 && args[0] instanceof Map) {
built = args[0];
}
else {
Object.keys(this).forEach((key, index) => {
const value = args[index];
if (value) {
built.set(key, value);
}
});
}
Object.assign(this, { ...Object.fromEntries(built) });
}
};
}
exports.allArgsConstructor = allArgsConstructor;