aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/common/object.ts
blob: 9780b2594327af1b0113dd1e6ae574300e74d7da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> {
  const result: any = {}

  for (const key of keys) {
    if (Object.prototype.hasOwnProperty.call(object, key)) {
      result[key] = object[key]
    }
  }

  return result
}

function omit <O extends object, K extends keyof O> (object: O, keys: K[]): Exclude<O, K> {
  const result: any = {}
  const keysSet = new Set(keys) as Set<string>

  for (const [ key, value ] of Object.entries(object)) {
    if (keysSet.has(key)) continue

    result[key] = value
  }

  return result
}

function getKeys <O extends object, K extends keyof O> (object: O, keys: K[]): K[] {
  return (Object.keys(object) as K[]).filter(k => keys.includes(k))
}

function sortObjectComparator (key: string, order: 'asc' | 'desc') {
  return (a: any, b: any) => {
    if (a[key] < b[key]) {
      return order === 'asc' ? -1 : 1
    }

    if (a[key] > b[key]) {
      return order === 'asc' ? 1 : -1
    }

    return 0
  }
}

function shallowCopy <T> (o: T): T {
  return Object.assign(Object.create(Object.getPrototypeOf(o)), o)
}

function simpleObjectsDeepEqual (a: any, b: any) {
  if (a === b) return true

  if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
    return false
  }

  const keysA = Object.keys(a)
  const keysB = Object.keys(b)

  if (keysA.length !== keysB.length) return false

  for (const key of keysA) {
    if (!keysB.includes(key)) return false

    if (!simpleObjectsDeepEqual(a[key], b[key])) return false
  }

  return true
}

export {
  pick,
  omit,
  getKeys,
  shallowCopy,
  sortObjectComparator,
  simpleObjectsDeepEqual
}