Skip to content

Commit

Permalink
refactor(🧹): architecture + code + tests
Browse files Browse the repository at this point in the history
β”œβ”€β”€ source
β”‚   β”œβ”€β”€ core
β”‚   β”‚   β”œβ”€β”€ data
β”‚   β”‚   β”‚   └── transformer
β”‚   β”‚   β”œβ”€β”€ security
β”‚   β”‚   └── util
β”‚   β”œβ”€β”€ error
β”‚   β”‚   └── key
β”‚   β”œβ”€β”€ i18n
β”‚   └── types
β”‚       └── data
└── test
    β”œβ”€β”€ core
    β”‚   β”œβ”€β”€ data
    β”‚   β”œβ”€β”€ security
    β”‚   └── util
    └── error
  • Loading branch information
Necrelox committed Jan 7, 2025
1 parent e73dfa5 commit d886f85
Show file tree
Hide file tree
Showing 54 changed files with 336 additions and 413 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ build
docs/
coverage/

node_modules

tsconfig.tsbuildinfo
node_modules
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*.tgz
bun.lockb
pnpm-lock.yaml
tsconfig.tsbuildinfo

# === Bundler configuration files ===
bundler.config.ts
Expand Down
Binary file modified bun.lockb
Binary file not shown.
37 changes: 26 additions & 11 deletions bundler.config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import dts from 'bun-plugin-dts';

import pkg from './package.json';

const dependencies = 'dependencies' in pkg ? Object.keys(pkg.dependencies ?? {}) : [];
const devDependencies = 'devDependencies' in pkg ? Object.keys(pkg.devDependencies ?? {}) : [];
const peerDependencies = 'peerDependencies' in pkg ? Object.keys(pkg.peerDependencies ?? {}) : [];

await Bun.build({
target: 'bun',
external: [...dependencies, ...devDependencies, ...peerDependencies],
root: './source',
entrypoints: [
'./source/index.ts',
'./source/common/error/index.ts',
'./source/common/type/data/index.ts',
'./source/common/util/index.ts',
'./source/domain/service/data/index.ts',
'./source/domain/service/data/strategy/transformer/index.ts',
'./source/domain/service/global/index.ts',
'./source/domain/service/security/index.ts',
'./source/common/i18n/index.ts'
'./source/core/data/index.ts',
'./source/core/data/transformer/index.ts',

'./source/core/security/index.ts',

'./source/core/util/index.ts',

'./source/error/index.ts',
'./source/error/key/index.ts',

'./source/i18n/index.ts',

'./source/types/index.ts',

'./source/index.ts'
],
plugins: [
dts({
output: {
noBanner: true
}
})
],
outdir: './build',
splitting: true,
format: 'esm',
minify: true,
sourcemap: process.env.NODE_ENV === 'development' ? 'external' : 'none',
target: 'node',
sourcemap: process.env.NODE_ENV === 'development' ? 'external' : 'none'
});
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export default [
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unnecessary-type-parameters': 'off',
'@typescript-eslint/no-dynamic-delete': 'off'
'@typescript-eslint/no-dynamic-delete': 'off',
'@typescript-eslint/no-confusing-void-expression': 'off'
}
}
];
78 changes: 25 additions & 53 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,26 @@
"type": "module",
"types": "build/index.d.ts",
"exports": {
".": {
"types": "./build/index.d.ts",
"import": "./build/index.js",
"require": "./build/index.js"
},
"./error": {
"types": "./build/common/error/index.d.js",
"import": "./build/common/error/index.js",
"require": "./build/common/error/index.js"
},
"./type": {
"types": "./build/common/type/data/index.d.js",
"import": "./build/common/type/data/index.js",
"require": "./build/common/type/data/index.js"
},
"./util": {
"types": "./build/common/util/index.d.js",
"import": "./build/common/util/index.js",
"require": "./build/common/util/index.js"
},
"./data": {
"types": "./build/domain/service/data/index.d.js",
"import": "./build/domain/service/data/index.js",
"require": "./build/domain/service/data/index.js"
},
"./transformerStrategies": {
"types": "./build/domain/service/data/transformerStrategies/index.d.js",
"import": "./build/domain/service/data/transformerStrategies/index.js",
"require": "./build/domain/service/data/transformerStrategies/index.js"
},
"./global": {
"types": "./build/domain/service/global/index.d.js",
"import": "./build/domain/service/global/index.js",
"require": "./build/domain/service/global/index.js"
},
"./security": {
"types": "./build/domain/service/security/index.d.js",
"import": "./build/domain/service/security/index.js",
"require": "./build/domain/service/security/index.js"
},
"./translation": {
"types": "./build/common/i18n/index.d.js",
"import": "./build/common/i18n/index.js",
"require": "./build/common/i18n/index.js"
}
"./data": "./build/core/data/index.js",
"./data/transformer": "./build/core/data/transformer",

"./security": "./build/core/security/index.js",

"./util": "./build/core/util/index.js",

"./error": "./build/error/index.js",
"./error/key": "./build/error/key/index.js",

"./i18n": "./build/i18n/index.js",

"./types": "./build/types/index.js",

".": "./build/index.js"
},
"scripts": {
"dev": "bun --watch source/index.ts",
"build": "tsc && tsc-alias && NODE_ENV=production bun bundler.config.ts",
"start": "NODE_ENV=production bun build/index.js",
"build": "NODE_ENV=production bun bundler.config.ts",
"start": "bun build/index.js",
"test": "bun test --coverage",
"docs": "typedoc",
"fix-lint": "eslint --fix ./source",
Expand All @@ -76,15 +46,17 @@
"url": "https://github.com/Basalt-Lab/basalt-helper/issues"
},
"devDependencies": {
"@types/bun": "^1.1.14",
"@types/node": "^22.10.2",
"@eslint/js": "^9.17.0",
"@stylistic/eslint-plugin": "^2.12.1",
"eslint": "^9.17.0",
"@types/bun": "^1.1.15",
"@types/node": "^22.10.5",
"bun-plugin-dts": "^0.3.0",
"eslint-plugin-tsdoc": "^0.4.0",
"typescript-eslint": "^8.18.1",
"tsc-alias": "^1.8.10",
"typedoc": "^0.27.5",
"eslint": "^9.17.0",
"typescript-eslint": "^8.19.1",
"typedoc": "^0.27.6"
},
"peerDependencies": {
"typescript": "^5.7.2"
},
"dependencies": {
Expand Down
20 changes: 0 additions & 20 deletions source/common/error/global.error.ts

This file was deleted.

2 changes: 0 additions & 2 deletions source/common/error/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions source/common/i18n/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion source/common/type/data/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions source/common/util/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions source/common/util/sleep.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BasaltError } from '#/common/error/basalt.error.ts';
import { GLOBAL_ERRORS } from '#/common/error/global.error.ts';
import type { BasaltKeyTransformer } from '#/common/type/data/basaltKeyTransformer.ts';
import { BasaltError } from '#/error/basaltError';
import { CORE_DATA_KEY_ERROR } from '#/error/key/coreDataKeyError';
import type { BasaltKeyTransformer } from '#/types/data/basaltKeyTransformer';

/**
* Checks if the provided data is null or undefined and throws an error if it is.
Expand All @@ -9,12 +9,12 @@ import type { BasaltKeyTransformer } from '#/common/type/data/basaltKeyTransform
*
* @param data - The data to be validated.
*
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link GLOBAL_ERRORS.DATA_IS_NULL})
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link CORE_DATA_KEY_ERROR}.DATA_IS_NULL)
*/
function _validateDataNull<T>(data: T): void {
if (data === null || data === undefined)
throw new BasaltError({
key: GLOBAL_ERRORS.DATA_IS_NULL
key: CORE_DATA_KEY_ERROR.DATA_IS_NULL
});
}

Expand All @@ -25,49 +25,15 @@ function _validateDataNull<T>(data: T): void {
*
* @param data - The data to be validated.
*
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link GLOBAL_ERRORS.DATA_MUST_BE_OBJECT})
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link CORE_DATA_KEY_ERROR}.DATA_MUST_BE_OBJECT)
*/
function _validateDataIsObject<T>(data: T): void {
if (typeof data !== 'object')
throw new BasaltError({
key: GLOBAL_ERRORS.DATA_MUST_BE_OBJECT
key: CORE_DATA_KEY_ERROR.DATA_MUST_BE_OBJECT
});
}

/**
* Creates a deep clone of the provided data object.
*
* @deprecated This method is deprecated and will be removed in the future. Use the `structuredClone` method instead.
* @see https://nodejs.org/api/globals.html#structuredclonevalue-options
*
* @typeParam T - The type of the data to be cloned.
*
* @param data - The data object to be cloned.
*
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link GLOBAL_ERRORS.DATA_IS_NULL})
*
* @returns The deep cloned object. ({@link T})
*/
export function deepClone<T>(data: T): T {
_validateDataNull(data);
if (data instanceof Date)
return new Date(data.getTime()) as T;
if (data instanceof RegExp)
return new RegExp(data) as T;
if (Array.isArray(data))
// eslint-disable-next-line @typescript-eslint/no-deprecated
return data.map((item: unknown) => deepClone(item)) as T;
if (typeof data === 'object') {
const clonedObject: Partial<T> = {} as Partial<T>;
for (const key in data)
if (Object.hasOwn(data, key))
// eslint-disable-next-line @typescript-eslint/no-deprecated
clonedObject[key as keyof T] = deepClone(data[key]) as T[keyof T];
return clonedObject as T;
}
return data;
}

/**
* Filters the provided data by excluding the specified keys. This method will create
* a new object that contains all properties from the original data object except for
Expand All @@ -80,8 +46,8 @@ export function deepClone<T>(data: T): T {
* @param keys - The array of keys to exclude from the data object. (Can be empty)
* @param excludeNullUndefined - Flag to determine if properties with null or undefined values should be excluded.
*
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link GLOBAL_ERRORS.DATA_IS_NULL})
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link GLOBAL_ERRORS.DATA_MUST_BE_OBJECT})
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link CORE_DATA_KEY_ERROR}.DATA_IS_NULL)
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link CORE_DATA_KEY_ERROR}.DATA_MUST_BE_OBJECT)
*
* @example
* ```typescript
Expand Down Expand Up @@ -121,8 +87,8 @@ export function filterByKeyExclusion<T extends Readonly<object>>(data: Readonly<
* @param keys - The array of keys to include in the resulting data object. (Can be empty)
* @param excludeNullUndefined - Flag to determine if properties with null or undefined values should be excluded.
*
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link GLOBAL_ERRORS.DATA_IS_NULL})
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link GLOBAL_ERRORS.DATA_MUST_BE_OBJECT})
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link CORE_DATA_KEY_ERROR}.DATA_IS_NULL)
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link CORE_DATA_KEY_ERROR}.DATA_MUST_BE_OBJECT)
*
* @example
* ```typescript
Expand Down Expand Up @@ -161,8 +127,8 @@ export function filterByKeyInclusion<T extends Readonly<object>>(data: Readonly<
* @param predicate - The predicate function to apply to the values.
* @param excludeNullUndefined - Flag to determine if properties with null or undefined values should be excluded. Default is false.
*
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link GLOBAL_ERRORS.DATA_IS_NULL})
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link GLOBAL_ERRORS.DATA_MUST_BE_OBJECT})
* @throws ({@link BasaltError}) - Throws an error if the data is null or undefined. ({@link CORE_DATA_KEY_ERROR}.DATA_IS_NULL)
* @throws ({@link BasaltError}) - Throws an error if the data is not a plain object. ({@link CORE_DATA_KEY_ERROR}.DATA_MUST_BE_OBJECT)
*
* @example
* ```typescript
Expand Down Expand Up @@ -199,8 +165,8 @@ export function filterByValue<T extends Readonly<object>> (data: Readonly<T>, pr
* @param data - The object whose keys are to be transformed.
* @param transformer - The key transformation strategy to use.
*
* @throws ({@link BasaltError}) - If the provided data object is null or undefined. ({@link GLOBAL_ERRORS.DATA_IS_NULL})
* @throws ({@link BasaltError}) - If the provided data object is not a plain object. ({@link GLOBAL_ERRORS.DATA_MUST_BE_OBJECT})
* @throws ({@link BasaltError}) - If the provided data object is null or undefined. ({@link CORE_DATA_KEY_ERROR}.DATA_IS_NULL)
* @throws ({@link BasaltError}) - If the provided data object is not a plain object. ({@link CORE_DATA_KEY_ERROR}.DATA_MUST_BE_OBJECT)
*
* @example
* ```typescript
Expand Down
1 change: 1 addition & 0 deletions source/core/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './data';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BasaltKeyTransformer } from '#/common/type/data/basaltKeyTransformer.ts';
import type { BasaltKeyTransformer } from '#/types/data/basaltKeyTransformer';

/**
* Transforms string keys into camelCase format.
Expand All @@ -24,7 +24,7 @@ export class BasaltCamelCaseTransformer implements BasaltKeyTransformer {
*/
public transformKey(key: string): string {
return key
.replace(/(?:[-_][a-z])/giu, (group: string) => (group[1] as string).toUpperCase())
.replace(/(?:[-_][a-z])/giu, (group: string) => (group[1]).toUpperCase())
.replace(/^[A-Z]/u, (firstLetter: string) => firstLetter.toLowerCase());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BasaltKeyTransformer } from '#/common/type/data/basaltKeyTransformer.ts';
import type { BasaltKeyTransformer } from '#/types/data/basaltKeyTransformer';

/**
* Transforms string keys into kebab-case format.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BasaltKeyTransformer } from '#/common/type/data/basaltKeyTransformer.ts';
import type { BasaltKeyTransformer } from '#/types/data/basaltKeyTransformer';

/**
* Transforms string keys into PascalCase format.
Expand All @@ -23,7 +23,7 @@ export class BasaltPascalCaseTransformer implements BasaltKeyTransformer {
* Returns "MyLongKeyName"
*/
public transformKey(key: string): string {
const camelCaseKey: string = key.replace(/(?:[-_][a-z])/giu, (group: string) => (group[1] as string).toUpperCase());
const camelCaseKey: string = key.replace(/(?:[-_][a-z])/giu, (group: string) => (group[1]).toUpperCase());
return camelCaseKey.charAt(0).toUpperCase() + camelCaseKey.slice(1);
}
}
Loading

0 comments on commit d886f85

Please sign in to comment.