diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fbb0458..7bc2626 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -93,6 +93,9 @@ jobs: # build lint plugins before linting - name: Build lint plugins run: npm run build:lint:ci + # check formatting + - name: Check Formatting + run: npm run format:ci # lint - name: Lint run: npm run lint:ci diff --git a/package-lock.json b/package-lock.json index e4755ce..438bb8b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14836,7 +14836,7 @@ }, "packages/branch-utilities": { "name": "@shiftcode/branch-utilities", - "version": "5.0.0", + "version": "5.0.1-pr53.1", "license": "MIT", "engines": { "node": "^20.0.0 || ^22.0.0" @@ -14847,10 +14847,10 @@ }, "packages/eslint-config-recommended": { "name": "@shiftcode/eslint-config-recommended", - "version": "5.0.0", + "version": "5.0.1-pr53.1", "license": "UNLICENSED", "dependencies": { - "@shiftcode/eslint-plugin-rules": "^4.0.0", + "@shiftcode/eslint-plugin-rules": "^4.0.1-pr53.1", "@typescript-eslint/eslint-plugin": "^8.18.0", "@typescript-eslint/parser": "^8.18.0", "eslint": "^8.56.0", @@ -14888,7 +14888,7 @@ }, "packages/eslint-plugin-rules": { "name": "@shiftcode/eslint-plugin-rules", - "version": "4.0.0", + "version": "4.0.1-pr53.1", "license": "UNLICENSED", "dependencies": { "@eslint/eslintrc": "^2.1.4", @@ -14907,21 +14907,21 @@ }, "packages/logger": { "name": "@shiftcode/logger", - "version": "3.0.0", + "version": "3.0.1-pr53.5", "license": "UNLICENSED", "devDependencies": { - "@shiftcode/utilities": "^4.0.0" + "@shiftcode/utilities": "^4.1.0-pr53.5" }, "engines": { "node": "^20.0.0 || ^22.0.0" }, "peerDependencies": { - "@shiftcode/utilities": "^4.0.0 || ^4.0.0-pr45" + "@shiftcode/utilities": "^4.0.0 || ^4.0.0-pr53" } }, "packages/publish-helper": { "name": "@shiftcode/publish-helper", - "version": "4.1.0", + "version": "4.1.1-pr53.1", "license": "MIT", "dependencies": { "conventional-changelog-angular": "^8.0.0", @@ -14932,7 +14932,7 @@ "publish-lib": "dist/publish-lib.js" }, "devDependencies": { - "@shiftcode/branch-utilities": "^5.0.0", + "@shiftcode/branch-utilities": "^5.0.1-pr53.1", "@types/yargs": "^17.0.32" }, "engines": { @@ -14957,7 +14957,7 @@ }, "packages/utilities": { "name": "@shiftcode/utilities", - "version": "4.0.0", + "version": "4.1.0-pr53.5", "license": "MIT", "engines": { "node": "^20.0.0 || ^22.0.0" diff --git a/package.json b/package.json index 17ba8b8..1b203e0 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,10 @@ "scripts": { "build:ci": "lerna run build", "build:lint:ci": "lerna run build:lint:ci", + "format": "lerna run format", + "format:ci": "lerna run format:ci", "lint:ci": "lerna run lint:ci", "prepare": "husky", - "prettier": "lerna run prettier", "publish-libs": "node packages/publish-helper/dist/publish-lib", "test:ci": "lerna run test:ci" }, diff --git a/packages/branch-utilities/.lintstagedrc.yml b/packages/branch-utilities/.lintstagedrc.yml index bf45dc5..a033e03 100644 --- a/packages/branch-utilities/.lintstagedrc.yml +++ b/packages/branch-utilities/.lintstagedrc.yml @@ -1,7 +1,6 @@ -# we use eslint instead of ng lint, because we can't use --project (tsconfig specifying files with include, exclude) -# and --files argument pointing to other files -src/{**/,}*.ts,test/{**/,}*.ts: - - npm run prettier:staged +# Reformat all .ts / .js +"**/*.(t|j)s": + - npm run format:staged - npm run lint:staged # sort package.json keys diff --git a/packages/branch-utilities/package.json b/packages/branch-utilities/package.json index 093b880..a1f2c49 100644 --- a/packages/branch-utilities/package.json +++ b/packages/branch-utilities/package.json @@ -1,6 +1,6 @@ { "name": "@shiftcode/branch-utilities", - "version": "5.0.0", + "version": "5.0.1-pr53.1", "description": "Utilities for local and ci to get branch name and stage", "repository": "https://github.com/shiftcode/sc-commons-public", "license": "MIT", @@ -16,13 +16,15 @@ "scripts": { "prebuild": "rm -rf ./dist", "build": "tsc", + "format": "npm run format:base -- --write", + "format:base": "prettier --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", + "format:ci": "npm run format:base -- --check", + "format:staged": "prettier --write --config ../../.prettierrc.yml", "lint": "npm run lint:src:fix", "lint:ci": "npm run lint:src", "lint:src": "eslint ./src", "lint:src:fix": "eslint ./src --cache --fix", "lint:staged": "eslint --fix --cache", - "prettier": "prettier --write --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", - "prettier:staged": "prettier --write --config ../../.prettierrc.yml", "prepublish": "node ../publish-helper/dist/prepare-dist.js", "test": "NODE_OPTIONS=\"--experimental-vm-modules --trace-warnings\" npx jest --config jest.config.js", "test:ci": "npm run test", diff --git a/packages/branch-utilities/src/lib/base.utils.spec.ts b/packages/branch-utilities/src/lib/base.utils.spec.ts index e57ca14..befd57d 100644 --- a/packages/branch-utilities/src/lib/base.utils.spec.ts +++ b/packages/branch-utilities/src/lib/base.utils.spec.ts @@ -81,9 +81,10 @@ describe('base utils', () => { }) describe('parseBranchName', () => { - test('works when valid pattern', () => { - expect(parseBranchName('#7-abc')).toEqual({ branchId: 7, branchName: 'abc' } satisfies ReturnType) + expect(parseBranchName('#7-abc')).toEqual({ branchId: 7, branchName: 'abc' } satisfies ReturnType< + typeof parseBranchName + >) expect(parseBranchName('#72-abc').branchId).toBe(72) expect(parseBranchName('#000-int').branchId).toBe(0) @@ -94,8 +95,12 @@ describe('base utils', () => { }) test('works for github copilot created branches', () => { - expect(parseBranchName('copilot/fix-123')).toEqual({ branchId: 123, branchName: 'fix' } satisfies ReturnType) - expect(parseBranchName('copilot/feat-123')).toEqual({ branchId: 123, branchName: 'feat' } satisfies ReturnType) + expect(parseBranchName('copilot/fix-123')).toEqual({ branchId: 123, branchName: 'fix' } satisfies ReturnType< + typeof parseBranchName + >) + expect(parseBranchName('copilot/feat-123')).toEqual({ branchId: 123, branchName: 'feat' } satisfies ReturnType< + typeof parseBranchName + >) }) test('throws when invalid pattern', () => { diff --git a/packages/branch-utilities/src/lib/base.utils.ts b/packages/branch-utilities/src/lib/base.utils.ts index 5f39748..c906fcb 100644 --- a/packages/branch-utilities/src/lib/base.utils.ts +++ b/packages/branch-utilities/src/lib/base.utils.ts @@ -90,9 +90,11 @@ export function getBranchInfo(env: unknown, branchName?: string): BranchInfo { } export function isFullBranchOverrideDefined(envVars: unknown): envVars is CustomScOverrideEnv { - return envVars !== null - && typeof (envVars as CustomScOverrideEnv).SC_OVERRIDE_BRANCH_NAME ==='string' - && typeof (envVars as CustomScOverrideEnv).SC_OVERRIDE_IS_PR ==='string' + return ( + envVars !== null && + typeof (envVars as CustomScOverrideEnv).SC_OVERRIDE_BRANCH_NAME === 'string' && + typeof (envVars as CustomScOverrideEnv).SC_OVERRIDE_IS_PR === 'string' + ) } export function getBranchNameOverride(env: CustomScOverrideEnv): string { diff --git a/packages/eslint-config-recommended/.lintstagedrc.yml b/packages/eslint-config-recommended/.lintstagedrc.yml index 69f89f4..bb9649d 100644 --- a/packages/eslint-config-recommended/.lintstagedrc.yml +++ b/packages/eslint-config-recommended/.lintstagedrc.yml @@ -1,7 +1,6 @@ -# we use eslint instead of ng lint, because we can't use --project (tsconfig specifying files with include, exclude) -# and --files argument pointing to other files -src/{**/,}*.ts,test/{**/,}*.ts: - - npm run prettier:staged +# Reformat all .ts / .js +"**/*.(t|j)s": + - npm run format:staged - npm run lint:staged # sort package.json keys diff --git a/packages/eslint-config-recommended/package.json b/packages/eslint-config-recommended/package.json index 4e9909b..b63127e 100644 --- a/packages/eslint-config-recommended/package.json +++ b/packages/eslint-config-recommended/package.json @@ -1,6 +1,6 @@ { "name": "@shiftcode/eslint-config-recommended", - "version": "5.0.0", + "version": "5.0.1-pr53.1", "description": "default shiftcode config for eslint", "repository": "https://github.com/shiftcode/sc-commons-public", "license": "UNLICENSED", @@ -24,19 +24,21 @@ "prebuild": "rm -rf ./dist", "build": "tsc", "build:lint:ci": "npm run build", + "format": "npm run format:base -- --write", + "format:base": "prettier --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", + "format:ci": "npm run format:base -- --check", + "format:staged": "prettier --write --config ../../.prettierrc.yml", "lint": "npm run lint:src:fix", "lint:ci": "npm run lint:src", "lint:src": "eslint ./src", "lint:src:fix": "eslint ./src --cache --fix", "lint:staged": "eslint --fix --cache", - "prettier": "prettier --write --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", - "prettier:staged": "prettier --write --config ../../.prettierrc.yml", "prepublish": "node ../publish-helper/dist/prepare-dist.js", "test": "echo 'Error: no test specificed'", "test:ci": "npm run test" }, "dependencies": { - "@shiftcode/eslint-plugin-rules": "^4.0.0", + "@shiftcode/eslint-plugin-rules": "^4.0.1-pr53.1", "@typescript-eslint/eslint-plugin": "^8.18.0", "@typescript-eslint/parser": "^8.18.0", "eslint": "^8.56.0", diff --git a/packages/eslint-plugin-rules/.lintstagedrc.yml b/packages/eslint-plugin-rules/.lintstagedrc.yml index bf45dc5..a033e03 100644 --- a/packages/eslint-plugin-rules/.lintstagedrc.yml +++ b/packages/eslint-plugin-rules/.lintstagedrc.yml @@ -1,7 +1,6 @@ -# we use eslint instead of ng lint, because we can't use --project (tsconfig specifying files with include, exclude) -# and --files argument pointing to other files -src/{**/,}*.ts,test/{**/,}*.ts: - - npm run prettier:staged +# Reformat all .ts / .js +"**/*.(t|j)s": + - npm run format:staged - npm run lint:staged # sort package.json keys diff --git a/packages/eslint-plugin-rules/package.json b/packages/eslint-plugin-rules/package.json index d587976..d446a75 100644 --- a/packages/eslint-plugin-rules/package.json +++ b/packages/eslint-plugin-rules/package.json @@ -1,6 +1,6 @@ { "name": "@shiftcode/eslint-plugin-rules", - "version": "4.0.0", + "version": "4.0.1-pr53.1", "description": "eslint-rules for shiftcode-specific eslint rules", "repository": "https://github.com/shiftcode/sc-commons-public", "license": "UNLICENSED", @@ -14,13 +14,15 @@ "prebuild": "rm -rf ./dist", "build": "tsc", "build:lint:ci": "npm run build", + "format": "npm run format:base -- --write", + "format:base": "prettier --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", + "format:ci": "npm run format:base -- --check", + "format:staged": "prettier --write --config ../../.prettierrc.yml", "lint": "npm run lint:src:fix", "lint:ci": "npm run lint:src", "lint:src": "eslint ./src", "lint:src:fix": "eslint ./src --cache --fix", "lint:staged": "eslint --fix --cache", - "prettier": "prettier --write --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", - "prettier:staged": "prettier --write --config ../../.prettierrc.yml", "prepublish": "node ../publish-helper/dist/prepare-dist.js", "test": "jest", "test:ci": "npm run test" diff --git a/packages/logger/.lintstagedrc.yml b/packages/logger/.lintstagedrc.yml index 3830437..a033e03 100644 --- a/packages/logger/.lintstagedrc.yml +++ b/packages/logger/.lintstagedrc.yml @@ -1,6 +1,6 @@ # Reformat all .ts / .js "**/*.(t|j)s": - - npm run prettier:staged + - npm run format:staged - npm run lint:staged # sort package.json keys diff --git a/packages/logger/package.json b/packages/logger/package.json index 5f42e0d..44d0330 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -1,6 +1,6 @@ { "name": "@shiftcode/logger", - "version": "3.0.0", + "version": "3.0.1-pr53.5", "description": "logger for local and aws lambda execution", "repository": "https://github.com/shiftcode/sc-commons-public", "license": "UNLICENSED", @@ -20,6 +20,10 @@ "scripts": { "prebuild": "rm -rf ./dist", "build": "tsc", + "format": "npm run format:base -- --write", + "format:base": "prettier --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", + "format:ci": "npm run format:base -- --check", + "format:staged": "prettier --write --config ../../.prettierrc.yml", "lint": "npm run lint:src:fix && npm run lint:test:fix", "lint:ci": "npm run lint:src && npm run lint:test", "lint:src": "eslint ./src", @@ -27,18 +31,16 @@ "lint:staged": "npm run lint", "lint:test": "eslint ./test", "lint:test:fix": "eslint ./test --cache --fix", - "prettier": "prettier --write --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", - "prettier:staged": "prettier --write --config ../../.prettierrc.yml", "prepublish": "node ../publish-helper/dist/prepare-dist.js", "test": "NODE_OPTIONS=\"--experimental-vm-modules --trace-warnings\" npx jest --config jest.config.js", "test:ci": "npm run test", "test:watch": "npm run test -- --watch" }, "devDependencies": { - "@shiftcode/utilities": "^4.0.0" + "@shiftcode/utilities": "^4.1.0-pr53.5" }, "peerDependencies": { - "@shiftcode/utilities": "^4.0.0 || ^4.0.0-pr45" + "@shiftcode/utilities": "^4.0.0 || ^4.0.0-pr53" }, "engines": { "node": "^20.0.0 || ^22.0.0" diff --git a/packages/publish-helper/.lintstagedrc.yml b/packages/publish-helper/.lintstagedrc.yml index bf45dc5..a033e03 100644 --- a/packages/publish-helper/.lintstagedrc.yml +++ b/packages/publish-helper/.lintstagedrc.yml @@ -1,7 +1,6 @@ -# we use eslint instead of ng lint, because we can't use --project (tsconfig specifying files with include, exclude) -# and --files argument pointing to other files -src/{**/,}*.ts,test/{**/,}*.ts: - - npm run prettier:staged +# Reformat all .ts / .js +"**/*.(t|j)s": + - npm run format:staged - npm run lint:staged # sort package.json keys diff --git a/packages/publish-helper/package.json b/packages/publish-helper/package.json index e8d6060..c2f570a 100644 --- a/packages/publish-helper/package.json +++ b/packages/publish-helper/package.json @@ -1,6 +1,6 @@ { "name": "@shiftcode/publish-helper", - "version": "4.1.0", + "version": "4.1.1-pr53.1", "description": "scripts for conventional (pre)releases", "repository": "https://github.com/shiftcode/sc-commons-public", "license": "MIT", @@ -13,13 +13,15 @@ "scripts": { "prebuild": "rm -rf ./dist", "build": "tsc", + "format": "npm run format:base -- --write", + "format:base": "prettier --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", + "format:ci": "npm run format:base -- --check", + "format:staged": "prettier --write --config ../../.prettierrc.yml", "lint": "npm run lint:src:fix", "lint:ci": "npm run lint:src", "lint:src": "eslint ./src", "lint:src:fix": "eslint ./src --cache --fix", "lint:staged": "eslint --fix --cache", - "prettier": "prettier --write --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", - "prettier:staged": "prettier --write --config ../../.prettierrc.yml", "prepublish": "node ./dist/prepare-dist.js", "test": "NODE_OPTIONS=\"--experimental-vm-modules --trace-warnings\" npx jest --config jest.config.js --passWithNoTests", "test:ci": "npm run test", @@ -30,7 +32,7 @@ "yargs": "^17.7.2" }, "devDependencies": { - "@shiftcode/branch-utilities": "^5.0.0", + "@shiftcode/branch-utilities": "^5.0.1-pr53.1", "@types/yargs": "^17.0.32" }, "peerDependencies": { diff --git a/packages/publish-helper/src/prepare-dist.ts b/packages/publish-helper/src/prepare-dist.ts index 4d10f90..2685a5e 100644 --- a/packages/publish-helper/src/prepare-dist.ts +++ b/packages/publish-helper/src/prepare-dist.ts @@ -18,7 +18,7 @@ const argv = yargs(hideBin(process.argv)).option('include', { type: 'array', description: 'copy additional files into the dist folder', default: [], - string: true + string: true, }).argv function log(...args: any[]) { diff --git a/packages/utilities/.lintstagedrc.yml b/packages/utilities/.lintstagedrc.yml index bf45dc5..a033e03 100644 --- a/packages/utilities/.lintstagedrc.yml +++ b/packages/utilities/.lintstagedrc.yml @@ -1,7 +1,6 @@ -# we use eslint instead of ng lint, because we can't use --project (tsconfig specifying files with include, exclude) -# and --files argument pointing to other files -src/{**/,}*.ts,test/{**/,}*.ts: - - npm run prettier:staged +# Reformat all .ts / .js +"**/*.(t|j)s": + - npm run format:staged - npm run lint:staged # sort package.json keys diff --git a/packages/utilities/package.json b/packages/utilities/package.json index 80bb2ab..162f5b4 100644 --- a/packages/utilities/package.json +++ b/packages/utilities/package.json @@ -1,6 +1,6 @@ { "name": "@shiftcode/utilities", - "version": "4.0.0", + "version": "4.1.0-pr53.5", "description": "Contains some utilities", "repository": "https://github.com/shiftcode/sc-commons-public", "license": "MIT", @@ -16,13 +16,15 @@ "scripts": { "prebuild": "rm -rf ./dist", "build": "tsc", + "format": "npm run format:base -- --write", + "format:base": "prettier --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", + "format:ci": "npm run format:base -- --check", + "format:staged": "prettier --write --config ../../.prettierrc.yml", "lint": "npm run lint:src:fix", "lint:ci": "npm run lint:src", "lint:src": "eslint ./src", "lint:src:fix": "eslint ./src --cache --fix", "lint:staged": "eslint --fix --cache", - "prettier": "prettier --write --config ../../.prettierrc.yml '{src,e2e,test}/**/*.ts'", - "prettier:staged": "prettier --write --config ../../.prettierrc.yml", "prepublish": "node ../publish-helper/dist/prepare-dist.js", "test": "NODE_OPTIONS=\"--experimental-vm-modules --trace-warnings\" npx jest --config jest.config.js", "test:ci": "npm run test", diff --git a/packages/utilities/src/index.ts b/packages/utilities/src/index.ts index 417e7f5..0748aee 100644 --- a/packages/utilities/src/index.ts +++ b/packages/utilities/src/index.ts @@ -19,3 +19,10 @@ export * from './lib/type-utils/nullable.type.js' export * from './lib/type-utils/union-to-intersection.type.js' export * from './lib/string/words.js' export * from './lib/string/capitalize.function.js' +export * from './lib/ts-guards/is-array.js' +export * from './lib/ts-guards/is-boolean.js' +export * from './lib/ts-guards/is-class.js' +export * from './lib/ts-guards/is-date.js' +export * from './lib/ts-guards/is-defined.js' +export * from './lib/ts-guards/is-number.js' +export * from './lib/ts-guards/is-string.js' diff --git a/packages/utilities/src/lib/ts-guards/is-array.spec.ts b/packages/utilities/src/lib/ts-guards/is-array.spec.ts new file mode 100644 index 0000000..02e36b1 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-array.spec.ts @@ -0,0 +1,23 @@ +import { isArray } from './is-array.js' + +describe('isArray', () => { + it('should return true for array values', () => { + expect(isArray([])).toBe(true) + expect(isArray([1, 2, 3])).toBe(true) + expect(isArray(['a', 'b', 'c'])).toBe(true) + expect(isArray([null, undefined])).toBe(true) + // eslint-disable-next-line @typescript-eslint/no-array-constructor + expect(isArray(new Array())).toBe(true) + }) + + it('should return false for non-array values', () => { + expect(isArray('array')).toBe(false) + expect(isArray(123)).toBe(false) + expect(isArray(true)).toBe(false) + expect(isArray(null)).toBe(false) + expect(isArray(undefined)).toBe(false) + expect(isArray({})).toBe(false) + expect(isArray(new Date())).toBe(false) + expect(isArray({ length: 0 })).toBe(false) + }) +}) diff --git a/packages/utilities/src/lib/ts-guards/is-array.ts b/packages/utilities/src/lib/ts-guards/is-array.ts new file mode 100644 index 0000000..b783132 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-array.ts @@ -0,0 +1,3 @@ +export function isArray(value: unknown): value is unknown[] { + return Array.isArray(value) +} diff --git a/packages/utilities/src/lib/ts-guards/is-boolean.spec.ts b/packages/utilities/src/lib/ts-guards/is-boolean.spec.ts new file mode 100644 index 0000000..520d1f6 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-boolean.spec.ts @@ -0,0 +1,20 @@ +import { isBoolean } from './is-boolean.js' + +describe('isBoolean', () => { + it('should return true for boolean values', () => { + expect(isBoolean(true)).toBe(true) + expect(isBoolean(false)).toBe(true) + }) + + it('should return false for non-boolean values', () => { + expect(isBoolean(1)).toBe(false) + expect(isBoolean(0)).toBe(false) + expect(isBoolean('true')).toBe(false) + expect(isBoolean('false')).toBe(false) + expect(isBoolean(null)).toBe(false) + expect(isBoolean(undefined)).toBe(false) + expect(isBoolean([])).toBe(false) + expect(isBoolean({})).toBe(false) + expect(isBoolean(new Date())).toBe(false) + }) +}) diff --git a/packages/utilities/src/lib/ts-guards/is-boolean.ts b/packages/utilities/src/lib/ts-guards/is-boolean.ts new file mode 100644 index 0000000..9e60021 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-boolean.ts @@ -0,0 +1,3 @@ +export function isBoolean(value: unknown): value is boolean { + return typeof value === 'boolean' +} diff --git a/packages/utilities/src/lib/ts-guards/is-class.spec.ts b/packages/utilities/src/lib/ts-guards/is-class.spec.ts new file mode 100644 index 0000000..93356e0 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-class.spec.ts @@ -0,0 +1,25 @@ +import { isClass } from './is-class.js' + +class TestClass { + constructor() {} + + x() {} +} + +describe('isClass', () => { + it('should return true for classes', () => { + expect(isClass(TestClass)).toBe(true) + }) + + it('should return false for non-class values', () => { + expect(isClass('string')).toBe(false) + expect(isClass(123)).toBe(false) + expect(isClass(true)).toBe(false) + expect(isClass(null)).toBe(false) + expect(isClass(undefined)).toBe(false) + expect(isClass([])).toBe(false) + expect(isClass({})).toBe(false) + expect(isClass(function y() {})).toBe(false) + expect(isClass(new TestClass().x)).toBe(false) + }) +}) diff --git a/packages/utilities/src/lib/ts-guards/is-class.ts b/packages/utilities/src/lib/ts-guards/is-class.ts new file mode 100644 index 0000000..49bcd38 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-class.ts @@ -0,0 +1,5 @@ +export function isClass any>(v: T): v is T +export function isClass(value: unknown): value is new (...args: any[]) => any +export function isClass(value: unknown): value is new (...args: any[]) => any { + return typeof value === 'function' && /^\s*class\s+/.test(value.toString()) +} diff --git a/packages/utilities/src/lib/ts-guards/is-date.spec.ts b/packages/utilities/src/lib/ts-guards/is-date.spec.ts new file mode 100644 index 0000000..4026291 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-date.spec.ts @@ -0,0 +1,22 @@ +import { isDate } from './is-date.js' + +describe('isDate', () => { + it('should return true for Date objects', () => { + expect(isDate(new Date())).toBe(true) + expect(isDate(new Date('2023-01-01'))).toBe(true) + expect(isDate(new Date(2023, 0, 1))).toBe(true) + expect(isDate(new Date(Date.now()))).toBe(true) + }) + + it('should return false for non-Date values', () => { + expect(isDate('2023-01-01')).toBe(false) + expect(isDate(1672531200000)).toBe(false) + expect(isDate(true)).toBe(false) + expect(isDate(false)).toBe(false) + expect(isDate(null)).toBe(false) + expect(isDate(undefined)).toBe(false) + expect(isDate([])).toBe(false) + expect(isDate({})).toBe(false) + expect(isDate('hello')).toBe(false) + }) +}) diff --git a/packages/utilities/src/lib/ts-guards/is-date.ts b/packages/utilities/src/lib/ts-guards/is-date.ts new file mode 100644 index 0000000..e698d74 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-date.ts @@ -0,0 +1,3 @@ +export function isDate(value: unknown): value is Date { + return value instanceof Date +} diff --git a/packages/utilities/src/lib/ts-guards/is-defined.spec.ts b/packages/utilities/src/lib/ts-guards/is-defined.spec.ts new file mode 100644 index 0000000..8e54c8c --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-defined.spec.ts @@ -0,0 +1,18 @@ +import { isDefined } from './is-defined.js' + +describe('isDefined', () => { + it('should return true for defined values', () => { + expect(isDefined('')).toBe(true) + expect(isDefined(0)).toBe(true) + expect(isDefined(false)).toBe(true) + expect(isDefined([])).toBe(true) + expect(isDefined({})).toBe(true) + expect(isDefined('hello')).toBe(true) + expect(isDefined(123)).toBe(true) + }) + + it('should return false for undefined values', () => { + expect(isDefined(undefined)).toBe(false) + expect(isDefined(null)).toBe(false) + }) +}) diff --git a/packages/utilities/src/lib/ts-guards/is-defined.ts b/packages/utilities/src/lib/ts-guards/is-defined.ts new file mode 100644 index 0000000..8ccf63b --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-defined.ts @@ -0,0 +1,6 @@ +/** + * Checks if a value is defined (not undefined or null). + */ +export function isDefined(value: T | undefined | null): value is T { + return value !== undefined && value !== null +} diff --git a/packages/utilities/src/lib/ts-guards/is-number.spec.ts b/packages/utilities/src/lib/ts-guards/is-number.spec.ts new file mode 100644 index 0000000..2061ea1 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-number.spec.ts @@ -0,0 +1,27 @@ +import { isNumber } from './is-number.js' + +describe('isNumber', () => { + it('should return true for valid number values', () => { + expect(isNumber(0)).toBe(true) + expect(isNumber(123)).toBe(true) + expect(isNumber(-456)).toBe(true) + expect(isNumber(3.14)).toBe(true) + expect(isNumber(Infinity)).toBe(true) + expect(isNumber(-Infinity)).toBe(true) + }) + + it('should return false for NaN', () => { + expect(isNumber(NaN)).toBe(false) + }) + + it('should return false for non-number values', () => { + expect(isNumber('123')).toBe(false) + expect(isNumber(true)).toBe(false) + expect(isNumber(false)).toBe(false) + expect(isNumber(null)).toBe(false) + expect(isNumber(undefined)).toBe(false) + expect(isNumber([])).toBe(false) + expect(isNumber({})).toBe(false) + expect(isNumber(new Date())).toBe(false) + }) +}) diff --git a/packages/utilities/src/lib/ts-guards/is-number.ts b/packages/utilities/src/lib/ts-guards/is-number.ts new file mode 100644 index 0000000..432eac2 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-number.ts @@ -0,0 +1,6 @@ +/** + * Checks if the provided value is a number. (NaN is technically a number, but this function returns false for NaN.) + */ +export function isNumber(value: unknown): value is number { + return typeof value === 'number' && !isNaN(value) +} diff --git a/packages/utilities/src/lib/ts-guards/is-string.spec.ts b/packages/utilities/src/lib/ts-guards/is-string.spec.ts new file mode 100644 index 0000000..75d4787 --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-string.spec.ts @@ -0,0 +1,21 @@ +import { isString } from './is-string.js' + +describe('isString', () => { + it('should return true for string values', () => { + expect(isString('')).toBe(true) + expect(isString('hello')).toBe(true) + expect(isString('123')).toBe(true) + expect(isString(' ')).toBe(true) + }) + + it('should return false for non-string values', () => { + expect(isString(123)).toBe(false) + expect(isString(true)).toBe(false) + expect(isString(false)).toBe(false) + expect(isString(null)).toBe(false) + expect(isString(undefined)).toBe(false) + expect(isString([])).toBe(false) + expect(isString({})).toBe(false) + expect(isString(new Date())).toBe(false) + }) +}) diff --git a/packages/utilities/src/lib/ts-guards/is-string.ts b/packages/utilities/src/lib/ts-guards/is-string.ts new file mode 100644 index 0000000..3c257ba --- /dev/null +++ b/packages/utilities/src/lib/ts-guards/is-string.ts @@ -0,0 +1,3 @@ +export function isString(value: unknown): value is string { + return typeof value === 'string' +}