Merge pull request #30282 from Deadly0/master

Update meteor-astronomy: improve Enum, add helpers.
This commit is contained in:
Armando Aguirre
2018-11-05 13:27:22 -08:00
committed by GitHub
2 changed files with 56 additions and 19 deletions
+15 -6
View File
@@ -7,6 +7,11 @@
/// <reference types="meteor" />
declare namespace MeteorAstronomy {
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]; // tslint:disable-line:ban-types
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
type FunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]; // tslint:disable-line:ban-types
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
type TypeOptionsPrimitives = typeof String | typeof Date | typeof Boolean | typeof Object | typeof Number;
type TypeOptions = TypeOptionsPrimitives | TypeOptionsPrimitives[] | Class<any> | Enum<any>;
type MongoQuery = object | string;
@@ -40,7 +45,11 @@ declare namespace MeteorAstronomy {
type ModelField<Field, Doc> = ModelFullField<Field, Doc> | TypeOptions;
type Fields<T> = {
[P in keyof T]: ModelField<T[P], T>;
[P in keyof NonFunctionProperties<T>]: ModelField<T[P], T>;
};
type Helpers<T> = {
[P in keyof FunctionProperties<T>]: (this: T, ...args: any[]) => any;
};
interface ClassModel<T> {
@@ -53,7 +62,7 @@ declare namespace MeteorAstronomy {
update: boolean,
remove: boolean,
} | boolean;
helpers?: object;
helpers?: Helpers<T>;
events?: object;
meteorMethods?: object;
indexes?: object;
@@ -61,7 +70,7 @@ declare namespace MeteorAstronomy {
interface EnumModel<T> {
name: string;
identifiers: T[] | object;
identifiers: string[] | T;
}
type Model<T> = T & {
@@ -92,10 +101,10 @@ declare namespace MeteorAstronomy {
update(search: object | string, query: object, callback?: () => void): void;
}
interface Enum<T> {
type Enum<T> = T & {
getValues(): any[];
getIdentifier(identifier: T): any;
}
getIdentifier(identifier: any): string;
};
}
declare module 'meteor/jagi:astronomy' { // tslint:disable-line:no-single-declare-module
@@ -65,8 +65,11 @@ const UserProfile = Class.create<UserProfileInterface>({
interface UserInterface extends Meteor.User {
address: object;
firstName: string;
lastName: string;
phone: string;
phoneNumber: string;
fullName: (param: string) => string;
}
const User = Class.create<UserInterface>({
@@ -74,9 +77,11 @@ const User = Class.create<UserInterface>({
collection: Meteor.users as Mongo.Collection<UserInterface>,
fields: {
createdAt: Number,
firstName: String,
lastName: String,
emails: {
type: [Object],
default: () => [],
default: (): Meteor.UserEmail[] => [],
},
profile: {
type: UserProfile,
@@ -91,10 +96,23 @@ const User = Class.create<UserInterface>({
},
phone: {
type: String,
resolve(doc) {
resolve(doc: UserInterface) {
return doc.phoneNumber;
},
},
},
helpers: {
fullName(param: string): string {
const fullName = `${this.firstName} ${this.lastName}`;
if (param === 'lower') {
return fullName.toLowerCase();
} else if (param === 'upper') {
return fullName.toUpperCase();
}
}
return fullName;
},
},
indexes: {
fullName: { // Index name.
@@ -111,13 +129,16 @@ const user = User.findOne();
user.set({username: 'user1'});
user.save();
enum IStatus {
OPENED, CLOSED, DONE, CANCELED
interface StatusInterface {
OPENED: number;
CLOSED: number;
DONE: number;
CANCELED: number;
}
const Status = Enum.create<IStatus>({
const Status = Enum.create<StatusInterface>({
name: 'Status',
identifiers: IStatus,
identifiers: ['OPENED', 'CLOSED', 'DONE', 'CANCELED'],
});
const Issue = Class.create({
@@ -131,18 +152,25 @@ const Issue = Class.create({
Status.getValues(); // [0, 1, 2, 3]
const StatusBis = Enum.create({
interface StatusBisInterface {
OPENED: string;
CLOSED: string;
DONE: string;
CANCELED: string;
}
const StatusBis = Enum.create<StatusBisInterface>({
name: 'Status',
identifiers: {
OPENED: 5,
CLOSED: null,
DONE: 15,
CANCELED: undefined
OPENED: 'OPENED',
CLOSED: 'CLOSED',
DONE: 'DONE',
CANCELED: 'CANCELED',
}
});
StatusBis.getValues(); // [5, 6, 15, 16]
const statusNumber = IStatus.OPENED;
const statusNumber = Status.OPENED;
Status.getIdentifier(statusNumber); // "OPENED"