diff --git a/.editorconfig b/.editorconfig index 3aca45d..b261b37 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,4 +2,5 @@ indent_style = space indent_size = 2 trim_trailing_whitespace = true -insert_final_newline = true \ No newline at end of file +insert_final_newline = true +end_of_line = lf \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json index 33ef820..2bfbaaf 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,9 +4,18 @@ }, "extends": [ "airbnb-base", - "prettier" + "plugin:prettier/recommended" ], "overrides": [ + { + "files": [ + "**/*.test.js" + ], + "env": { + "node": true, + "jest": true + } + }, { "files": [ "**/*.ts", @@ -20,7 +29,7 @@ "airbnb-typescript/base", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended", - "prettier" + "plugin:prettier/recommended" ], "globals": { "Atomics": "readonly", diff --git a/build.js b/build.js index 09ed1f6..feb71d3 100644 --- a/build.js +++ b/build.js @@ -1,9 +1,9 @@ -const fs = require("fs"); +const fs = require('fs'); -if (fs.existsSync("dist")) { - fs.rmdirSync("dist", { recursive: true }); +if (fs.existsSync('dist')) { + fs.rmdirSync('dist', { recursive: true }); } -fs.mkdirSync("dist"); -fs.copyFileSync("lib/torch.js", "dist/torch.js"); -fs.copyFileSync("lib/torch.d.ts", "dist/torch.d.ts"); -require("typescript/bin/tsc"); +fs.mkdirSync('dist'); +fs.copyFileSync('lib/torch.js', 'dist/torch.js'); +fs.copyFileSync('lib/torch.d.ts', 'dist/torch.d.ts'); +require('typescript/bin/tsc'); diff --git a/lib/index.ts b/lib/index.ts index eca29ef..717f423 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,7 +1,7 @@ -import assert = require("assert"); -import torch = require("./torch"); +import assert = require('assert'); +import torch = require('./torch'); -export * from "./torch"; +export * from './torch'; // eslint-disable-next-line no-use-before-define type Matrix = number[] | NestedArray; @@ -33,7 +33,7 @@ function typedArrayType(dtype: number) { case torch.int32: return Int32Array; default: - throw new TypeError("Unsupported dtype"); + throw new TypeError('Unsupported dtype'); } } diff --git a/lib/torch.js b/lib/torch.js index 873aded..e5f9e11 100644 --- a/lib/torch.js +++ b/lib/torch.js @@ -1,10 +1,10 @@ -const torch = require("bindings")("torch-js"); +const torch = require('bindings')('torch-js'); -const bindings = require("bindings"); -const path = require("path"); +const bindings = require('bindings'); +const path = require('path'); const moduleRoot = bindings.getRoot(bindings.getFileName()); -const buildFolder = path.join(moduleRoot, "build", "Release"); +const buildFolder = path.join(moduleRoot, 'build', 'Release'); torch.initenv(`${buildFolder};${process.env.path}`); module.exports = torch; diff --git a/package.json b/package.json index f4c49c5..275f2b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@arition/torch-js", - "version": "0.12.3", + "version": "0.13.0", "main": "dist/index.js", "types": "dist/index.d.ts", "author": "Kittipat Virochsiri, arition, raghavmecheri", @@ -36,6 +36,7 @@ "eslint-config-airbnb-typescript": "^17.0.0", "eslint-config-prettier": "^8.5.0", "eslint-plugin-import": "^2.26.0", + "eslint-plugin-prettier": "^4.0.0", "husky": "^7.0.4", "jest": "^27.5.1", "lint-staged": "^12.4.0", @@ -53,4 +54,4 @@ 3 ] } -} \ No newline at end of file +} diff --git a/tests/rand.test.js b/tests/rand.test.js index 10c5b89..4bdc74f 100644 --- a/tests/rand.test.js +++ b/tests/rand.test.js @@ -3,12 +3,10 @@ * TODO: Check why a.shape is of type object, instead of type Array * TODO: Implement toBeInstanceOf checks for data children of objects */ -import { describe, expect, test } from "@jest/globals"; +const torch = require('../dist'); -const torch = require("../dist"); - -describe("Random tensor creation", () => { - test("Random tensor creation using variable number of arguements", () => { +describe('Random tensor creation', () => { + test('Random tensor creation using variable number of arguements', () => { const a = torch.rand(1, 5).toObject(); expect(a.data.length).toBe(5); expect(a.shape).toMatchObject([1, 5]); @@ -22,7 +20,7 @@ describe("Random tensor creation", () => { expect(c.shape).toMatchObject([2, 3]); }); - test("Random tensor creation using shape array", () => { + test('Random tensor creation using shape array', () => { const a = torch.rand([1, 5]).toObject(); expect(a.data.length).toBe(5); expect(a.shape).toMatchObject([1, 5]); @@ -36,7 +34,7 @@ describe("Random tensor creation", () => { expect(c.shape).toMatchObject([2, 3]); }); - test("Random tensor creation using option parsing", () => { + test('Random tensor creation using option parsing', () => { const a = torch .rand([1, 5], { dtype: torch.float64, @@ -46,22 +44,22 @@ describe("Random tensor creation", () => { expect(a.shape).toMatchObject([1, 5]); }); - test("Random tensor creation using single param", () => { + test('Random tensor creation using single param', () => { const a = torch.rand(1).toObject(); expect(a.data.length).toBe(1); expect(a.shape.length).toBe(1); }); - test("Random tensor creation using no params", () => { + test('Random tensor creation using no params', () => { const a = torch.rand().toObject(); expect(a.data.length).toBe(1); expect(a.shape.length).toBe(0); }); - test("Random tensor creation using invalid params", () => { - const t = () => torch.rand("Hello World"); + test('Random tensor creation using invalid params', () => { + const t = () => torch.rand('Hello World'); const t2 = () => torch.rand(true); - expect(t).toThrow(new Error("A number was expected")); - expect(t2).toThrow(new Error("A number was expected")); + expect(t).toThrow(new Error('A number was expected')); + expect(t2).toThrow(new Error('A number was expected')); }); }); diff --git a/tests/scriptmodule.test.js b/tests/scriptmodule.test.js index e0a6b62..6f18337 100644 --- a/tests/scriptmodule.test.js +++ b/tests/scriptmodule.test.js @@ -1,41 +1,37 @@ -import { describe, expect, test } from "@jest/globals"; - -const torch = require("../dist"); +const torch = require('../dist'); const testModelPath = `${__dirname}/resources/test_model.pt`; -describe("Constructor", () => { - test("Call constructor using valid model path", () => { +describe('Constructor', () => { + test('Call constructor using valid model path', () => { const scriptModule = new torch.ScriptModule(testModelPath); expect(scriptModule.toString()).toMatch(/ScriptModule.*\.pt/); }); // TODO -- Fine tune error message to remove stacktrace for error thrown on invalid model file - test("Call constructor from invalid model path", () => { - const t = () => new torch.ScriptModule("/resources/no_model.pt"); - expect(t).toThrow( - /open file failed because of errno 2 on fopen: No such file or directory, file path: \/resources\/no_model.pt/ - ); + test('Call constructor from invalid model path', () => { + const t = () => new torch.ScriptModule('/resources/no_model.pt'); + expect(t).toThrow(/open file failed because of errno 2 on fopen.*/); expect(true).toEqual(true); }); - test("Call constructor with missing params", () => { + test('Call constructor with missing params', () => { const t = () => new torch.ScriptModule(); - expect(t).toThrow(new Error("A string was expected")); + expect(t).toThrow(new Error('A string was expected')); }); - test("Call constructor with invalid params", () => { + test('Call constructor with invalid params', () => { const t = () => new torch.ScriptModule(true); const t2 = () => new torch.ScriptModule(123); const t3 = () => new torch.ScriptModule(122.3); - expect(t).toThrow(new Error("A string was expected")); - expect(t2).toThrow(new Error("A string was expected")); - expect(t3).toThrow(new Error("A string was expected")); + expect(t).toThrow(new Error('A string was expected')); + expect(t2).toThrow(new Error('A string was expected')); + expect(t3).toThrow(new Error('A string was expected')); }); }); -describe("toString", () => { - test("Call toString using valid model path", () => { +describe('toString', () => { + test('Call toString using valid model path', () => { const scriptModule = new torch.ScriptModule(testModelPath); expect(scriptModule.toString()).toMatch(/ScriptModule.*\.pt/); }); @@ -45,14 +41,14 @@ describe("toString", () => { expect(scriptModule.toString(3)).toMatch(/ScriptModule.*\.pt/); expect(scriptModule.toString(false)).toMatch(/ScriptModule.*\.pt/); expect(scriptModule.toString(3.233)).toMatch(/ScriptModule.*\.pt/); - expect(scriptModule.toString(["Hello world"])).toMatch( + expect(scriptModule.toString(['Hello world'])).toMatch( /ScriptModule.*\.pt/ ); }); }); -describe("Forward function", () => { - test("Call to forward using valid tensor params", async () => { +describe('Forward function', () => { + test('Call to forward using valid tensor params', async () => { const scriptModule = new torch.ScriptModule(testModelPath); const a = torch.tensor([ [0.1, 0.2, 0.3], @@ -68,7 +64,7 @@ describe("Forward function", () => { }); // TODO -- Fine tune error message to remove stacktrace for missing arguement value -- at the moment, the multiple lines make it difficult to test for an error message - test("Call to forward using missing second tensor param", async () => { + test('Call to forward using missing second tensor param', async () => { const scriptModule = new torch.ScriptModule(testModelPath); const a = torch.tensor([ [0.1, 0.2, 0.3], diff --git a/tests/tensor.test.js b/tests/tensor.test.js index 7b3f77d..053937e 100644 --- a/tests/tensor.test.js +++ b/tests/tensor.test.js @@ -1,9 +1,7 @@ -import { describe, expect, test } from "@jest/globals"; +const torch = require('../dist'); -const torch = require("../dist"); - -describe("Tensor creation", () => { - test("Tensor creation using valid array (1-D)", () => { +describe('Tensor creation', () => { + test('Tensor creation using valid array (1-D)', () => { const a = torch.tensor([[1, 2, 3, 4, 5, 6]]).toObject(); expect(a.data.length).toBe(6); expect(a.shape).toMatchObject([1, 6]); @@ -13,7 +11,7 @@ describe("Tensor creation", () => { expect(b.shape).toMatchObject([1, 3]); }); - test("Tensor creation using valid array (multi-dimensional)", () => { + test('Tensor creation using valid array (multi-dimensional)', () => { const a = torch .tensor([ [0.1, 0.2, 0.3], @@ -34,7 +32,7 @@ describe("Tensor creation", () => { expect(b.shape).toMatchObject([3, 3]); }); - test("Tensor creation using valid object", () => { + test('Tensor creation using valid object', () => { const a = torch .tensor(new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5]), { shape: [1, 5], @@ -52,17 +50,17 @@ describe("Tensor creation", () => { expect(b.shape).toMatchObject([3, 3]); }); - test("Tensor creation using invalid params", () => { + test('Tensor creation using invalid params', () => { const t = () => torch.tensor(); const t2 = () => torch.tensor(123); const t3 = () => torch.tensor(true); expect(t).toThrow("Cannot read properties of undefined (reading 'length')"); - expect(t2).toThrow("Invalid argument"); - expect(t3).toThrow("Invalid argument"); + expect(t2).toThrow('Invalid argument'); + expect(t3).toThrow('Invalid argument'); }); - test("Tensor clone", () => { + test('Tensor clone', () => { const t = torch.rand(2, 3); const tObject = t.toObject(); const tCloneObject = t.clone().toObject(); diff --git a/yarn.lock b/yarn.lock index 8508fd0..146df9c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1894,6 +1894,13 @@ eslint-plugin-import@^2.26.0: resolve "^1.22.0" tsconfig-paths "^3.14.1" +eslint-plugin-prettier@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.0.0.tgz#8b99d1e4b8b24a762472b4567992023619cb98e0" + integrity sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ== + dependencies: + prettier-linter-helpers "^1.0.0" + eslint-scope@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c" @@ -2080,6 +2087,11 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3: resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q== +fast-diff@^1.1.2: + version "1.2.0" + resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03" + integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w== + fast-glob@^3.2.9: version "3.2.11" resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9" @@ -4058,6 +4070,13 @@ prelude-ls@~1.1.2: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ= +prettier-linter-helpers@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz#d23d41fe1375646de2d0104d3454a3008802cf7b" + integrity sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w== + dependencies: + fast-diff "^1.1.2" + prettier@^2.6.2: version "2.6.2" resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032"