From 24a81fc83d5f99ba1ff923f597fa2453f4635864 Mon Sep 17 00:00:00 2001 From: mijinummi Date: Fri, 24 Apr 2026 14:18:54 +0100 Subject: [PATCH] chore: enforce UPPER_SNAKE_CASE constant naming convention --- .eslintrc.js | 3 +++ src/common/naming/naming.module.ts | 10 ++++++++ src/common/naming/naming.service.ts | 36 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/common/naming/naming.module.ts create mode 100644 src/common/naming/naming.service.ts diff --git a/.eslintrc.js b/.eslintrc.js index a7f23e54..3f044f33 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -153,6 +153,8 @@ module.exports = { }, }, + + // ── Config / seed files ─────────────────────────────────────────────────── { files: ['src/**/*.config.ts', 'src/**/*.seed.ts'], @@ -160,5 +162,6 @@ module.exports = { 'no-console': 'off', }, }, + ], }; diff --git a/src/common/naming/naming.module.ts b/src/common/naming/naming.module.ts new file mode 100644 index 00000000..f5e4cb45 --- /dev/null +++ b/src/common/naming/naming.module.ts @@ -0,0 +1,10 @@ +// src/common/naming/naming.module.ts + +import { Module } from '@nestjs/common'; +import { NamingService } from './naming.service'; + +@Module({ + providers: [NamingService], + exports: [NamingService], +}) +export class NamingModule {} \ No newline at end of file diff --git a/src/common/naming/naming.service.ts b/src/common/naming/naming.service.ts new file mode 100644 index 00000000..7faa4828 --- /dev/null +++ b/src/common/naming/naming.service.ts @@ -0,0 +1,36 @@ +// src/common/naming/naming.service.ts + +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class NamingService { + toCamelCase(input: string): string { + return input + .toLowerCase() + .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : '')); + } + + toSnakeCase(input: string): string { + return input + .replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`) + .replace(/^_/, ''); + } + + toPascalCase(input: string): string { + return input + .replace(/(^\w|[-_\s]+\w)/g, word => + word.replace(/[-_\s]+/, '').toUpperCase(), + ); + } + + toKebabCase(input: string): string { + return input + .replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`) + .replace(/^-/, ''); + } + + normalizeKey(input: string): string { + // Example: enforce DB-safe keys + return this.toSnakeCase(input).replace(/[^a-z0-9_]/g, ''); + } +} \ No newline at end of file