-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
283 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import makeClassMethods from "./makeClassMethods"; | ||
import f from "../test/fixtures/fieldDefinition"; | ||
|
||
describe("makeClassMethods", () => { | ||
it("returns empty string for empty fields", () => { | ||
expect(makeClassMethods([])).toBe(""); | ||
}); | ||
|
||
it("returns string for single field", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the_field_name", | ||
}), | ||
]), | ||
).toContain("the_field_name"); | ||
}); | ||
|
||
it("returns string for multiple fields", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the_field_name", | ||
}), | ||
f({ | ||
name: "the_field_name2", | ||
}), | ||
]), | ||
).toContain("the_field_name2"); | ||
}); | ||
|
||
it("handles field names with spaces", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the field name", | ||
}), | ||
]), | ||
).toContain("getTheFieldName"); | ||
}); | ||
|
||
it("handles field names with spaces in setter", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the field name", | ||
}), | ||
]), | ||
).toContain("setTheFieldName"); | ||
}); | ||
|
||
it("sets return type", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the_field_name", | ||
type: "text", | ||
}), | ||
]), | ||
).toContain("(): string"); | ||
}); | ||
|
||
it("uses mapped type for value arg", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the_field_name", | ||
type: "text", | ||
}), | ||
]), | ||
).toContain("value: string"); | ||
}); | ||
|
||
it("uses mapped type for getField generic", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the_field_name", | ||
type: "text", | ||
}), | ||
]), | ||
).toContain("<string>"); | ||
}); | ||
|
||
it("handles emoji field name", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "🔥", | ||
}), | ||
]), | ||
).toContain("getFire()"); | ||
}); | ||
|
||
it("handles lookup types", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the_field_name", | ||
type: "lookup", | ||
formula_type: "number", | ||
}), | ||
]), | ||
).toContain("number"); | ||
}); | ||
|
||
it("parses numbers", () => { | ||
expect( | ||
makeClassMethods([ | ||
f({ | ||
name: "the_field_name", | ||
type: "number", | ||
}), | ||
]), | ||
).toContain("parseFloat"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { FieldDefinition, ListFieldsResponse } from "../index.js"; | ||
import { makeFieldType } from "./makeFieldType.js"; | ||
import * as emoji from "node-emoji"; | ||
|
||
function toCamelCase(str: string): string { | ||
return emoji | ||
.unemojify(str) | ||
.replaceAll(":", "") | ||
.replace(/([-_\s:][a-z])/gi, ($1) => { | ||
return $1 | ||
.toUpperCase() | ||
.replace("-", "") | ||
.replace("_", "") | ||
.replace(" ", ""); | ||
}); | ||
} | ||
|
||
function makeGetter(field: FieldDefinition): string { | ||
const t = makeFieldType(field); | ||
|
||
if (t.includes("number | string")) { | ||
return `public ${toCamelCase(`get ${field.name}`)}(): number { | ||
return parseFloat(this.getField<${t}>("${field.name}").toString()); | ||
}`; | ||
} | ||
|
||
return `public ${toCamelCase(`get ${field.name}`)}(): ${t} { | ||
return this.getField<${t}>("${field.name}"); | ||
}`; | ||
} | ||
|
||
export default function makeClassMethods(fields: ListFieldsResponse): string { | ||
let classMethods = ""; | ||
|
||
fields.forEach((field) => { | ||
const t = makeFieldType(field); | ||
classMethods += ` | ||
${makeGetter(field)} | ||
public ${toCamelCase(`set ${field.name}`)}(value: ${t}): Promise<void> { | ||
return this.setField("${field.name}", value); | ||
} | ||
`; | ||
}); | ||
|
||
return classMethods; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FieldDefinition } from "../index.js"; | ||
import { mapPrimitive } from "./mapPrimitive.js"; | ||
|
||
export function makeFieldType(field: FieldDefinition): string { | ||
if (field.type === "link_row") { | ||
return '{ "id": number, "value": string }[]'; | ||
} | ||
if (["rollup", "formula", "lookup"].includes(field.type)) { | ||
switch (field.formula_type) { | ||
case "array": | ||
return `(${mapPrimitive(field.array_formula_type)})[]`; | ||
default: | ||
return mapPrimitive(field.formula_type); | ||
} | ||
} | ||
return mapPrimitive(field.type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.