11# @lleon/object-builders
22
3+ Library to easily create object builders, useful for testing or for generating seed data.
4+
5+ This library requires
6+ [ES Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
7+ to work. So be sure your browser or the node version you're using supports proxies.
8+
9+ - [Installation](#installation)
10+ - [fromInterface()](#frominterface--)
11+ - [fromClassObject()](#fromclassobject--)
12+ - [fromClassConstructor()](#fromclassconstructor--)
13+ - [fromFactory()](#fromfactory--)
14+ - [Development](#development)
15+ - [yarn build:commonjs](#yarn-build-commonjs)
16+ - [yarn build:commonjs](#yarn-build-commonjs-1)
17+ - [yarn build:esm](#yarn-build-esm)
18+ - [yarn clean](#yarn-clean)
19+ - [yarn test](#yarn-test)
20+ - [yarn cover](#yarn-cover)
21+ - [yarn lint](#yarn-lint)
22+ - [yarn lint:fix](#yarn-lint-fix)
23+
24+ ## Installation
25+
26+ ```sh
27+ yarn add @lleon/object-builders
28+
29+ # or using npm...
30+ npm install @lleon/object-builders
31+ ```
32+
33+ The project use [husky](https://github.com/typicode/husky) and
34+ [lint-staged](https://github.com/okonet/lint-staged) for linting and fixing possible errors on
35+ source code before commit
36+
37+ ### fromInterface()
38+
39+ Creates a new object builder typed from an interface
40+
41+ ```ts
42+ import { fromInterface } from '@lleon/object-builders';
43+
44+ interface User {
45+ name: string;
46+ }
47+
48+ const user = fromInterface<User>()
49+ .name.set('John')
50+ .build();
51+
52+ console.log(user.name); // prints "John"
53+ ```
54+
55+ ### fromClassObject()
56+
57+ Creates a new object builder from the given class. The properties that can be set are the same
58+ properties as the class has.
59+
60+ When using this builder object is instantiated using `Object.create` so the function constructor is
61+ never called.
62+
63+ This is a good builder when using in conjunction with
64+ [class-transformer](https://www.npmjs.com/package/class-transformer) and
65+ [class-validators](https://www.npmjs.com/package/class-validator)
66+
67+ ```ts
68+ import { fromClassObject } from '@lleon/object-builders';
69+ import { IsString } from 'class-validators';
70+
71+ class User {
72+ @IsString()
73+ name!: string;
74+ }
75+
76+ const user = fromClassObject(User)
77+ .name.set('John')
78+ .build();
79+
80+ console.log(user instanceof User); // prints `true`
81+ console.log(user.name); // prints "John"
82+ ```
83+
84+ ### fromClassConstructor()
85+
86+ Creates a new object builder from the given class. The class must receive an object as unique
87+ argument. In this case the object is instantiated using the `new` operator
88+
89+ ```ts
90+ import { fromClassConstructor } from '@lleon/object-builders';
91+
92+ class User {
93+ givenName: string;
94+ familyName: string;
95+
96+ constructor({ firstName, lastName }: { firstName: string; lastName: string }) {
97+ this.givenName = firstName;
98+ this.familyName = lastName;
99+ }
100+ }
101+
102+ // the available builder properties are the properties that receives the constructor
103+ const user = fromClassConstructor(User)
104+ .firstName.set('John')
105+ .lastName.set('Doe')
106+ .build();
107+
108+ console.log(user instanceof User); // prints `true`
109+ console.log(user.givenName); // prints "John"
110+ console.log(user.familyName); // prints "Doe"
111+ ```
112+
113+ ### fromFactory()
114+
115+ Creates a new builder using the given factory function. Using a factory allow you to return any type
116+ you want not just objects
117+
118+ ```ts
119+ import { fromFactory } from '@lleon/object-builders';
120+
121+ const factory = (properties: Partial<{ userName: string }>): string => {
122+ return properties.userName || '';
123+ };
124+
125+ const userName = fromFactory(factory)
126+ .userName.set('john.doe1')
127+ .build();
128+
129+ console.log(userName === 'john.doe1'); // prints `true`
130+ ```
131+
3132## Development
4133
5134The project use [husky](https://github.com/typicode/husky) and
6135[lint-staged](https://github.com/okonet/lint-staged) for linting and fixing possible errors on
7136source code before commit
8137
9- Git hooks scripts are installed after running `yarn` the first time
138+ ### yarn build
139+
140+ Concurrently run `build:commonjs` and `build:esm`
10141
11142### yarn build:commonjs
12143
13144Compile typescript files from the `src` folder inside the `lib` folder
14145
15146### yarn build:esm
16147
17- Compile typescript files from the `src` folder inside the `esm` folder using es modules
18-
19- ### yarn build
20-
21- Concurrently run both `build:commonjs` and `build:esm`
148+ Compile typescript files from the `src` folder inside the `esm` folder using ES modules
22149
23150### yarn clean
24151
@@ -30,7 +157,7 @@ Remove the following directories/files
30157
31158### yarn test
32159
33- Run tests files inside the `tests` folder that matches the following patterns. Exit with code > 0 on
160+ Run tests files inside the `tests` and `src` folders that matches the following patterns. Exit with code > 0 on
34161error
35162
36163- **\*.test.ts**
0 commit comments