Skip to content

Commit

Permalink
Rename lib to src and format using Prettier (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoesel authored Nov 29, 2022
1 parent c828ec4 commit 59bf4ca
Show file tree
Hide file tree
Showing 14 changed files with 1,011 additions and 491 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"node": true,
"mocha": true
},
"extends": ["eslint:recommended"]
"extends": ["eslint:recommended", "prettier"]
}
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.vscode
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"trailingComma": "none",
"arrowParens": "avoid",
"endOfLine": "auto",
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true
}
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,24 @@ codeService.ensureValueSetsInLibrary(library, true, 'myUMLSUserName', 'myUMLSPas

# Linting the Code

To encourage quality and consistency within the code base, all code should pass eslint without any warnings. Many text editors can be configured to automatically flag eslint violations. We also provide an npm script for running eslint on the project. To run eslint, execute the following command:
To encourage quality and consistency within the code base, all code should pass eslint without any warnings. Many text editors can be configured to automatically flag eslint violations. We also provide an npm script for running eslint on the project. To check your code against eslint's rules, execute the following command:
```
$ npm run lint
```

To fix any code that violates eslint's rules:
```
$ npm run lint:fix
```

# Prettier

To encourage quality and consistency within the code base, all code should also be formatted using [Prettier](https://prettier.io/). Many text editors can be configured to automatically reformat code using Prettier on save. We also provide an npm script for running prettier on the project. To check your code against Prettier's rules, execute the following command:
```
$ npm run prettier
```

To fix any code that violates Prettier's rules:
```
$ npm run prettier:fix
```
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/CodeService.js');
module.exports = require('./src/CodeService.js');
20 changes: 15 additions & 5 deletions manual-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint-disable no-console */
const { CodeService } = require('./lib/CodeService');
const { CodeService } = require('./src/CodeService');
const proc = require('process');
const env = proc.env;

const VALUESET = { name: 'HDL Cholesterol', id: '2.16.840.1.113883.3.464.1003.104.12.1013' };
const VALUESET = {
name: 'HDL Cholesterol',
id: '2.16.840.1.113883.3.464.1003.104.12.1013'
};

async function main() {
if (env['UMLS_USER_NAME'] == null && env['UMLS_PASSWORD'] == null && env['UMLS_API_KEY'] == null) {
if (
env['UMLS_USER_NAME'] == null &&
env['UMLS_PASSWORD'] == null &&
env['UMLS_API_KEY'] == null
) {
console.error('This test requires you to set the UMLS_API_KEY environment variable');
process.exit(1);
}
Expand All @@ -17,7 +24,11 @@ async function main() {
let found = codeService.findValueSet(VALUESET.id);
console.log(`RESULT: ${found}`);
console.log();
console.log(`CALL: codeService.ensureValueSetsWithAPIKey(['${JSON.stringify(VALUESET)}'], env['UMLS_API_KEY'], false);`);
console.log(
`CALL: codeService.ensureValueSetsWithAPIKey(['${JSON.stringify(
VALUESET
)}'], env['UMLS_API_KEY'], false);`
);
await codeService.ensureValueSetsWithAPIKey([VALUESET], env['UMLS_API_KEY'], false);
console.log('EXPECT: <void>');
console.log(`RESULT: <void>`);
Expand All @@ -29,4 +40,3 @@ async function main() {
}

main().catch(e => console.error(e));

12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"test:watch": "npm test -- --watch",
"test:debug": "./node_modules/.bin/mocha --inspect --debug-brk --reporter spec --recursive",
"lint": "./node_modules/.bin/eslint .",
"lint:fix": "./node_modules/.bin/eslint . --fix"
"lint:fix": "./node_modules/.bin/eslint . --fix",
"prettier": "prettier --check \"**/*.js\"",
"prettier:fix": "prettier --write \"**/*.js\"",
"test:plus": "npm test && npm run lint && npm run prettier"
},
"dependencies": {
"debug": "^4.3.4",
Expand All @@ -25,9 +28,11 @@
"chai-as-promised": "^7.1.1",
"cql-execution": "^2.4.3",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"fs-extra": "^11.0.0",
"mocha": "^10.1.0",
"nock": "^13.2.9"
"nock": "^13.2.9",
"prettier": "^2.8.0"
},
"peerDependencies": {
"cql-execution": ">=1.3.0 || ^3.0.0-beta"
Expand Down
92 changes: 69 additions & 23 deletions lib/CodeService.js → src/CodeService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {Code, ValueSet} = require('cql-execution');
const { Code, ValueSet } = require('cql-execution');
const fs = require('fs');
const proc = require('process');
const env = proc.env;
const path = require('path');
const {downloadFromVSAC,downloadFromVSACWithAPIKey} = require('./download-vsac');
const { downloadFromVSAC, downloadFromVSACWithAPIKey } = require('./download-vsac');
const extractOidAndVersion = require('./extractOidAndVersion');

/**
Expand Down Expand Up @@ -44,14 +44,16 @@ class CodeService {
*/
loadValueSetsFromFile(filePath) {
filePath = path.resolve(filePath);
if (! fs.existsSync(filePath)) {
if (!fs.existsSync(filePath)) {
return;
}
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (let oid in json) {
let myOid = json[oid];
for (let version in myOid) {
let myCodes = myOid[version].codes.map(elem => new Code(elem.code, elem.system, elem.version));
let myCodes = myOid[version].codes.map(
elem => new Code(elem.code, elem.system, elem.version)
);
if (typeof this.valueSets[oid] === 'undefined') {
this.valueSets[oid] = {};
}
Expand All @@ -71,9 +73,16 @@ class CodeService {
* @returns {Promise.<undefined,Error>} A promise that returns nothing when
* resolved and returns an error when rejected.
*/
ensureValueSets(valueSetList = [], umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD'], caching = true) {
console.warn('WARNING! As of Jan 1 2021 VSAC will no longer accept accept username and password. As such ' +
'ensureValueSets() has been deprecated, please use the new ensureValueSetsWithAPIKey() function instead!');
ensureValueSets(
valueSetList = [],
umlsUserName = env['UMLS_USER_NAME'],
umlsPassword = env['UMLS_PASSWORD'],
caching = true
) {
console.warn(
'WARNING! As of Jan 1 2021 VSAC will no longer accept accept username and password. As such ' +
'ensureValueSets() has been deprecated, please use the new ensureValueSetsWithAPIKey() function instead!'
);
// First, filter out the value sets we already have
const filteredVSList = valueSetList.filter(vs => {
const result = this.findValueSet(vs.id, vs.version);
Expand All @@ -82,10 +91,24 @@ class CodeService {
// Now download from VSAC if necessary
if (filteredVSList.length == 0) {
return Promise.resolve();
} else if ( typeof umlsUserName === 'undefined' || umlsUserName == null ||typeof umlsPassword === 'undefined' || umlsPassword == null) {
return Promise.reject('Failed to download value sets since UMLS_USER_NAME and/or UMLS_PASSWORD is not set.');
} else if (
typeof umlsUserName === 'undefined' ||
umlsUserName == null ||
typeof umlsPassword === 'undefined' ||
umlsPassword == null
) {
return Promise.reject(
'Failed to download value sets since UMLS_USER_NAME and/or UMLS_PASSWORD is not set.'
);
} else {
return downloadFromVSAC(umlsUserName, umlsPassword, filteredVSList, this.cache, this.valueSets, caching);
return downloadFromVSAC(
umlsUserName,
umlsPassword,
filteredVSList,
this.cache,
this.valueSets,
caching
);
}
}
/**
Expand All @@ -106,10 +129,16 @@ class CodeService {
// Now download from VSAC if necessary
if (filteredVSList.length == 0) {
return Promise.resolve();
} else if ( typeof umlsAPIKey === 'undefined' || umlsAPIKey == null) {
} else if (typeof umlsAPIKey === 'undefined' || umlsAPIKey == null) {
return Promise.reject('Failed to download value sets since UMLS_API_KEY is not set.');
} else {
return downloadFromVSACWithAPIKey(umlsAPIKey,filteredVSList, this.cache, this.valueSets, caching);
return downloadFromVSACWithAPIKey(
umlsAPIKey,
filteredVSList,
this.cache,
this.valueSets,
caching
);
}
}

Expand All @@ -124,9 +153,17 @@ class CodeService {
* @param {string} umlsPassword - the UMLS password to use when downloading value sets (defaults to env "UMLS_PASSWORD")
* @returns {Promise.<undefined,Error>} A promise that returns nothing when resolved and returns an error when rejected.
*/
ensureValueSetsInLibrary(library, checkIncluded = true, umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD'], caching = true) {
console.warn('WARNING! As of Jan 1 2021 VSAC will no longer accept accept username and password. As such ' +
'ensureValueSetsInLibrary() has been deprecated, please use the new ensureValueSetsInLibraryWithAPIKey() function instead!');
ensureValueSetsInLibrary(
library,
checkIncluded = true,
umlsUserName = env['UMLS_USER_NAME'],
umlsPassword = env['UMLS_PASSWORD'],
caching = true
) {
console.warn(
'WARNING! As of Jan 1 2021 VSAC will no longer accept accept username and password. As such ' +
'ensureValueSetsInLibrary() has been deprecated, please use the new ensureValueSetsInLibraryWithAPIKey() function instead!'
);
const valueSets = extractSetOfValueSetsFromLibrary(library, checkIncluded);
return this.ensureValueSets(Array.from(valueSets), umlsUserName, umlsPassword, caching);
}
Expand All @@ -139,12 +176,16 @@ class CodeService {
* @param {string} umlsAPIKey - the UMLS API Key to use when downloading value sets (defaults to env "UMLS_USER_NAME")
* @returns {Promise.<undefined,Error>} A promise that returns nothing when resolved and returns an error when rejected.
*/
ensureValueSetsInLibraryWithAPIKey(library, checkIncluded = true, umlsAPIKey = env['UMLS_API_KEY'], caching = true) {
ensureValueSetsInLibraryWithAPIKey(
library,
checkIncluded = true,
umlsAPIKey = env['UMLS_API_KEY'],
caching = true
) {
const valueSets = extractSetOfValueSetsFromLibrary(library, checkIncluded);
return this.ensureValueSetsWithAPIKey(Array.from(valueSets), umlsAPIKey, caching);
}


/**
* The findValueSetsByOid function is kept for backwards compatibility (and since cql-execution
* calls it), but now it just calls the more appropriately named findValuesets.
Expand Down Expand Up @@ -196,11 +237,10 @@ class CodeService {
} else if (results.length === 1) {
return results[0];
} else {
return results.reduce(function(a, b) {
return results.reduce(function (a, b) {
if (a.version > b.version) {
return a;
}
else {
} else {
return b;
}
});
Expand All @@ -215,14 +255,20 @@ class CodeService {
* @param {Set} valueSets - the Set of valueSets extracted so far (defaults to empty set)
* @returns {Set} the set of value sets referenced by the library
*/
function extractSetOfValueSetsFromLibrary(library, extractFromIncluded = true, valueSets = new Set()) {
function extractSetOfValueSetsFromLibrary(
library,
extractFromIncluded = true,
valueSets = new Set()
) {
// First add all the value sets from this library into the set
Object.values(library.valuesets).forEach(vs => valueSets.add(vs));
// Then, if requested, loop through the included libraries and add value sets from each of them
if (extractFromIncluded && library.includes) {
Object.values(library.includes).forEach(included => extractSetOfValueSetsFromLibrary(included, extractFromIncluded, valueSets));
Object.values(library.includes).forEach(included =>
extractSetOfValueSetsFromLibrary(included, extractFromIncluded, valueSets)
);
}
return valueSets;
}

module.exports = { CodeService };
module.exports = { CodeService };
Loading

0 comments on commit 59bf4ca

Please sign in to comment.