Skip to content

Commit

Permalink
Imrpoved Vite configuration and prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
elPandaFriki committed Dec 25, 2023
1 parent 79c5bd9 commit 00a8468
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 115 deletions.
83 changes: 83 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
"typescript": "^5.3.3",
"vite": "^5.0.9",
"vite-plugin-checker": "^0.6.2",
"vite-plugin-full-reload": "^1.1.0",
"vite-tsconfig-paths": "^4.2.2",
"vitest": "^1.0.4",
"webdriverio": "^8.26.1"
}
Expand Down
File renamed without changes.
13 changes: 5 additions & 8 deletions src/components/Root/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component } from 'react';
import { StrictMode } from 'react';
import App from '../App';
import { store } from '../../redux';
import { Provider } from 'react-redux';
Expand All @@ -9,13 +8,11 @@ import { Props, State } from './types';
export default class Root extends Component<Props, State> {
render() {
return (
<StrictMode>
<Provider store={store}>
<Version>
<App />
</Version>
</Provider>
</StrictMode>
<Provider store={store}>
<Version>
<App />
</Version>
</Provider>
);
}
}
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './index.scss';
import Root from './components/Root';
import { createRoot } from 'react-dom/client';
import { setPrototypes } from './tools';
import { setPrototypes } from './prototypes';

function setHotReload() {
if (!import.meta.hot) return;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/prototypes/Array.ts → src/prototypes/Array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from '..';
import { isEqual } from '../tools';

export function setArrayPrototype() {
Array.prototype.isEqual = isEqual;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from '..';
import { isEqual } from '../tools';

export function setBooleanPrototype() {
Boolean.prototype.isEqual = isEqual;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from '..';
import { isEqual } from '../tools';

export function setNumberPrototype() {
Number.prototype.isEqual = isEqual;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from '..';
import { isEqual } from '../tools';

export function setObjectPrototype() {
Object.prototype.isEqual = isEqual;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from '..';
import { isEqual } from '../tools';

export function setStringPrototype() {
String.prototype.isEqual = isEqual;
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions src/prototypes/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export {};

declare global {
interface Object {
isEqual(this: object, target: unknown): boolean;
}
interface Array<T> {
isEqual(this: Array<T>, target: unknown): boolean;
}
interface String {
isEqual(this: string, target: unknown): boolean;
}
interface Number {
isEqual(this: number, target: unknown): boolean;
}
interface Boolean {
isEqual(this: boolean, target: unknown): boolean;
}
}
3 changes: 1 addition & 2 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './methods';
export * from './prototypes';
export * from './methods';
77 changes: 18 additions & 59 deletions src/tools/methods/isEqual.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,22 @@
export function isEqual(this: unknown, target: unknown) {
let leftChain: Array<unknown> = [];
let rightChain: Array<unknown> = [];
function compare2Objects(x: unknown, y: unknown) {
let p;
if (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)) return true;
if (x === y) return true;
if (
(typeof x === 'function' && typeof y === 'function') ||
(x instanceof Date && y instanceof Date) ||
(x instanceof RegExp && y instanceof RegExp) ||
(x instanceof String && y instanceof String) ||
(x instanceof Number && y instanceof Number)
)
return x.toString() === y.toString();
if (!(x instanceof Object && y instanceof Object)) return false;
if (Object.prototype.isPrototypeOf.call(x, y) || Object.prototype.isPrototypeOf.call(y, x))
return false;
if (x.constructor !== y.constructor) return false;
if (Object.prototype.valueOf.call(x) !== Object.prototype.valueOf.call(y)) return false;
if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) return false;
const a = (x as Record<string, unknown>)
const b = (y as Record<string, unknown>)
for (const p in b) {
if (
Object.prototype.hasOwnProperty.call(y, p) !==
Object.prototype.hasOwnProperty.call(x, p)
) {
return false;
} else if (typeof b[p] !== typeof a[p]) {
return false;
}
if (this === target) return true;
// this verification for primitives (number, string, boolean, etc.)
if (this == null || target == null || (typeof this !== 'object' && typeof target !== 'object'))
return this === target;
// deep object/array comparison
// keep shallow copy of comparator
const remainder = {
...target
};
for (const key in this) {
if (this[key].isEqual(target[key])) {
// remove properties from shallow copy to keep track of verified children
delete remainder[key];
continue;
}
for (p in x) {
if (
Object.prototype.hasOwnProperty.call(y, p) !==
Object.prototype.hasOwnProperty.call(x, p)
)
return false;
else if (typeof b[p] !== typeof b[p]) return false;
switch (typeof a[p]) {
case 'object':
case 'function': {
leftChain.push(x);
rightChain.push(y);
if (!compare2Objects(a[p], b[p])) return false;
leftChain.pop();
rightChain.pop();
break;
}
default: {
if (a[p] !== b[p]) return false;
break;
}
}
}
return true;
// if one property of comparator is not equal to itself on the original
return false;
}
leftChain = [];
rightChain = [];
if (!compare2Objects(this, target)) return false;
return true;
// check if comparator has the same properties than original
return Object.entries(remainder).length === 0;
}
15 changes: 0 additions & 15 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
export {};

declare global {
interface Object {
isEqual(this: unknown, target: unknown): boolean;
}
interface Array<T> {
isEqual(this: Array<T>, target: unknown): boolean;
}
interface String {
isEqual(this: string, target: unknown): boolean;
}
interface Number {
isEqual(this: number, target: unknown): boolean;
}
interface Boolean {
isEqual(this: boolean, target: unknown): boolean;
}
namespace Window {
//
}
Expand Down
20 changes: 16 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"module": "ESNext",
"skipLibCheck": true, // this causes .d.ts files to not have type check
"allowJs": true,
"skipLibCheck": false, // If true causes .d.ts files to not have type check
"allowJs": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,

Expand All @@ -18,19 +18,31 @@

/* Linting */
"strict": true,
"noImplicitAny": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"useDefineForClassFields": true,

/* Paths*/
"baseUrl": ".",
"paths": {
"resources/*": ["src/resources/*"],
"proto": ["src/proto/index.ts"],
"proto/*": ["src/proto/*"],
"enums": ["src/enums/index.ts"],
"enums/*": ["src/enums/*"]
},

/* Type overrides */
"types": ["vite/client", "./src/react-env.d.ts", "vitest"]
"types": ["vite/client", "./react-env.d.ts", "vitest"]
},
"include": ["*", "src"],
"exclude": ["node_modules", "dist", "build", "scripts"],
"references": [
{
"path": "./tsconfig.node.json"
}
]
}
}
2 changes: 1 addition & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"esModuleInterop": true
},
"include": ["vite.config.ts"]
}
}
6 changes: 3 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'vite';
import config from './vite.shared';
import shared from './vite.shared';

// https://vitejs.dev/config/
export default defineConfig(async () => {
return config;
});
return shared;
});
Loading

0 comments on commit 00a8468

Please sign in to comment.