Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(utilities/data-types): exports #173

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
"space-infix-ops": "error",
"new-cap": ["error", {"capIsNewExceptionPattern": "Mixin$"}],
"brace-style": ["error", "stroustrup", {"allowSingleLine": true}],
"indent": "off",
"@typescript-eslint/indent": [
"indent": [
"error",
2,
{
Expand Down
19 changes: 4 additions & 15 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,13 @@
"*.njk": "html",
"ssh-config": "ssh_config"
},
"markdownlint.ignore": [
"**/CHANGELOG.md"
],
"markdownlint.ignore": ["**/CHANGELOG.md"],
"cSpell.language": "en,fa,fa-IR",
"cSpell.words": [
"Alwatr",
"Gecut"
],
"cSpell.words": ["Alwatr", "Gecut"],
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"git.autoStash": true,
"git.branchProtection": [
"main",
"next"
],
"githubPullRequests.ignoredPullRequestBranches": [
"main",
"next"
],
"git.branchProtection": ["main", "next"],
"githubPullRequests.ignoredPullRequestBranches": ["main", "next"],
"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@typescript-eslint/eslint-plugin": "^8.5.0",
"@typescript-eslint/parser": "^8.5.0",
"@web/dev-server": "^0.4.6",
"eslint": "^9.10.0",
"eslint": "^8.0.0",
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-import": "^2.30.0",
"npm-run-all": "^4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

### Bug Fixes

* **logger:** log ([ed2ff07](https://github.com/gecut/hybrid-core/commit/ed2ff07649d2cb0d1665a541ec38eefa91537837)) by @MM25Zamanian
- **logger:** log ([ed2ff07](https://github.com/gecut/hybrid-core/commit/ed2ff07649d2cb0d1665a541ec38eefa91537837)) by @MM25Zamanian

## [1.4.5](https://github.com/gecut/hybrid-core/compare/@gecut/logger@1.4.4...@gecut/logger@1.4.5) (2024-07-04)

Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import {env} from '@gecut/utilities/env.js';
export const NODE_MODE = isNode();

export const DEV_MODE =
(NODE_MODE === false ? localStorage?.getItem('DEBUG') ?? '0' : env('DEBUG', '0', 'string')) === '1';
(NODE_MODE === false ? (localStorage?.getItem('DEBUG') ?? '0') : env('DEBUG', '0', 'string')) === '1';
12 changes: 6 additions & 6 deletions packages/signal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

### Bug Fixes

* **signal:** core notify ([63d37e0](https://github.com/gecut/hybrid-core/commit/63d37e0b9a29a86c13357bc5c9807e89f865db00)) by @MM25Zamanian
- **signal:** core notify ([63d37e0](https://github.com/gecut/hybrid-core/commit/63d37e0b9a29a86c13357bc5c9807e89f865db00)) by @MM25Zamanian

## [2.3.3](https://github.com/gecut/hybrid-core/compare/@gecut/signal@2.3.2...@gecut/signal@2.3.3) (2024-07-06)

Expand All @@ -21,29 +21,29 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

### Bug Fixes

* **signal:** npm files ([b33e1bd](https://github.com/gecut/hybrid-core/commit/b33e1bd6fa96523f5e7525a8df947342e94fa405)) by @MM25Zamanian
- **signal:** npm files ([b33e1bd](https://github.com/gecut/hybrid-core/commit/b33e1bd6fa96523f5e7525a8df947342e94fa405)) by @MM25Zamanian

## [2.3.1](https://github.com/gecut/hybrid-core/compare/@gecut/signal@2.3.0...@gecut/signal@2.3.1) (2024-07-04)

### Bug Fixes

* **signal:** exports ([ad09e9d](https://github.com/gecut/hybrid-core/commit/ad09e9d7681a82a89e5e178d144bf44b857b13c2)) by @MM25Zamanian
- **signal:** exports ([ad09e9d](https://github.com/gecut/hybrid-core/commit/ad09e9d7681a82a89e5e178d144bf44b857b13c2)) by @MM25Zamanian

# [2.3.0](https://github.com/gecut/hybrid-core/compare/@gecut/signal@2.2.3...@gecut/signal@2.3.0) (2024-07-04)

### Bug Fixes

* **signal:** path ([9bcfca2](https://github.com/gecut/hybrid-core/commit/9bcfca2dc618024a0ab260ab4c8a3c24360c29bd)) by @MM25Zamanian
- **signal:** path ([9bcfca2](https://github.com/gecut/hybrid-core/commit/9bcfca2dc618024a0ab260ab4c8a3c24360c29bd)) by @MM25Zamanian

### Features

* **signal:** signal state ([2e86970](https://github.com/gecut/hybrid-core/commit/2e86970da56af295ff31bc5ab1e0fbd2f02f89fe)) by @MM25Zamanian
- **signal:** signal state ([2e86970](https://github.com/gecut/hybrid-core/commit/2e86970da56af295ff31bc5ab1e0fbd2f02f89fe)) by @MM25Zamanian

## [2.2.3](https://github.com/gecut/hybrid-core/compare/@gecut/signal@2.2.2...@gecut/signal@2.2.3) (2024-06-20)

### Bug Fixes

* **signal:** context `functionalValue` ([70e046d](https://github.com/gecut/hybrid-core/commit/70e046d3620f26eedddec78d5b58cfd8062d9288)) by @MM25Zamanian
- **signal:** context `functionalValue` ([70e046d](https://github.com/gecut/hybrid-core/commit/70e046d3620f26eedddec78d5b58cfd8062d9288)) by @MM25Zamanian

## [2.2.2](https://github.com/gecut/hybrid-core/compare/@gecut/signal@2.2.1...@gecut/signal@2.2.2) (2024-06-12)

Expand Down
7 changes: 1 addition & 6 deletions packages/types/src/type-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ export type ObjectPartial<T, K extends ObjectBooleanize<T>> = {
[P in keyof T]: K[P] extends true ? T[P] : undefined;
};

export type SanitizeFunction<T> = {
// eslint-disable-next-line @typescript-eslint/ban-types
[P in keyof T]: T[P] extends Function ? undefined : T[P];
};

/**
* Object that can be JSON.stringify.
*/
Expand All @@ -44,7 +39,7 @@ export type Prop<T, K> = K extends keyof T ? T[K] : never;

export type Values<T> = T[keyof T];
export type Keys<T> = keyof T;
export type ArrayValues<T extends Readonly<any[]>> = T[number];
export type ArrayValues<T extends readonly any[]> = T[number];
export type ArrayItems<T> = T extends (infer K)[] ? K : T;

export type UnknownRecord<T = unknown> = Record<string, T>;
2 changes: 1 addition & 1 deletion packages/utilities/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

### Bug Fixes

* **signal:** context `functionalValue` ([70e046d](https://github.com/gecut/hybrid-core/commit/70e046d3620f26eedddec78d5b58cfd8062d9288)) by @
- **signal:** context `functionalValue` ([70e046d](https://github.com/gecut/hybrid-core/commit/70e046d3620f26eedddec78d5b58cfd8062d9288)) by @

# [5.4.0](https://github.com/gecut/hybrid-core/compare/@gecut/utilities@5.3.0...@gecut/utilities@5.4.0) (2024-06-12)

Expand Down
32 changes: 13 additions & 19 deletions packages/utilities/src/data-types/array.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import {numberUtils} from './number.js';
import {Nullable} from '@gecut/types';

export const arrayUtils = {
is: Array.isArray,
sanitize<T extends unknown[]>(array: T): T {
return array.filter((item, index) => array.indexOf(item) === index) as T;
},
range(count: number): number[] {
return Array.from({length: count}, (_, i) => i + 1);
},
joinString(separator = ' ', ...values: (unknown | undefined | null)[]): string {
values = values.map((value) => String(value));
import {randomNumber} from './number';

return values.join(separator);
},
random: {
choice<T>(arr: T[]): T {
return arr[numberUtils.random.number(arr.length - 1)];
},
},
};
export const isArray = (value: unknown): value is unknown[] => Array.isArray(value);

export const sanitize = <T extends unknown[]>(array: T): T =>
array.filter((item, index) => array.indexOf(item) === index) as T;

export const range = (count: number): number[] => Array.from({length: count}, (_, i) => i + 1);

export const joinString = (separator = ' ', ...values: Nullable<unknown>[]): string =>
values.map(String).join(separator);

export const choose = <T>(arr: T[]): T => arr[randomNumber(arr.length - 1)];
48 changes: 24 additions & 24 deletions packages/utilities/src/data-types/number.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import type {Nullable} from '@gecut/types/type-helper.js';

export const numberUtils = {
is(value: unknown) {
return (
(typeof value === 'number' && isFinite(value)) ||
(typeof value === 'string' && value.trim() !== '' && !isNaN(Number(value)))
);
},
sanitizer<T extends Nullable<number>>(data: T): NonNullable<T> {
return Number(data ?? 0) as NonNullable<T>;
},
clamp(min: number, current: number, max: number) {
return Math.min(max, Math.max(min, current));
},
random: {
number(max: number, min = 0): number {
min = Math.ceil(min);
max = Math.floor(max);
export const isNumber = (value: unknown): boolean => {
return (
(typeof value === 'number' && isFinite(value)) ||
(typeof value === 'string' && value.trim() !== '' && !isNaN(Number(value)))
);
};

export const sanitizeNumber = <T extends Nullable<number>>(data: T): NonNullable<T> => {
return Number(data ?? 0) as NonNullable<T>;
};

export const clamp = (min: number, current: number, max: number): number => {
return Math.min(max, Math.max(min, current));
};

export const randomNumber = (max: number, min = 0): number => {
min = Math.ceil(min);
max = Math.floor(max);

return Math.floor(Math.random() * (max - min) + min);
};

return Math.floor(Math.random() * (max - min) + min);
},
floor(max: number, min = 0, fixed = 1): number {
const keyNumber = fixed * 10;
export const randomFloor = (max: number, min = 0, fixed = 1): number => {
const keyNumber = fixed * 10;

return this.number(max * keyNumber, min * keyNumber) / keyNumber;
},
},
return randomNumber(max * keyNumber, min * keyNumber) / keyNumber;
};
6 changes: 2 additions & 4 deletions packages/utilities/src/data-types/string.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {Nullable} from '@gecut/types';

export const stringUtils = {
sanitizer<T extends Nullable<string>>(data: T): NonNullable<T> {
return String(data ?? '') as NonNullable<T>;
},
export const sanitize = <T extends Nullable<string>>(str: T): T => {
return String(str ?? '') as NonNullable<T>;
};
70 changes: 40 additions & 30 deletions packages/utilities/src/data-types/unknown.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
export const unknownUtils = {
isEqual<T>(a: T, b: T): boolean {
if (typeof a !== typeof b) {
return false;
}
export function isEqual<T>(a: T, b: T): boolean {
// Early exit for identical references
if (a === b) {
return true;
}

if (typeof a === 'object') {
if (a === null || b === null) {
return a === b;
}
// Handle null and undefined values
if (a == null || b == null) {
return false;
}

const aKeys = Object.keys(a);
const bKeys = Object.keys(b as T & object);
// Check types
if (typeof a !== typeof b) {
return false;
}

if (aKeys.length !== bKeys.length) {
// Handle arrays
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!isEqual(a[i], b[i])) {
return false;
}
}
return true;
}

if (Array.isArray(a) && Array.isArray(b)) {
for (let i = 0; i < aKeys.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
}
else {
for (const key of aKeys) {
if (!bKeys.includes(key)) {
return false;
}
}
}
// Handle objects
if (typeof a === 'object' && typeof b === 'object') {
const aKeys = Object.keys(a) as (keyof typeof a)[];
const bKeys = Object.keys(b) as (keyof typeof b)[];

if (aKeys.length !== bKeys.length) {
return false;
}

return true;
for (const key of aKeys) {
if (!bKeys.includes(key) || !isEqual(a[key], b[key])) {
return false;
}
}
return true;
}

return a === b;
},
};
// Handle primitive values
return a === b;
}
Loading