Skip to content

Commit 26d7719

Browse files
authored
feat: truncate constructor option (#143)
closes #142
1 parent c3afa6e commit 26d7719

File tree

13 files changed

+1124
-19
lines changed

13 files changed

+1124
-19
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ function (fieldOptionValue, userValue) {
248248
}
249249
```
250250

251+
</td>
252+
</tr>
253+
<tr>
254+
<th>
255+
<code>options.truncate</code>
256+
</th>
257+
<td>
258+
<code>Function</code>
259+
</td>
260+
<td>
261+
262+
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.
263+
251264
</td>
252265
</tr>
253266
</tbody>

api/lib/default-truncate-function.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Maximum text field value length is 1024 bytes. By default, we don't do anything.
3+
*
4+
* @param {string} text
5+
* @returns {string}
6+
*/
7+
export function defaultTruncateFunction(text) {
8+
return text;
9+
}

api/lib/get-fields-update-query-and-fields.js

+27-19
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ export function getFieldsUpdateQueryAndFields(state, fields) {
5757
userName: state.fields[id].userName,
5858
userValue,
5959
}))
60-
.filter(({ id, name }) => {
61-
const field = state.fields[id];
60+
.filter(({ name }) => {
6261
return READ_ONLY_FIELDS.some((readOnlyField) => {
6362
return state.matchFieldName(
6463
readOnlyField.toLowerCase(),
@@ -102,30 +101,31 @@ export function getFieldsUpdateQueryAndFields(state, fields) {
102101
key,
103102
value: null,
104103
};
105-
} else {
106-
const valueOrOption =
107-
"optionsByValue" in field
108-
? findFieldOptionIdAndValue(state, field, value)
109-
: value;
104+
}
110105

111-
const query = `
106+
const valueOrOption =
107+
"optionsByValue" in field
108+
? findFieldOptionIdAndValue(state, field, value)
109+
: value;
110+
111+
const query = `
112112
${alias}: updateProjectV2ItemFieldValue(input: {projectId: $projectId, itemId: $itemId, fieldId: "${fieldId}", ${toItemFieldValueInput(
113+
state,
113114
field,
114115
valueOrOption
115116
)}}) {
116117
${queryNodes}
117118
}
118119
`;
119120

120-
return {
121-
query,
122-
key,
123-
value:
124-
typeof valueOrOption === "string"
125-
? valueOrOption
126-
: valueOrOption.value,
127-
};
128-
}
121+
return {
122+
query,
123+
key,
124+
value:
125+
typeof valueOrOption === "string"
126+
? valueOrOption
127+
: valueOrOption.value,
128+
};
129129
})
130130
.filter(Boolean);
131131

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

147147
/**
148+
* @param {import("../..").GitHubProjectStateWithFields} state
148149
* @param {import("../..").ProjectField} field
149150
* @param {string | {id: string, value: string | undefined}} valueOrOption
150151
*
151152
* @returns {string}
152153
*/
153-
function toItemFieldValueInput(field, valueOrOption) {
154+
function toItemFieldValueInput(state, field, valueOrOption) {
155+
console.log("state.truncate");
156+
console.log(state.truncate.toString());
157+
154158
const value =
155-
typeof valueOrOption === "string" ? valueOrOption : valueOrOption.id;
159+
typeof valueOrOption === "string"
160+
? state.truncate(valueOrOption)
161+
: valueOrOption.id;
162+
163+
console.log({ value });
156164

157165
const valueKey =
158166
{

index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export type MatchFieldOptionValueFn = (
134134
fieldOptionValue: string,
135135
userValue: string,
136136
) => boolean;
137+
export type TruncateFn = (text: string) => string;
137138

138139
export type GitHubProjectOptions<
139140
TFields extends Record<string, FieldOptions> = {},
@@ -145,6 +146,7 @@ export type GitHubProjectOptions<
145146
fields?: TFields;
146147
matchFieldName?: MatchFieldNameFn;
147148
matchFieldOptionValue?: MatchFieldOptionValueFn;
149+
truncate?: TruncateFn;
148150
}
149151
| {
150152
owner: string;
@@ -153,6 +155,7 @@ export type GitHubProjectOptions<
153155
fields?: TFields;
154156
matchFieldName?: MatchFieldNameFn;
155157
matchFieldOptionValue?: MatchFieldOptionValueFn;
158+
truncate?: TruncateFn;
156159
};
157160

158161
export type GitHubProjectItem<
@@ -289,6 +292,7 @@ export type GitHubProjectState =
289292
type GitHubProjectStateCommon = {
290293
matchFieldName: MatchFieldNameFn;
291294
matchFieldOptionValue: MatchFieldOptionValueFn;
295+
truncate: TruncateFn;
292296
};
293297
type GitHubProjectStateInit = GitHubProjectStateCommon & {
294298
didLoadFields: false;

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { removeItemByContentRepositoryAndNumber } from "./api/items.remove-by-co
2020
import { getProperties } from "./api/project.getProperties.js";
2121

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

2425
/** @type {import("./").BUILT_IN_FIELDS} */
2526
export const BUILT_IN_FIELDS = {
@@ -48,6 +49,7 @@ export default class GitHubProject {
4849
matchFieldName: options.matchFieldName || defaultMatchFunction,
4950
matchFieldOptionValue:
5051
options.matchFieldOptionValue || defaultMatchFunction,
52+
truncate: options.truncate || defaultTruncateFunction,
5153
};
5254

5355
// set API

0 commit comments

Comments
 (0)