Skip to content

Commit 7852f59

Browse files
author
Sophia Marie Terry
committed
CLOUDP-328283: Prettier
1 parent a902bff commit 7852f59

File tree

2 files changed

+71
-64
lines changed

2 files changed

+71
-64
lines changed

tools/spectral/ipa/__tests__/utils/operationIdGeneration.test.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@ import { describe, expect, it } from '@jest/globals';
22
import { generateOperationID } from '../../rulesets/functions/utils/operationIdGeneration';
33

44
describe('tools/spectral/ipa/utils/operationIdGeneration.js', () => {
5-
it('should singularize all nouns', () => {
6-
expect(generateOperationID("create", "/groups/{groupId}/clusters")).toEqual("createGroupCluster");
7-
expect(generateOperationID("delete", "/groups/{groupId}/clusters/{clusterName}")).toEqual("deleteGroupCluster");
8-
expect(generateOperationID("get", "/groups/{groupId}/clusters/{clusterName}")).toEqual("getGroupCluster");
9-
expect(generateOperationID("update", "/groups/{groupId}/clusters/{clusterName}")).toEqual("updateGroupCluster");
10-
expect(generateOperationID("pause", "/groups/{groupId}/clusters/{clusterName}")).toEqual("pauseGroupCluster");
11-
})
5+
it('should singularize all nouns', () => {
6+
expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster');
7+
expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster');
8+
expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster');
9+
expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster');
10+
expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster');
11+
});
1212

13-
it('should leave the final noun as is', () => {
14-
expect(generateOperationID("list", "/groups/{groupId}/clusters")).toEqual("listGroupClusters");
15-
expect(generateOperationID("get", "/groups/{groupId}/settings")).toEqual("getGroupSettings");
16-
expect(generateOperationID("update", "/groups/{groupId}/settings")).toEqual("updateGroupSettings");
17-
expect(generateOperationID("search", "/groups/{groupId}/clusters")).toEqual("searchGroupClusters");
18-
expect(generateOperationID("get", "/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements")).toEqual("getGroupClusterCollStatMeasurements");
19-
expect(generateOperationID("grant", "/api/atlas/v2/groups/{groupId}/access")).toEqual("grantGroupAccess");
20-
})
13+
it('should leave the final noun as is', () => {
14+
expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters');
15+
expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings');
16+
expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings');
17+
expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters');
18+
expect(
19+
generateOperationID(
20+
'get',
21+
'/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements'
22+
)
23+
).toEqual('getGroupClusterCollStatMeasurements');
24+
expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess');
25+
});
2126

22-
it('should split camelCase method names', () => {
23-
expect(generateOperationID("addNode", "/groups/{groupId}/clusters/{clusterName}")).toEqual("addGroupClusterNode");
24-
})
27+
it('should split camelCase method names', () => {
28+
expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode');
29+
});
2530

26-
it('should accomodate legacy custom methods', () => {
27-
expect(generateOperationID("", "/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries")).toEqual("restartGroupClusterPrimaries");
28-
})
29-
})
31+
it('should accomodate legacy custom methods', () => {
32+
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual(
33+
'restartGroupClusterPrimaries'
34+
);
35+
});
36+
});
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
import { singularize } from "ember-inflector";
1+
import { singularize } from 'ember-inflector';
22
import { isPathParam, removePrefix, isSingleResourceIdentifier } from './resourceEvaluation.js';
33
import { casing } from '@stoplight/spectral-functions';
44

55
/**
66
* Returns IPA Compliant Operation ID.
7-
*
7+
*
88
* @param method the standard method name (create, update, get etc.), custom method name, or empty string (only for legacy custom methods)
99
* @param path the path for the endpoint
1010
*/
1111
export function generateOperationID(method, path) {
12-
let resourceIdentifier = removePrefix(path);
13-
let nouns = resourceIdentifier
14-
.split("/")
15-
.filter(id => id.length > 0 && !isPathParam(id))
16-
.map(noun => capitalize(noun));
17-
18-
// legacy custom method - use end of path as custom method name
19-
if (method === "") {
20-
method = path.split("/").pop();
21-
nouns.pop();
22-
}
23-
24-
let verb = deriveActionVerb(method);
25-
26-
// if custom method name is multiple words, add trailing nouns to the operation ID
27-
if (!casing(method, { type: 'camel'}) && method.length > verb.length) {
28-
nouns.push(method.slice(verb.length));
29-
}
30-
31-
let opID = verb;
32-
for(let i = 0; i < nouns.length - 1; i++) {
33-
opID += singularize(nouns[i]);
34-
}
35-
36-
// singularize final noun, dependent on resource identifier
37-
if (isSingleResourceIdentifier(resourceIdentifier) || verb == "create") {
38-
nouns[nouns.length - 1] = singularize(nouns[nouns.length - 1]);
39-
}
40-
41-
opID += nouns.pop();
42-
43-
return opID;
12+
let resourceIdentifier = removePrefix(path);
13+
let nouns = resourceIdentifier
14+
.split('/')
15+
.filter((id) => id.length > 0 && !isPathParam(id))
16+
.map((noun) => capitalize(noun));
17+
18+
// legacy custom method - use end of path as custom method name
19+
if (method === '') {
20+
method = path.split('/').pop();
21+
nouns.pop();
22+
}
23+
24+
let verb = deriveActionVerb(method);
25+
26+
// if custom method name is multiple words, add trailing nouns to the operation ID
27+
if (!casing(method, { type: 'camel' }) && method.length > verb.length) {
28+
nouns.push(method.slice(verb.length));
29+
}
30+
31+
let opID = verb;
32+
for (let i = 0; i < nouns.length - 1; i++) {
33+
opID += singularize(nouns[i]);
34+
}
35+
36+
// singularize final noun, dependent on resource identifier
37+
if (isSingleResourceIdentifier(resourceIdentifier) || verb == 'create') {
38+
nouns[nouns.length - 1] = singularize(nouns[nouns.length - 1]);
39+
}
40+
41+
opID += nouns.pop();
42+
43+
return opID;
4444
}
4545

4646
/**
4747
* Derives action verb from custom method name. Returns standard method names as is.
48-
*
48+
*
4949
* @param method the custom method name
5050
*/
5151
function deriveActionVerb(method) {
52-
// custom method name is camelCase return first word (assumed verb)
53-
if (!casing(method, { type: 'camel'})) {
54-
return method.match(/[A-Z]?[a-z]+/g)[0];
55-
}
52+
// custom method name is camelCase return first word (assumed verb)
53+
if (!casing(method, { type: 'camel' })) {
54+
return method.match(/[A-Z]?[a-z]+/g)[0];
55+
}
5656

57-
return method;
57+
return method;
5858
}
5959

6060
function capitalize(val) {
61-
return String(val).charAt(0).toUpperCase() + String(val).slice(1);
62-
}
61+
return String(val).charAt(0).toUpperCase() + String(val).slice(1);
62+
}

0 commit comments

Comments
 (0)