vincent-pang / builder-pattern Goto Github PK
View Code? Open in Web Editor NEWBuilder pattern for Typescript using ES6 proxy
License: MIT License
Builder pattern for Typescript using ES6 proxy
License: MIT License
I really appreciate the idea of this API. Thanks.
The problem is that the API doesn't work on IE11 browser because IE11 doesn't support ES6 proxy.
It should be a good thing if this API work on IE11.
What do you think about that ?
Hi,
In our projects we usually use builders like this for usage in tests:
contact-builder.ts
export const aContact = () => Builder<Contact>().fullName(chance.string()).phoneNumber(chance.phone()).build();
This is done so that not every test needs to know "Builder" and "Contact" and "" to function.
The thing is that many tests, want to add an override on the default properties. So we end up writing the builders like so:
export const aContact = (props?: Partial<Contact>) => ({
...Builder<Contact>().fullName(chance.string()).phoneNumber(chance.phone()).build(),
...props,
});
Feature Request
Being able to call an "override" method before calling build (which is like calling the constructor):
export const aContact = (props?: Partial<Contact>) =>
Builder<Contact>()
.fullName(chance.string())
.phoneNumber(chance.phone())
.override(props)
.build();
Or simply use the build method:
export const aContact = (props?: Partial<Contact>) =>
Builder<Contact>()
.fullName(chance.string())
.phoneNumber(chance.phone())
.build(props);
I'm willing to donate the code, just want to know the feature will be accepted.
Vincent Peng, your open source lib for the builder pattern is a very good idea, thanks.
I have a question about performance.
I sometimes read that the ES6 proxies are not very fast and performant compared to the vanilla javascript.
If we use your lib, the performance are good ?
Can you give me an opinion about that please ?
Sorry to open an issue for that, but i wanted to send you a private message, but i didn't find your email address.
Thank your for developing this awesome library!
I use it to build domain object, but it will affect the annotations. the annotations in all field will lost
Currently the project is only distributed as CommonJS module.
To also support optimized bundle size it would also make sense to distribute the project as ESM module.
I have found an article about this topic that describes how to create an hybrid that uses the same codebase to build ESM and CommonJS distributions:
https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html
Hi, thank you for this great library! I was playing around with it and figured out a way to get Typescript to enforce required fields while still using your builder pattern.
You can see a proof of concept here.
Let me know if you have any questions or would like me to submit a pull request!
export interface Data {
title: string;
note?: string;
}
const a = Builder<Data>().title("test").note("note").build();
const b = Builder(a).note(undefined).build(); /// I am getting compilation error. Argument of type 'undefined' is not assignable to parameter of type 'string'
Even I declared the "note" as optional, the builder still doesn't allow set undefined to it. Could you tell me how to remove existing value using this builder library?
Exposing additional methods for collections would be helpful, for example in the Type class at present we have setter for name, properties and it would be useful if we can have addItem(key, property);
I tried adding a manual method inside Type class but it wasn't able to accept a object built out of builder lib. Found it strange.
export class Type {
name: string;
properties: Map<String, Property> = new Map<String, Property>();
export class Property {
name: string;
}
Hi. Thanks for this great library.
I've been using it for my side project, and encountered a problem.
My class looks like:
export class MyClass {
id:number;
public method(){};
}
I created a class instance by using builder as below:
const myClass: MyClass =
Builder<MyClass>()
.id(1)
.build();
I expected the instance would have the class method but the object does not have any method.
Does build method only support field properties, not the method?
Regards,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.