Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setter functions to be named like With.name With.anotherField #1

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ module RecordSetter exposing (..)


s_f1 : a -> { b | f1 : a } -> { b | f1 : a }
s_f1 value record =
{ record | f1 = value }
s_f1 value__ record__ =
{ record__ | f1 = value__ }


s_f2 : a -> { b | f2 : a } -> { b | f2 : a }
s_f2 value record =
{ record | f2 = value }
s_f2 value__ record__ =
{ record__ | f2 = value__ }

...
```
Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ module ${moduleName} exposing (..)
`;
}

function setterDefinition(recordFieldIdentifier) {
return `s_${recordFieldIdentifier} : a -> { b | ${recordFieldIdentifier} : a } -> { b | ${recordFieldIdentifier} : a }
s_${recordFieldIdentifier} value record =
{ record | ${recordFieldIdentifier} = value }
function setterDefinition(prefix) {
return function(recordFieldIdentifier) {
return `${prefix}${recordFieldIdentifier} : a -> { b | ${recordFieldIdentifier} : a } -> { b | ${recordFieldIdentifier} : a }
${prefix}${recordFieldIdentifier} value__ record__ =
{ record__ | ${recordFieldIdentifier} = value__ }
`;
}
}

const cwd = process.cwd();
Expand All @@ -46,10 +48,10 @@ function reducePerFile(identifierSet, filepath) {
return sourceToIdentifiers(source, identifierSet);
}

function generate(filepaths = [], moduleName = "RecordSetter") {
function generate(filepaths = [], moduleName = "RecordSetter", prefix = "s_") {
const uniqIdentifiers = [filepaths].flat().reduce(reducePerFile, new Set());
const sortedUniqIdentifiers = [...uniqIdentifiers].sort();
const setters = sortedUniqIdentifiers.map(setterDefinition);
const setters = sortedUniqIdentifiers.map(setterDefinition(prefix));
return [moduleDeclaration(moduleName), ...setters].join("\n\n");
}

Expand Down
7 changes: 6 additions & 1 deletion setem.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ program
"--src, --output <srcDirectory>",
"Set Elm src directory to write generated file to. Defaults to current working directory"
)
.option(
"--prefix <function prefix>",
"Set prefix of generated functions. Defaults to `s_`"
)
.option(
"--module <moduleName>",
`Set generated module name (also, file name). Defaults to \`RecordSetter\`.
Expand All @@ -32,9 +36,10 @@ Must be fully qualified

function mainAction(args, options) {
const module = options.module || "RecordSetter";
const prefix = options.prefix === undefined ? "s_" : options.prefix;
const paths = [...new Set(expandDirs(args))];
if (options.verbose) for (const path of paths) fileLoaded(path);
const generated = generate(paths, module);
const generated = generate(paths, module, prefix);

if (options.stdout) {
console.log(generated);
Expand Down
44 changes: 22 additions & 22 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ module RecordSetter exposing (..)


s_f1 : a -> { b | f1 : a } -> { b | f1 : a }
s_f1 value record =
{ record | f1 = value }
s_f1 value__ record__ =
{ record__ | f1 = value__ }


s_f2 : a -> { b | f2 : a } -> { b | f2 : a }
s_f2 value record =
{ record | f2 = value }
s_f2 value__ record__ =
{ record__ | f2 = value__ }


s_f3 : a -> { b | f3 : a } -> { b | f3 : a }
s_f3 value record =
{ record | f3 = value }
s_f3 value__ record__ =
{ record__ | f3 = value__ }


s_f3_f1 : a -> { b | f3_f1 : a } -> { b | f3_f1 : a }
s_f3_f1 value record =
{ record | f3_f1 = value }
s_f3_f1 value__ record__ =
{ record__ | f3_f1 = value__ }


s_f3_f2 : a -> { b | f3_f2 : a } -> { b | f3_f2 : a }
s_f3_f2 value record =
{ record | f3_f2 = value }
s_f3_f2 value__ record__ =
{ record__ | f3_f2 = value__ }
`;

test("generate from RecordDefAndExpr", () => {
Expand Down Expand Up @@ -63,18 +63,18 @@ module RecordSetter exposing (..)


s_f1 : a -> { b | f1 : a } -> { b | f1 : a }
s_f1 value record =
{ record | f1 = value }
s_f1 value__ record__ =
{ record__ | f1 = value__ }


s_f2 : a -> { b | f2 : a } -> { b | f2 : a }
s_f2 value record =
{ record | f2 = value }
s_f2 value__ record__ =
{ record__ | f2 = value__ }


s_f3 : a -> { b | f3 : a } -> { b | f3 : a }
s_f3 value record =
{ record | f3 = value }
s_f3 value__ record__ =
{ record__ | f3 = value__ }
`);
});

Expand All @@ -87,17 +87,17 @@ module RecordSetter exposing (..)


s_f1 : a -> { b | f1 : a } -> { b | f1 : a }
s_f1 value record =
{ record | f1 = value }
s_f1 value__ record__ =
{ record__ | f1 = value__ }


s_f2 : a -> { b | f2 : a } -> { b | f2 : a }
s_f2 value record =
{ record | f2 = value }
s_f2 value__ record__ =
{ record__ | f2 = value__ }


s_f3 : a -> { b | f3 : a } -> { b | f3 : a }
s_f3 value record =
{ record | f3 = value }
s_f3 value__ record__ =
{ record__ | f3 = value__ }
`);
});