Skip to content

Commit

Permalink
⚡ working eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tokyo committed Dec 8, 2024
1 parent 0e67dd1 commit 8e218be
Show file tree
Hide file tree
Showing 5 changed files with 1,162 additions and 119 deletions.
28 changes: 23 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
module.exports = {
root: true,
extends: ['airbnb', 'prettier'],
parser: 'babel-eslint',
"extends": [
"airbnb",
"plugin:import/typescript",
"prettier"
],
"parser": "@babel/eslint-parser",
env: {
browser: true,
es6: true,
node: true,
jest: true,
},
plugins: ['react', 'prettier'],
"plugins": [
"react",
"@typescript-eslint",
"import",
"prettier"
],
rules: {
'import/extensions': [
'error',
'ignorePackages',
{
js: 'never',
jsx: 'never',
ts: 'never',
tsx: 'never',
},
],
'import/prefer-default-export': 'off',
'import/no-named-as-default-member': 'off',
'jsx-a11y/label-has-associated-control': 0,
'no-console': 0,
'no-underscore-dangle': 0,
'no-console': 1,
'react/jsx-filename-extension': 0,
'react/jsx-props-no-spreading': 0,
'react/require-default-props': 0,
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"scripts": {
"start": "webpack serve --mode development --port 3001",
"transpile": "rm -rf dist && yarn tsc -p ./tsconfig.build.json && babel src -x .ts,.js --out-dir dist -s --copy-files && rm -rf dist/**/__snapshots__ && find ./dist -name '*.test.js*' -type f -delete ",
"transpile": "rm -rf dist && yarn eslint --ext .ts src && yarn tsc -p ./tsconfig.build.json && babel src -x .ts,.js --out-dir dist -s --copy-files && rm -rf dist/**/__snapshots__ && find ./dist -name '*.test.js*' -type f -delete ",
"prepublishOnly": "npm run transpile",
"build": "webpack --mode production",
"deploy": "gh-pages -d demo/dist",
Expand All @@ -36,11 +36,14 @@
"devDependencies": {
"@babel/cli": "^7.14.8",
"@babel/core": "^7.14.8",
"@babel/eslint-parser": "^7.19.1",
"@babel/preset-env": "^7.14.8",
"@babel/preset-react": "^7.14.5",
"@babel/preset-typescript": "^7.26.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react-hooks": "^7.0.1",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.2",
"css-loader": "^6.2.0",
Expand All @@ -50,7 +53,8 @@
"eslint": "^7.31.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.24.0",
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export default function add(x, y) { return x + y };
import cosineSimilarity from './utils/cosineSimilarity';

const x = '';

console.log('x');

export default function add(x, y) {
return typeof cosineSimilarity === 'function' ? x + y : 0;
}
41 changes: 41 additions & 0 deletions src/utils/cosineSimilarity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Computes the cosine similarity between two numerical vectors.
*
* @param {number[]} a - The first input vector.
* @param {number[]} b - The second input vector.
* @returns {number} - The cosine similarity between vectors `a` and `b`.
* Range: [-1, 1]. Returns NaN if both vectors are zero.
*
* @complexity
* Time Complexity: O(n)
* - The function processes each element of the vectors `a` and `b` exactly once in a single loop.
*
* Space Complexity: O(1)
* - The function uses a constant amount of extra space regardless of the input size.
*/
export default function cosineSimilarity(a: number[], b: number[]): number {
// Initialize variables for dot product and squared norms
let dotProduct = 0; // Sum of element-wise products of a and b
let normASq = 0; // Sum of squares of elements in a
let normBSq = 0; // Sum of squares of elements in b

// Loop through the elements of both vectors
for (let i = 0; i < a.length; i += 1) {
// Update dot product with the product of corresponding elements
dotProduct += a[i] * b[i];
// Update the squared norm for vector a
normASq += a[i] * a[i];
// Update the squared norm for vector b
normBSq += b[i] * b[i];
}

// Calculate the Euclidean norms of both vectors
const normA = Math.sqrt(normASq); // Magnitude of vector a
const normB = Math.sqrt(normBSq); // Magnitude of vector b

// Return cosine similarity:
// If both norms are non-zero, calculate the similarity as:
// dotProduct / (normA * normB).
// If either norm is zero, this will return NaN.
return dotProduct / (normA * normB);
}
Loading

0 comments on commit 8e218be

Please sign in to comment.