Created
February 2, 2024 16:06
-
-
Save Swahvay/bc91257bdd89371e5b77bf12c7359705 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { LocationBaseMixin } from './generated/location_base.ts'; | |
| type Constructor<T = {}> = new (...args: any[]) => T; | |
| interface ILocation { | |
| isAddressInCalifornia: () => boolean; | |
| } | |
| export function LocationMixin<T extends Constructor>(BaseClass: T) { | |
| return class LocationMixin extends LocationBaseMixin(BaseClass) implements ILocation { | |
| isAddressInCalifornia() { | |
| return this.state === 'CA'; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Data, Viewer, Ent } from '@snowtop/ent'; | |
| import { Country, convertNullableCountry } from '../types'; | |
| type Constructor<T = {}> = new (...args: any[]) => T; | |
| export function isLocation(ent: unknown): ent is ILocation { | |
| const o = ent as ILocation; | |
| return (o.isLocation && o.isLocation()) ?? false; | |
| } | |
| interface ILocation extends Ent { | |
| isLocation(): boolean; | |
| address: string | null; | |
| address2: string | null; | |
| city: string | null; | |
| state: string | null; | |
| postalCode: string | null; | |
| country: Country | null; | |
| } | |
| function extractFromArgs<TViewer extends Viewer, TData extends Data>( | |
| args: any[], | |
| ): { viewer: TViewer; data: TData } { | |
| if (args.length !== 2) { | |
| throw new Error('args should be length 2'); | |
| } | |
| return { | |
| viewer: args[0], | |
| data: args[1], | |
| }; | |
| } | |
| export function LocationBaseMixin<T extends Constructor>(BaseClass: T) { | |
| return class LocationBaseMixin extends BaseClass { | |
| readonly address: string | null; | |
| readonly address2: string | null; | |
| readonly city: string | null; | |
| readonly state: string | null; | |
| readonly postalCode: string | null; | |
| readonly country: Country | null; | |
| constructor(...args: any[]) { | |
| super(...args); | |
| const { data } = extractFromArgs(args); | |
| this.address = data.address; | |
| this.address2 = data.address_2; | |
| this.city = data.city; | |
| this.state = data.state; | |
| this.postalCode = data.postal_code; | |
| this.country = convertNullableCountry(data.country); | |
| } | |
| isLocation() { | |
| return true; | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
got it