OperateObjectAbstract.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import { IValue } from 'utils/types'
  21. export abstract class OperateObjectAbstract {
  22. public getTarget<T> (target: T): T {
  23. return target
  24. }
  25. public getTargetPropsByProperty <T, U extends keyof T> (target: T, property: U): IValue<T, U> {
  26. return target[property]
  27. }
  28. public setTargetPropsByProperty<T, K extends keyof T, U extends IValue<T, K>> (target: T, property: K, value: U): T {
  29. target[property] = value
  30. return target
  31. }
  32. public setTargetProps<T> (target: T, source: Partial<T>): T {
  33. try {
  34. Object.keys(target).forEach((property: keyof T) => {
  35. this.setTargetPropsByProperty(target, property, source[property])
  36. })
  37. } catch (error) {
  38. throw new Error(error)
  39. }
  40. return target
  41. }
  42. public static getPropsByPropery<T, U extends keyof T> (target: T, property: U): IValue<T, U> {
  43. return target[property]
  44. }
  45. }