diff --git a/package.json b/package.json index dd12376..63ec61c 100755 --- a/package.json +++ b/package.json @@ -27,10 +27,10 @@ "check-types": "tsc --noEmit", "clean": "rimraf lib lib-esm", "eslint": "eslint src --cache", - "eslint:fix": "npm run eslint -- --fix", + "eslint-fix": "npm run eslint -- --fix", "prepack": "npm run tsc", "prettier": "prettier --check src", - "prettier:fix": "prettier --write src", + "prettier-write": "prettier --write src", "test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types", "test-only": "jest --coverage", "tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm", @@ -38,18 +38,16 @@ "tsc-esm": "tsc --project tsconfig.esm.json" }, "devDependencies": { - "@types/jest": "^29.2.3", - "cheminfo-build": "^1.1.11", - "cheminfo-types": "^1.4.0", - "eslint": "^8.25.0", - "eslint-config-cheminfo-typescript": "^11.2.2", - "eslint-plugin-import": "^2.28.0", - "jest": "^29.3.1", - "prettier": "^2.7.1", - "ts-jest": "^29.0.3", - "typescript": "^4.9.3" + "@types/jest": "^29.5.3", + "cheminfo-types": "^1.7.2", + "eslint": "^8.46.0", + "eslint-config-cheminfo-typescript": "^12.0.4", + "jest": "^29.6.2", + "prettier": "^3.0.1", + "ts-jest": "^29.1.1", + "typescript": "^5.1.6" }, "dependencies": { - "iobuffer": "^5.2.1" + "iobuffer": "^5.3.2" } } diff --git a/src/__tests__/attributeExists.test.ts b/src/__tests__/attributeExists.test.ts index 78ed5f7..1084cfa 100755 --- a/src/__tests__/attributeExists.test.ts +++ b/src/__tests__/attributeExists.test.ts @@ -7,7 +7,7 @@ const pathFiles = `${__dirname}/files/`; test('attributeExists', () => { const data = readFileSync(`${pathFiles}P071.CDF`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.attributeExists('operator_name')).toBe(true); expect(reader.attributeExists('operator_nameXX')).toBe(false); }); diff --git a/src/__tests__/dataVariableExists.test.ts b/src/__tests__/dataVariableExists.test.ts index 7ccac0d..985e5d1 100755 --- a/src/__tests__/dataVariableExists.test.ts +++ b/src/__tests__/dataVariableExists.test.ts @@ -7,7 +7,7 @@ const pathFiles = `${__dirname}/files/`; test('dataVariableExists', () => { const data = readFileSync(`${pathFiles}P071.CDF`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.dataVariableExists('instrument_name')).toBe(true); expect(reader.dataVariableExists('instrument_nameXX')).toBe(false); }); diff --git a/src/__tests__/getAttribute.test.ts b/src/__tests__/getAttribute.test.ts index a690da9..751a6ea 100755 --- a/src/__tests__/getAttribute.test.ts +++ b/src/__tests__/getAttribute.test.ts @@ -7,6 +7,6 @@ const pathFiles = `${__dirname}/files/`; test('getAttribute', () => { const data = readFileSync(`${pathFiles}P071.CDF`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.getAttribute('operator_name')).toBe('SC'); }); diff --git a/src/__tests__/getDataVariableAsString.test.ts b/src/__tests__/getDataVariableAsString.test.ts index b37067f..0cf7520 100755 --- a/src/__tests__/getDataVariableAsString.test.ts +++ b/src/__tests__/getDataVariableAsString.test.ts @@ -7,7 +7,7 @@ const pathFiles = `${__dirname}/files/`; test('getDataVariableAsString', () => { const data = readFileSync(`${pathFiles}P071.CDF`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.getDataVariableAsString('instrument_name')).toBe( 'Gas Chromatograph', ); diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 86bf0b0..9199f4e 100755 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -17,7 +17,7 @@ describe('Read file', () => { // http://www.unidata.ucar.edu/software/netcdf/examples/madis-sao.cdl const data = readFileSync(`${pathFiles}madis-sao.nc`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.version).toBe('classic format'); expect(reader.recordDimension).toStrictEqual({ length: 178, @@ -94,14 +94,14 @@ describe('Read file', () => { it('read non-record variable', () => { const data = readFileSync(`${pathFiles}madis-sao.nc`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.getDataVariable('nStaticIds')[0]).toBe(145); }); it('read 2 dimensional variable', () => { const data = readFileSync(`${pathFiles}ichthyop.nc`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.getDataVariable('time')).toHaveLength(49); expect(reader.getDataVariable('time')[0]).toBe(1547070300); expect(reader.getDataVariable('lat')).toHaveLength(49); @@ -112,9 +112,9 @@ describe('Read file', () => { it('read record variable with string', () => { const data = readFileSync(`${pathFiles}madis-sao.nc`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); - let record = reader.getDataVariable('wmoId'); + const record = reader.getDataVariable('wmoId'); expect(record[0]).toBe(71419); expect(record[1]).toBe(71415); expect(record[2]).toBe(71408); @@ -122,11 +122,11 @@ describe('Read file', () => { it('read non-record variable with object', () => { const data = readFileSync(`${pathFiles}madis-sao.nc`); - let reader = new NetCDFReader(data); - let variables = reader.variables; + const reader = new NetCDFReader(data); + const variables = reader.variables; - let withString = reader.getDataVariable('staticIds'); - let withObject = reader.getDataVariable(variables[1]); + const withString = reader.getDataVariable('staticIds'); + const withObject = reader.getDataVariable(variables[1]); expect(withString[0]).toBe('W'); expect(withString[1]).toBe('A'); expect(withString[2]).toBe('F'); @@ -137,7 +137,7 @@ describe('Read file', () => { it('read non-existent variable string', () => { const data = readFileSync(`${pathFiles}madis-sao.nc`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.getDataVariable.bind(reader, "n'importe quoi")).toThrow( 'Not a valid NetCDF v3.x file: variable not found', @@ -146,7 +146,7 @@ describe('Read file', () => { it('read 64 bit offset file', () => { const data = readFileSync(`${pathFiles}model1_md2.nc`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.version).toBe('64-bit offset format'); expect(reader.getDataVariable('cell_angular')[0]).toBe('a'); expect(reader.getDataVariable('cell_spatial')[0]).toBe('a'); @@ -154,13 +154,13 @@ describe('Read file', () => { it('read agilent hplc file file', () => { const data = readFileSync(`${pathFiles}agilent_hplc.cdf`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.version).toBe('classic format'); - let variables = []; + const variables = []; - for (let variable of reader.variables) { + for (const variable of reader.variables) { const value = reader.getDataVariable(variable); variables.push({ value, ...variable }); } diff --git a/src/__tests__/toString.test.ts b/src/__tests__/toString.test.ts index 96ffc46..aa2f99a 100755 --- a/src/__tests__/toString.test.ts +++ b/src/__tests__/toString.test.ts @@ -7,6 +7,6 @@ const pathFiles = `${__dirname}/files/`; test('toString', () => { const data = readFileSync(`${pathFiles}P071.CDF`); - let reader = new NetCDFReader(data); + const reader = new NetCDFReader(data); expect(reader.toString()).toMatchSnapshot(); }); diff --git a/src/data.ts b/src/data.ts index e394164..37bd206 100644 --- a/src/data.ts +++ b/src/data.ts @@ -13,7 +13,7 @@ import { num2bytes, str2num, readType } from './types'; export function nonRecord( buffer: IOBuffer, variable: Header['variables'][number], -): ReturnType[] { +): Array> { // variable type const type = str2num(variable.type); @@ -21,7 +21,7 @@ export function nonRecord( const size = variable.size / num2bytes(type); // iterates over the data - let data = new Array(size); + const data = new Array(size); for (let i = 0; i < size; i++) { data[i] = readType(buffer, type, 1); } @@ -40,7 +40,7 @@ export function record( buffer: IOBuffer, variable: Header['variables'][number], recordDimension: Header['recordDimension'], -): ReturnType[] { +): Array> { // variable type const type = str2num(variable.type); const width = variable.size ? variable.size / num2bytes(type) : 1; @@ -50,11 +50,11 @@ export function record( const size = recordDimension.length; // iterates over the data - let data = new Array(size); + const data = new Array(size); const step = recordDimension.recordStep; if (step) { for (let i = 0; i < size; i++) { - let currentOffset = buffer.offset; + const currentOffset = buffer.offset; data[i] = readType(buffer, type, width); buffer.seek(currentOffset + step); } diff --git a/src/header.ts b/src/header.ts index 0687939..ce1297a 100644 --- a/src/header.ts +++ b/src/header.ts @@ -66,12 +66,12 @@ export function header(buffer: IOBuffer, version: number): Header { export interface Dimensions { /* that is an array of dimension object:*/ - dimensions: { + dimensions: Array<{ /* name of the dimension*/ name: string; /* size of the dimension */ size: number; - }[]; + }>; /* id of the dimension that has unlimited size or undefined,*/ recordId?: number; /* name of the dimension that has unlimited size */ @@ -203,7 +203,10 @@ export interface Variable { /* True if is a record variable, false otherwise (unlimited size) */ record: boolean; } -type Variables = { variables: Variable[]; recordStep: number }; +interface Variables { + variables: Variable[]; + recordStep: number; +} /** * @param buffer - Buffer for the file data * @param recordId - Id of the unlimited dimension (also called record dimension) @@ -233,22 +236,22 @@ function variablesList( variables = new Array(variableSize); for (let v = 0; v < variableSize; v++) { // Read name - let name = readName(buffer); + const name = readName(buffer); // Read dimensionality of the variable const dimensionality = buffer.readUint32(); // Index into the list of dimensions - let dimensionsIds = new Array(dimensionality); + const dimensionsIds = new Array(dimensionality); for (let dim = 0; dim < dimensionality; dim++) { dimensionsIds[dim] = buffer.readUint32(); } // Read variables size - let attributes = attributesList(buffer); + const attributes = attributesList(buffer); // Read type - let type = buffer.readUint32(); + const type = buffer.readUint32(); notNetcdf(type < 1 && type > 6, `non valid type ${type}`); // Read variable size diff --git a/src/toString.ts b/src/toString.ts index 6e72946..ecb5569 100644 --- a/src/toString.ts +++ b/src/toString.ts @@ -1,22 +1,22 @@ import { NetCDFReader } from './parser'; export function toString(this: NetCDFReader) { - let result = []; + const result = []; result.push('DIMENSIONS'); - for (let dimension of this.dimensions) { + for (const dimension of this.dimensions) { result.push(` ${dimension.name.padEnd(30)} = size: ${dimension.size}`); } result.push(''); result.push('GLOBAL ATTRIBUTES'); - for (let attribute of this.globalAttributes) { + for (const attribute of this.globalAttributes) { result.push(` ${attribute.name.padEnd(30)} = ${attribute.value}`); } - let variables = JSON.parse(JSON.stringify(this.variables)); + const variables = JSON.parse(JSON.stringify(this.variables)); result.push(''); result.push('VARIABLES:'); - for (let variable of variables) { + for (const variable of variables) { variable.value = this.getDataVariable(variable); let stringify = JSON.stringify(variable.value); if (stringify.length > 50) stringify = stringify.substring(0, 50); diff --git a/src/types.ts b/src/types.ts index 5833dd1..e490c62 100644 --- a/src/types.ts +++ b/src/types.ts @@ -93,7 +93,7 @@ function readNumber( bufferReader: () => number, ): number | number[] { if (size !== 1) { - let numbers = new Array(size); + const numbers = new Array(size); for (let i = 0; i < size; i++) { numbers[i] = bufferReader(); }