Skip to content

Commit

Permalink
Added js level utilities
Browse files Browse the repository at this point in the history
Configured prettier
  • Loading branch information
erayhanoglu committed Apr 24, 2024
1 parent cc16f01 commit e4f7fca
Show file tree
Hide file tree
Showing 41 changed files with 3,442 additions and 1,598 deletions.
27 changes: 24 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
node_modules
.vscode
package.json
/node_modules
/coverage
/.idea
/.circleci
/.husky
/.DS_Store
/.nyc_output
/.github
/*.md

# IDEs and editors
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
7 changes: 2 additions & 5 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"printWidth": 120,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"proseWrap": "always"
"arrowParens": "avoid",
"trailingComma": "all"
}
16 changes: 0 additions & 16 deletions jest.config.js

This file was deleted.

18 changes: 18 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
testEnvironment: 'node',
verbose: true,
maxWorkers: '50%',
testMatch: ['<rootDir>/test/**/*.spec.ts'],
transform: {
'^.+\\.m?[tj]sx?$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.json',
},
],
},
transformIgnorePatterns: ['node_modules'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
};
16 changes: 0 additions & 16 deletions jest.nostrict.config.js

This file was deleted.

17 changes: 17 additions & 0 deletions jest.nostrict.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
testEnvironment: 'node',
verbose: true,
maxWorkers: '50%',
testMatch: ['<rootDir>/test/**/*.spec.ts'],
transform: {
'^.+.ts?$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig-nostrict.json',
},
],
},
moduleNameMapper: {
'(\\..+)\\.js': '$1',
},
};
8 changes: 4 additions & 4 deletions lib/combine.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Merges types without merging types of properties.
*/
export type Combine<T1, T2, T3 = {}, T4 = {}> = T1
& Omit<T2, keyof T1>
& Omit<T3, keyof T1 | keyof T2>
& Omit<T4, keyof T1 | keyof T2 | keyof T3>;
export type Combine<T1, T2, T3 = {}, T4 = {}> = T1 &
Omit<T2, keyof T1> &
Omit<T3, keyof T1 | keyof T2> &
Omit<T4, keyof T1 | keyof T2 | keyof T3>;
18 changes: 11 additions & 7 deletions lib/dto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import { IfNever } from './type-check.js';
* @template T - The type of the data being transferred.
*/
export type DTO<T> = {
[K in keyof T as (IfNever<Exclude<T[K], undefined | Function>, never, K>)]:
// Deep process arrays
Exclude<T[K], undefined | null> extends (infer U)[] ? DTO<U>[]
// Do not deep process No-Deep values
: IfNoDeepValue<Exclude<T[K], undefined | null>> extends true ? Exclude<T[K], undefined | null>
// Deep process objects
: DTO<Exclude<T[K], undefined | null>>
[K in keyof T as IfNever<
Exclude<T[K], undefined | Function>,
never,
K
>]: Exclude<T[K], undefined | null> extends (infer U)[] // Deep process arrays
? DTO<U>[]
: // Do not deep process No-Deep values
IfNoDeepValue<Exclude<T[K], undefined | null>> extends true
? Exclude<T[K], undefined | null>
: // Deep process objects
DTO<Exclude<T[K], undefined | null>>;
};

export type PartialDTO<T> = DeepPartial<DTO<T>>;
Expand Down
41 changes: 25 additions & 16 deletions lib/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@ import { Builtin } from './types';
/**
* Returns true if T is excluded from deep operations
*/
export type IfNoDeepValue<T> =
T extends Builtin ? true
: IfAny<T> extends true ? T
: IfTuple<T> extends true ? true
: T extends Function ? true
: IfClass<T> extends true ? true
: T extends Map<any, any> ? true
: T extends ReadonlyMap<any, any> ? true
: T extends WeakMap<any, any> ? true
: T extends Set<any> ? true
: T extends ReadonlySet<any> ? true
: T extends WeakSet<any> ? true
: T extends any[] ? true
: false;

export type IfNoDeepValue<T> = T extends Builtin
? true
: IfAny<T> extends true
? T
: IfTuple<T> extends true
? true
: T extends Function
? true
: IfClass<T> extends true
? true
: T extends Map<any, any>
? true
: T extends ReadonlyMap<any, any>
? true
: T extends WeakMap<any, any>
? true
: T extends Set<any>
? true
: T extends ReadonlySet<any>
? true
: T extends WeakSet<any>
? true
: T extends any[]
? true
: false;

/**
* ValuesOf
* @desc Returns the union type of all the values in a type
*/
export type ValuesOf<T> = T[keyof T];

16 changes: 16 additions & 0 deletions lib/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const noOp = x => x;

module.exports = {
asMutable: noOp,
asDeepMutable: noOp,
asDeeperMutable: noOp,
asReadonly: noOp,
asDeepReadonly: noOp,
asDeeperReadonly: noOp,
asPartial: noOp,
asDeepPartial: noOp,
asDeeperPartial: noOp,
asRequired: noOp,
asDeepRequired: noOp,
asDeeperRequired: noOp,
};
76 changes: 61 additions & 15 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
export * from "./combine";
export * from "./dto";
export * from "./helpers";
export * from "./logical.js";
export * from "./mutable";
export * from "./nullish";
export * from "./omit";
export * from "./omit-never";
export * from "./opaque";
export * from "./partial";
export * from "./pick";
export * from "./readonly";
export * from "./required";
export * from "./type-check";
export * from "./types";
// noinspection JSUnusedGlobalSymbols

import { DeeperMutable, DeepMutable, Mutable } from './mutable';
import { DeeperPartial, DeepPartial } from './partial';
import { DeeperReadonly, DeepReadonly } from './readonly';
import { DeeperRequired, DeepRequired } from './required';

export * from './combine';
export * from './dto';
export * from './helpers';
export * from './logical';
export * from './mutable';
export * from './nullish';
export * from './omit';
export * from './omit-never';
export * from './opaque';
export * from './partial';
export * from './pick';
export * from './readonly';
export * from './required';
export * from './type-check';
export * from './types';

declare function asMutable<T>(x: T): Mutable<T>;

declare function asDeepMutable<T>(x: T): DeepMutable<T>;

declare function asDeeperMutable<T>(x: T): DeeperMutable<T>;

declare function asReadonly<T>(x: T): Readonly<T>;

declare function asDeepReadonly<T>(x: T): DeepReadonly<T>;

declare function asDeeperReadonly<T>(x: T): DeeperReadonly<T>;

declare function asPartial<T>(x: T): Partial<T>;

declare function asDeepPartial<T>(x: T): DeepPartial<T>;

declare function asDeeperPartial<T>(x: T): DeeperPartial<T>;

declare function asRequired<T>(x: T): Required<T>;

declare function asDeepRequired<T>(x: T): DeepRequired<T>;

declare function asDeeperRequired<T>(x: T): DeeperRequired<T>;

export {
asMutable,
asDeepMutable,
asDeeperMutable,
asReadonly,
asDeepReadonly,
asDeeperReadonly,
asPartial,
asDeepPartial,
asDeeperPartial,
asRequired,
asDeepRequired,
asDeeperRequired,
};
Empty file removed lib/index.js
Empty file.
16 changes: 16 additions & 0 deletions lib/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const noOp = x => x;

export {
noOp as asMutable,
noOp as asDeepMutable,
noOp as asDeeperMutable,
noOp as asReadonly,
noOp as asDeepReadonly,
noOp as asDeeperReadonly,
noOp as asPartial,
noOp as asDeepPartial,
noOp as asDeeperPartial,
noOp as asRequired,
noOp as asDeepRequired,
noOp as asDeeperRequired,
};
40 changes: 26 additions & 14 deletions lib/logical.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import { IfNever } from './type-check';

export type And<T1, T2, T3 = true, T4 = true, T5 = true, T6 = true> =
IfNever<Exclude<T1, undefined | null | false>> extends true ? false :
IfNever<Exclude<T2, undefined | null | false>> extends true ? false :
IfNever<Exclude<T3, undefined | null | false>> extends true ? false :
IfNever<Exclude<T4, undefined | null | false>> extends true ? false :
IfNever<Exclude<T5, undefined | null | false>> extends true ? false :
IfNever<Exclude<T6, undefined | null | false>> extends true ? false :
true;
IfNever<Exclude<T1, undefined | null | false>> extends true
? false
: IfNever<Exclude<T2, undefined | null | false>> extends true
? false
: IfNever<Exclude<T3, undefined | null | false>> extends true
? false
: IfNever<Exclude<T4, undefined | null | false>> extends true
? false
: IfNever<Exclude<T5, undefined | null | false>> extends true
? false
: IfNever<Exclude<T6, undefined | null | false>> extends true
? false
: true;

export type Or<T1, T2, T3 = false, T4 = false, T5 = false, T6 = false> =
IfNever<Exclude<T1, undefined | null | false>> extends false ? true :
IfNever<Exclude<T2, undefined | null | false>> extends false ? true :
IfNever<Exclude<T3, undefined | null | false>> extends false ? true :
IfNever<Exclude<T4, undefined | null | false>> extends false ? true :
IfNever<Exclude<T5, undefined | null | false>> extends false ? true :
IfNever<Exclude<T6, undefined | null | false>> extends false ? true :
false;
IfNever<Exclude<T1, undefined | null | false>> extends false
? true
: IfNever<Exclude<T2, undefined | null | false>> extends false
? true
: IfNever<Exclude<T3, undefined | null | false>> extends false
? true
: IfNever<Exclude<T4, undefined | null | false>> extends false
? true
: IfNever<Exclude<T5, undefined | null | false>> extends false
? true
: IfNever<Exclude<T6, undefined | null | false>> extends false
? true
: false;
Loading

0 comments on commit e4f7fca

Please sign in to comment.