Skip to content

Commit

Permalink
fix json reads
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Mar 5, 2024
1 parent ffab9db commit fd0b2b1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable security/detect-non-literal-fs-filename, security/detect-object-injection */
import fs from 'node:fs/promises';
import path from 'node:path';
let codeTypes = {};
let codeTypeFieldValues = {};
const currentFolder = path.dirname(import.meta.filename ?? '.');
/**
* Returns an object of code types.
* @returns {Promise<Record<string, string>>} - An object with "code type" keys and "code type description" values.
*/
export async function getCodeTypes() {
if (Object.keys(codeTypes).length === 0) {
const codeTypesData = await fs.readFile(path.join(currentFolder, 'data', 'codeTypes.json'));
const codeTypesData = await fs.readFile(new URL('data/codeTypes.json', import.meta.url));
codeTypes = JSON.parse(codeTypesData);
}
return codeTypes;
Expand Down Expand Up @@ -42,7 +40,7 @@ export async function getCodeTypeDescription(codeType) {
export async function getFieldValues(codeType) {
if ((await isCodeType(codeType)) &&
!Object.hasOwn(codeTypeFieldValues, codeType)) {
const fieldValueData = await fs.readFile(path.join(currentFolder, 'data', `${codeType}.json`));
const fieldValueData = await fs.readFile(new URL(`data/${codeType}.json`, import.meta.url));
codeTypeFieldValues[codeType] = JSON.parse(fieldValueData);
}
return codeTypeFieldValues[codeType] ?? {};
Expand Down
11 changes: 6 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
/* eslint-disable security/detect-non-literal-fs-filename, security/detect-object-injection */

import fs from 'node:fs/promises'
import path from 'node:path'

let codeTypes: Record<string, string> = {}
let codeTypeFieldValues: Record<string, Record<string, string>> = {}

const currentFolder = path.dirname(import.meta.filename ?? '.')

/**
* Returns an object of code types.
* @returns {Promise<Record<string, string>>} - An object with "code type" keys and "code type description" values.
*/
export async function getCodeTypes(): Promise<Record<string, string>> {
if (Object.keys(codeTypes).length === 0) {
const codeTypesData = await fs.readFile(path.join(currentFolder, 'data', 'codeTypes.json'))
const codeTypesData = await fs.readFile(
new URL('data/codeTypes.json', import.meta.url)
)
codeTypes = JSON.parse(codeTypesData as unknown as string)
}

Expand Down Expand Up @@ -56,7 +55,9 @@ export async function getFieldValues(
(await isCodeType(codeType)) &&
!Object.hasOwn(codeTypeFieldValues, codeType)
) {
const fieldValueData = await fs.readFile(path.join(currentFolder, 'data', `${codeType}.json`))
const fieldValueData = await fs.readFile(
new URL(`data/${codeType}.json`, import.meta.url)
)

codeTypeFieldValues[codeType] = JSON.parse(
fieldValueData as unknown as string
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cityssm/ncic-lookup",
"version": "0.1.1",
"version": "0.1.2",
"type": "module",
"engines": {
"node": ">=18.0.0"
Expand Down

0 comments on commit fd0b2b1

Please sign in to comment.