Skip to content

Commit

Permalink
feat: use new typescript features to create setX y unsetX methods
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the properies are no longer set/unset using
`property.set()` or `property.unset()` methods. New methods follow the
convention `setProperty()` and `unsetProperty()`

BREAKING CHANGE: remove undocumented lazy constructor helper
  • Loading branch information
leon19 committed Nov 28, 2020
1 parent fad1180 commit 8b49e33
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 977 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@
/reports/
*.log
/*.tgz
/tsconfig.tsbuildinfo
19 changes: 5 additions & 14 deletions README.md
Expand Up @@ -47,17 +47,15 @@ interface User {
name: string;
}

const user = fromInterface<User>()
.name.set('John')
.build();
const user = fromInterface<User>().setName('John').build();

console.log(user.name); // prints "John"
```

### fromClassObject()

Creates a new object builder from the given class. The properties that can be set are the same
properties as the class has.
properties as the class has excluding methods.

When using this builder object is instantiated using `Object.create` so the function constructor is
never called.
Expand All @@ -75,9 +73,7 @@ class User {
name!: string;
}

const user = fromClassObject(User)
.name.set('John')
.build();
const user = fromClassObject(User).setName('John').build();

console.log(user instanceof User); // prints `true`
console.log(user.name); // prints "John"
Expand All @@ -102,10 +98,7 @@ class User {
}

// the available builder properties are the properties that receives the constructor
const user = fromClassConstructor(User)
.firstName.set('John')
.lastName.set('Doe')
.build();
const user = fromClassConstructor(User).setFirstName('John').setLastName('Doe').build();

console.log(user instanceof User); // prints `true`
console.log(user.givenName); // prints "John"
Expand All @@ -124,9 +117,7 @@ const factory = (properties: Partial<{ userName: string }>): string => {
return properties.userName || '';
};

const userName = fromFactory(factory)
.userName.set('john.doe1')
.build();
const userName = fromFactory(factory).setUserName('john.doe1').build();

console.log(userName === 'john.doe1'); // prints `true`
```
Expand Down
22 changes: 11 additions & 11 deletions package.json
Expand Up @@ -34,25 +34,25 @@
"devDependencies": {
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/chai": "^4.2.12",
"@types/mocha": "^8.0.3",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.0.4",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"chai": "^4.2.0",
"concurrently": "^5.3.0",
"coveralls": "^3.1.0",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"husky": "^4.3.0",
"lint-staged": "^10.4.0",
"mocha": "^8.1.3",
"lint-staged": "^10.5.2",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"prettier": "^2.1.2",
"semantic-release": "^17.1.2",
"prettier": "^2.2.1",
"semantic-release": "^17.3.0",
"ts-mockito": "^2.6.1",
"ts-node": "^9.0.0",
"typescript": "^4.0.3"
"typescript": "^4.1.2"
},
"scripts": {
"prebuild": "yarn clean",
Expand Down
4 changes: 2 additions & 2 deletions src/builders/fromClassConstructor.spec.ts
Expand Up @@ -18,7 +18,7 @@ describe('fromClassConstructor()', () => {
describe('#get()', () => {
it('should return a plain object with the set properties', () => {
const value = 'foo';
const test = getTestBuilder().property.set(value).get();
const test = getTestBuilder().setProperty(value).get();

expect(test).to.not.be.instanceOf(Test);
expect(test.property).to.be.equal(value);
Expand All @@ -28,7 +28,7 @@ describe('fromClassConstructor()', () => {
describe('#build()', () => {
it('should return a class instance with the set properties', () => {
const value = 'foo';
const test = getTestBuilder().property.set(value).build();
const test = getTestBuilder().setProperty(value).build();

expect(test).to.be.instanceOf(Test);
expect(test.testProperty).to.be.equal(value);
Expand Down
4 changes: 2 additions & 2 deletions src/builders/fromClassObject.spec.ts
Expand Up @@ -18,7 +18,7 @@ describe('fromClassObject()', () => {
describe('#get()', () => {
it('should return a plain object with the set properties', () => {
const value = 'foo';
const test = getTestBuilder().testProperty.set(value).get();
const test = getTestBuilder().setTestProperty(value).get();

expect(test).to.not.be.instanceOf(Test);
expect(test.testProperty).to.be.equal(value);
Expand All @@ -28,7 +28,7 @@ describe('fromClassObject()', () => {
describe('#build()', () => {
it('should return a class instance with the set properties', () => {
const value = 'foo';
const test = getTestBuilder().testProperty.set(value).build();
const test = getTestBuilder().setTestProperty(value).build();

expect(test).to.be.instanceOf(Test);
expect(test.testProperty).to.be.equal(value);
Expand Down
4 changes: 2 additions & 2 deletions src/builders/fromFactory.spec.ts
Expand Up @@ -26,7 +26,7 @@ describe('fromFactory()', () => {
describe('#get()', () => {
it('should return a plain object with the set properties', () => {
const value = 'foo';
const test = getTestBuilder().testInputProperty.set(value).get();
const test = getTestBuilder().setTestInputProperty(value).get();

expect(test).to.not.be.instanceOf(TestResult);
expect(test.testInputProperty).to.be.equal(value);
Expand All @@ -36,7 +36,7 @@ describe('fromFactory()', () => {
describe('#build()', () => {
it('should return a class instance with the set properties', () => {
const value = 'foo';
const builder = getTestBuilder().testInputProperty.set(value);
const builder = getTestBuilder().setTestInputProperty(value);
const test = builder.build();

verify(spiedFactory.factory(deepEqual(builder.get()))).once();
Expand Down
2 changes: 1 addition & 1 deletion src/builders/fromFactory.ts
@@ -1,5 +1,5 @@
import { makeBuilder } from '../makeBuilder';
import { Builder } from '../types/Builder';
import { makeBuilder } from '../makeBuilder';

export function fromFactory<Properties extends object, Result>(
factory: (properties: Partial<Properties>) => Result
Expand Down
2 changes: 1 addition & 1 deletion src/builders/fromInterface.spec.ts
Expand Up @@ -16,7 +16,7 @@ describe('fromInterface()', () => {
});

it('get and build should return the same object', () => {
const builder = getTestBuilder().testProperty.set('foo');
const builder = getTestBuilder().setTestProperty('foo');

expect(builder.get()).to.be.deep.equal(builder.build());
});
Expand Down
4 changes: 2 additions & 2 deletions src/builders/fromInterface.ts
@@ -1,6 +1,6 @@
import { makeBuilder } from '../makeBuilder';
import { Builder } from '../types/Builder';
import { makeBuilder } from '../makeBuilder';

export function fromInterface<Properties extends object>(): Builder<Properties> {
export function fromInterface<Properties extends object>(): Builder<Properties, Properties> {
return makeBuilder(properties => properties as Properties);
}
13 changes: 0 additions & 13 deletions src/helpers/mapValues.ts

This file was deleted.

48 changes: 0 additions & 48 deletions src/lazy.spec.ts

This file was deleted.

132 changes: 0 additions & 132 deletions src/lazy.ts

This file was deleted.

0 comments on commit 8b49e33

Please sign in to comment.