Skip to content

Commit 58b63ca

Browse files
authored
feat: use indefinite to find the correct article (#26)
[indefinite](https://www.npmjs.com/package/indefinite) is able to find the correct indefinite article (`a` or `an`) for a given word. It contains a list of irregular words for exceptions BREAKING CHANGE: mock names for irregular words might change. Example: - before: `aUpload` - after: `anUpload`
1 parent eb7c2a8 commit 58b63ca

File tree

4 files changed

+112
-74
lines changed

4 files changed

+112
-74
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
"dependencies": {
2525
"@graphql-codegen/plugin-helpers": "^1.13.1",
2626
"casual": "^1.6.2",
27+
"indefinite": "^2.3.2",
2728
"pascal-case": "^3.1.1",
29+
"sentence-case": "^3.0.3",
2830
"upper-case": "^2.0.1"
2931
},
3032
"peerDependencies": {

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { printSchema, parse, visit, ASTKindToNode, NamedTypeNode, TypeNode, VisitFn } from 'graphql';
1+
import { ASTKindToNode, NamedTypeNode, parse, printSchema, TypeNode, visit, VisitFn } from 'graphql';
22
import casual from 'casual';
33
import { PluginFunction } from '@graphql-codegen/plugin-helpers';
44
import { pascalCase } from 'pascal-case';
55
import { upperCase } from 'upper-case';
6+
import { sentenceCase } from 'sentence-case';
7+
import a from 'indefinite';
68

79
type NamingConvention = 'upper-case#upperCase' | 'pascal-case#pascalCase' | 'keep';
810

@@ -20,12 +22,12 @@ const createNameConverter = (convention: NamingConvention) => (value: string) =>
2022
}
2123
};
2224

23-
const toMockName = (name: string, prefix?: string) => {
25+
const toMockName = (typedName: string, casedName: string, prefix?: string) => {
2426
if (prefix) {
25-
return `${prefix}${name}`;
27+
return `${prefix}${casedName}`;
2628
}
27-
const isVowel = name.match(/^[AEIO]/);
28-
return isVowel ? `an${name}` : `a${name}`;
29+
const firstWord = sentenceCase(typedName).split(' ')[0];
30+
return `${a(firstWord, { articleOnly: true })}${casedName}`;
2931
};
3032

3133
const updateTextCase = (str: string, enumValuesConvention: NamingConvention) => {
@@ -130,7 +132,7 @@ const getNamedType = (
130132
throw `foundType is unknown: ${foundType.name}: ${foundType.type}`;
131133
}
132134
}
133-
return `${toMockName(name, prefix)}()`;
135+
return `${toMockName(name, name, prefix)}()`;
134136
}
135137
}
136138
};
@@ -194,7 +196,7 @@ const getMockString = (
194196
const casedName = createNameConverter(typenamesConvention)(typeName);
195197
const typename = addTypename ? `\n __typename: '${casedName}',` : '';
196198
return `
197-
export const ${toMockName(casedName, prefix)} = (overrides?: Partial<${casedName}>): ${casedName} => {
199+
export const ${toMockName(typeName, casedName, prefix)} = (overrides?: Partial<${casedName}>): ${casedName} => {
198200
return {${typename}
199201
${fields}
200202
};

tests/__snapshots__/typescript-mock-data.spec.ts.snap

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
5252
};
5353
};
5454
55-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
55+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
5656
return {
5757
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
5858
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -89,7 +89,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
8989
};
9090
};
9191
92-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
92+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
9393
return {
9494
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
9595
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -126,7 +126,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
126126
};
127127
};
128128
129-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
129+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
130130
return {
131131
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
132132
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -165,7 +165,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
165165
};
166166
};
167167
168-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
168+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
169169
return {
170170
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
171171
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -202,7 +202,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
202202
};
203203
};
204204
205-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
205+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
206206
return {
207207
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
208208
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -239,7 +239,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
239239
};
240240
};
241241
242-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
242+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
243243
return {
244244
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
245245
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -276,7 +276,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
276276
};
277277
};
278278
279-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
279+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
280280
return {
281281
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
282282
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -313,7 +313,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
313313
};
314314
};
315315
316-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
316+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
317317
return {
318318
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
319319
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -350,7 +350,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
350350
};
351351
};
352352
353-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
353+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
354354
return {
355355
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
356356
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -387,7 +387,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
387387
};
388388
};
389389
390-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
390+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
391391
return {
392392
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
393393
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -424,7 +424,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
424424
};
425425
};
426426
427-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
427+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
428428
return {
429429
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
430430
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -463,7 +463,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
463463
};
464464
};
465465
466-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
466+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
467467
return {
468468
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
469469
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -501,7 +501,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
501501
};
502502
};
503503
504-
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
504+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
505505
return {
506506
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
507507
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
@@ -538,7 +538,7 @@ export const anAVATAR = (overrides?: Partial<AVATAR>): AVATAR => {
538538
};
539539
};
540540
541-
export const aUPDATEUSERINPUT = (overrides?: Partial<UPDATEUSERINPUT>): UPDATEUSERINPUT => {
541+
export const anUPDATEUSERINPUT = (overrides?: Partial<UPDATEUSERINPUT>): UPDATEUSERINPUT => {
542542
return {
543543
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
544544
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',

0 commit comments

Comments
 (0)