Skip to content

Commit

Permalink
feat: truncate constructor option (#143)
Browse files Browse the repository at this point in the history
closes #142
  • Loading branch information
gr2m committed Oct 5, 2023
1 parent c3afa6e commit 26d7719
Show file tree
Hide file tree
Showing 13 changed files with 1,124 additions and 19 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ function (fieldOptionValue, userValue) {
}
```

</td>
</tr>
<tr>
<th>
<code>options.truncate</code>
</th>
<td>
<code>Function</code>
</td>
<td>

Text field values cannot exceed 1024 characters. By default, the `options.truncate` just returns text as is. We recommend to use an establish truncate function such as [loadsh's `_.truncate()`](https://lodash.com/docs/4.17.15#truncate), as byte size is not the same as text length.

</td>
</tr>
</tbody>
Expand Down
9 changes: 9 additions & 0 deletions api/lib/default-truncate-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Maximum text field value length is 1024 bytes. By default, we don't do anything.
*
* @param {string} text
* @returns {string}
*/
export function defaultTruncateFunction(text) {
return text;
}
46 changes: 27 additions & 19 deletions api/lib/get-fields-update-query-and-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export function getFieldsUpdateQueryAndFields(state, fields) {
userName: state.fields[id].userName,
userValue,
}))
.filter(({ id, name }) => {
const field = state.fields[id];
.filter(({ name }) => {
return READ_ONLY_FIELDS.some((readOnlyField) => {
return state.matchFieldName(
readOnlyField.toLowerCase(),
Expand Down Expand Up @@ -102,30 +101,31 @@ export function getFieldsUpdateQueryAndFields(state, fields) {
key,
value: null,
};
} else {
const valueOrOption =
"optionsByValue" in field
? findFieldOptionIdAndValue(state, field, value)
: value;
}

const query = `
const valueOrOption =
"optionsByValue" in field
? findFieldOptionIdAndValue(state, field, value)
: value;

const query = `
${alias}: updateProjectV2ItemFieldValue(input: {projectId: $projectId, itemId: $itemId, fieldId: "${fieldId}", ${toItemFieldValueInput(
state,
field,
valueOrOption
)}}) {
${queryNodes}
}
`;

return {
query,
key,
value:
typeof valueOrOption === "string"
? valueOrOption
: valueOrOption.value,
};
}
return {
query,
key,
value:
typeof valueOrOption === "string"
? valueOrOption
: valueOrOption.value,
};
})
.filter(Boolean);

Expand All @@ -145,14 +145,22 @@ export function getFieldsUpdateQueryAndFields(state, fields) {
}

/**
* @param {import("../..").GitHubProjectStateWithFields} state
* @param {import("../..").ProjectField} field
* @param {string | {id: string, value: string | undefined}} valueOrOption
*
* @returns {string}
*/
function toItemFieldValueInput(field, valueOrOption) {
function toItemFieldValueInput(state, field, valueOrOption) {
console.log("state.truncate");
console.log(state.truncate.toString());

const value =
typeof valueOrOption === "string" ? valueOrOption : valueOrOption.id;
typeof valueOrOption === "string"
? state.truncate(valueOrOption)
: valueOrOption.id;

console.log({ value });

const valueKey =
{
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export type MatchFieldOptionValueFn = (
fieldOptionValue: string,
userValue: string,
) => boolean;
export type TruncateFn = (text: string) => string;

export type GitHubProjectOptions<
TFields extends Record<string, FieldOptions> = {},
Expand All @@ -145,6 +146,7 @@ export type GitHubProjectOptions<
fields?: TFields;
matchFieldName?: MatchFieldNameFn;
matchFieldOptionValue?: MatchFieldOptionValueFn;
truncate?: TruncateFn;
}
| {
owner: string;
Expand All @@ -153,6 +155,7 @@ export type GitHubProjectOptions<
fields?: TFields;
matchFieldName?: MatchFieldNameFn;
matchFieldOptionValue?: MatchFieldOptionValueFn;
truncate?: TruncateFn;
};

export type GitHubProjectItem<
Expand Down Expand Up @@ -289,6 +292,7 @@ export type GitHubProjectState =
type GitHubProjectStateCommon = {
matchFieldName: MatchFieldNameFn;
matchFieldOptionValue: MatchFieldOptionValueFn;
truncate: TruncateFn;
};
type GitHubProjectStateInit = GitHubProjectStateCommon & {
didLoadFields: false;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { removeItemByContentRepositoryAndNumber } from "./api/items.remove-by-co
import { getProperties } from "./api/project.getProperties.js";

import { defaultMatchFunction } from "./api/lib/default-match-function.js";
import { defaultTruncateFunction } from "./api/lib/default-truncate-function.js";

/** @type {import("./").BUILT_IN_FIELDS} */
export const BUILT_IN_FIELDS = {
Expand Down Expand Up @@ -48,6 +49,7 @@ export default class GitHubProject {
matchFieldName: options.matchFieldName || defaultMatchFunction,
matchFieldOptionValue:
options.matchFieldOptionValue || defaultMatchFunction,
truncate: options.truncate || defaultTruncateFunction,
};

// set API
Expand Down

0 comments on commit 26d7719

Please sign in to comment.