From 8b65f197bd98e475dabf570657cd2279bd81c184 Mon Sep 17 00:00:00 2001 From: Pari Work Temp Date: Fri, 10 Jan 2025 16:41:50 -0500 Subject: [PATCH 1/4] added new field type extractor to reduce code --- src/index.ts | 3 ++- src/types.ts | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/types.ts diff --git a/src/index.ts b/src/index.ts index 0d24480..83c4717 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,4 +3,5 @@ import Form from './forms' export * from './validators' export * from './interfaces' export * from './forms' -export * from './utils' \ No newline at end of file +export * from './utils' +export * from './types' \ No newline at end of file diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..ec73782 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,6 @@ +import { IFormArray, IFormField } from "./interfaces"; + +export type ExtractFormFields = { + [K in keyof T]: T[K] extends IFormField | IFormArray ? T[K] : never; + }; + \ No newline at end of file From 09714076201481d05cd66d574ecb67c152474038 Mon Sep 17 00:00:00 2001 From: Pari Work Temp Date: Fri, 10 Jan 2025 18:22:56 -0500 Subject: [PATCH 2/4] added new type and build --- lib/cjs/index.js | 1 + lib/cjs/types.js | 2 ++ lib/cjs/types/index.d.ts | 1 + lib/cjs/types/index.d.ts.map | 2 +- lib/cjs/types/types.d.ts | 5 +++++ lib/cjs/types/types.d.ts.map | 1 + lib/esm/index.mjs | 1 + lib/esm/types.js | 1 + lib/esm/types/index.d.ts | 1 + lib/esm/types/index.d.ts.map | 2 +- lib/esm/types/types.d.ts | 5 +++++ lib/esm/types/types.d.ts.map | 1 + 12 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 lib/cjs/types.js create mode 100644 lib/cjs/types/types.d.ts create mode 100644 lib/cjs/types/types.d.ts.map create mode 100644 lib/esm/types.js create mode 100644 lib/esm/types/types.d.ts create mode 100644 lib/esm/types/types.d.ts.map diff --git a/lib/cjs/index.js b/lib/cjs/index.js index e29e18a..594f7db 100644 --- a/lib/cjs/index.js +++ b/lib/cjs/index.js @@ -23,3 +23,4 @@ __exportStar(require("./validators"), exports); __exportStar(require("./interfaces"), exports); __exportStar(require("./forms"), exports); __exportStar(require("./utils"), exports); +__exportStar(require("./types"), exports); diff --git a/lib/cjs/types.js b/lib/cjs/types.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/lib/cjs/types.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/lib/cjs/types/index.d.ts b/lib/cjs/types/index.d.ts index d3c6b8f..e615469 100644 --- a/lib/cjs/types/index.d.ts +++ b/lib/cjs/types/index.d.ts @@ -4,4 +4,5 @@ export * from './validators'; export * from './interfaces'; export * from './forms'; export * from './utils'; +export * from './types'; //# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/lib/cjs/types/index.d.ts.map b/lib/cjs/types/index.d.ts.map index 19c20f9..7aceb29 100644 --- a/lib/cjs/types/index.d.ts.map +++ b/lib/cjs/types/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe,IAAI,CAAA;AACnB,OAAO,IAAI,MAAM,SAAS,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe,IAAI,CAAA;AACnB,OAAO,IAAI,MAAM,SAAS,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"} \ No newline at end of file diff --git a/lib/cjs/types/types.d.ts b/lib/cjs/types/types.d.ts new file mode 100644 index 0000000..8c7235d --- /dev/null +++ b/lib/cjs/types/types.d.ts @@ -0,0 +1,5 @@ +import { IFormArray, IFormField } from "./interfaces"; +export type ExtractFormFields = { + [K in keyof T]: T[K] extends IFormField | IFormArray ? T[K] : never; +}; +//# sourceMappingURL=types.d.ts.map \ No newline at end of file diff --git a/lib/cjs/types/types.d.ts.map b/lib/cjs/types/types.d.ts.map new file mode 100644 index 0000000..f080e76 --- /dev/null +++ b/lib/cjs/types/types.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEtD,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI;KAC9B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK;CAC9E,CAAC"} \ No newline at end of file diff --git a/lib/esm/index.mjs b/lib/esm/index.mjs index 555a362..21e8495 100644 --- a/lib/esm/index.mjs +++ b/lib/esm/index.mjs @@ -4,3 +4,4 @@ export * from './validators'; export * from './interfaces'; export * from './forms'; export * from './utils'; +export * from './types'; diff --git a/lib/esm/types.js b/lib/esm/types.js new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/lib/esm/types.js @@ -0,0 +1 @@ +export {}; diff --git a/lib/esm/types/index.d.ts b/lib/esm/types/index.d.ts index d3c6b8f..e615469 100644 --- a/lib/esm/types/index.d.ts +++ b/lib/esm/types/index.d.ts @@ -4,4 +4,5 @@ export * from './validators'; export * from './interfaces'; export * from './forms'; export * from './utils'; +export * from './types'; //# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/lib/esm/types/index.d.ts.map b/lib/esm/types/index.d.ts.map index 19c20f9..7aceb29 100644 --- a/lib/esm/types/index.d.ts.map +++ b/lib/esm/types/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe,IAAI,CAAA;AACnB,OAAO,IAAI,MAAM,SAAS,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe,IAAI,CAAA;AACnB,OAAO,IAAI,MAAM,SAAS,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"} \ No newline at end of file diff --git a/lib/esm/types/types.d.ts b/lib/esm/types/types.d.ts new file mode 100644 index 0000000..8c7235d --- /dev/null +++ b/lib/esm/types/types.d.ts @@ -0,0 +1,5 @@ +import { IFormArray, IFormField } from "./interfaces"; +export type ExtractFormFields = { + [K in keyof T]: T[K] extends IFormField | IFormArray ? T[K] : never; +}; +//# sourceMappingURL=types.d.ts.map \ No newline at end of file diff --git a/lib/esm/types/types.d.ts.map b/lib/esm/types/types.d.ts.map new file mode 100644 index 0000000..f080e76 --- /dev/null +++ b/lib/esm/types/types.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEtD,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI;KAC9B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK;CAC9E,CAAC"} \ No newline at end of file From d4ff877ef65dd54ffc465d869bf7bda0698e630d Mon Sep 17 00:00:00 2001 From: Pari Work Temp Date: Mon, 13 Jan 2025 10:54:53 -0500 Subject: [PATCH 3/4] tests --- test/test-forms.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/test-forms.ts b/test/test-forms.ts index cae689c..1e80ff0 100644 --- a/test/test-forms.ts +++ b/test/test-forms.ts @@ -1,5 +1,5 @@ import * as assert from 'assert' -import { Equals, Expect } from './type-utils' +import { Equals, Expect } from './type-utils'; import { FormArray, FormField, Form } from '../src/forms' import { GetFormFieldNames, IFormArray, IFormField } from '../src/interfaces' @@ -14,6 +14,7 @@ import { DynamicMinDateValidator, UrlValidator, } from '../src/validators' +import { ExtractFormFields } from '../src' type IUserAddressForm = { street: IFormField @@ -429,4 +430,25 @@ describe('Forms', () => { > }) }) + + describe('# ExtractFormFields', () => { + class MyForm extends Form { + static name = new FormField() + static age = new FormField() + } + type TMyForm = MyForm & ExtractFormFields + it('should extract the form fields', () => { + let myForm = new MyForm() as TMyForm + assert.equal(myForm.name instanceof FormField, true) + assert.equal(myForm.age instanceof FormField, true) + }) + it('should have age as FormField', () => { + let myForm = new MyForm() as TMyForm + type AgeFieldType = typeof myForm.age + type ExpectedType = FormField + type isCorrectType = [Expect>] + + + }) + }) }) From 06794af621f9df7000a9ae95333cd2e0a34106ba Mon Sep 17 00:00:00 2001 From: Pari Work Temp Date: Mon, 13 Jan 2025 11:43:58 -0500 Subject: [PATCH 4/4] adding tests --- test/test-forms.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test-forms.ts b/test/test-forms.ts index 1e80ff0..8f379cc 100644 --- a/test/test-forms.ts +++ b/test/test-forms.ts @@ -432,7 +432,7 @@ describe('Forms', () => { }) describe('# ExtractFormFields', () => { - class MyForm extends Form { + class MyForm extends Form> { static name = new FormField() static age = new FormField() } @@ -447,7 +447,6 @@ describe('Forms', () => { type AgeFieldType = typeof myForm.age type ExpectedType = FormField type isCorrectType = [Expect>] - }) })