Skip to content

Instantly share code, notes, and snippets.

@lancetw
Last active September 26, 2018 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancetw/80f98935e4387d0bc7128db9e07f1027 to your computer and use it in GitHub Desktop.
Save lancetw/80f98935e4387d0bc7128db9e07f1027 to your computer and use it in GitHub Desktop.
let user = new User(filterByProps(form, [
'username',
'displayname'
]))
function filterByProps(obj: any, props: string[]): any {
if (!props) return obj
let ret = {}
Object.keys(obj).forEach(key => {
if (props.some(prop => prop === key)) {
ret[key] = clone(obj[key])
}
})
return ret
}
export interface user {
username: email | username
firstname?: string
lastname?: string
displayname?: string
}
export class User implements user {
username: email | username
firstname?: string
lastname?: string
displayname?: string
constructor(user?: user) {
Object.assign(this, user)
}
}
export function clone(obj: any): any {
return JSON.parse(JSON.stringify(obj))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment