dope2/lib/structures/struct.js
2025-06-02 15:28:07 +02:00

30 lines
865 B
JavaScript

import { unit } from "../primitives/unit.js";
import { capitalizeFirstLetter } from "../util/util.js";
export const makeConstructor = fieldNames => {
const internal = (fieldNames, ret) => {
if (fieldNames.length === 0) {
const result = ret(unit);
return result;
}
return nextParam => {
const [fieldName, ...rest] = fieldNames;
const wrappedName = 'ctor_' + fieldName;
const newRet = {
[wrappedName]: inner => ({[fieldName]: nextParam, ...ret(inner)}),
}[wrappedName];
return internal(rest, newRet);
}
};
const id = x => x;
return internal(fieldNames, id);
};
export const makeGetters = fieldNames => {
return fieldNames.map(fieldName => {
const getterName = `get${capitalizeFirstLetter(fieldName)}`;
return {
[getterName]: obj => obj[fieldName],
}[getterName];
})
};